Tkinter button only works once - python-3.x

Tkinter button only runs a separate script once
Hello all, Im a new to python and raspberry pi and have been searching high and low on how to get a Tkinter button to run a script more than once on my raspberry pi. From research I believe it has something to do with name="main", but I cant figure out what needs to be done and why. My button runs a separate python file (called SendRF.py) in the same directory that generates an RF signal, it works the first time but then the button click does nothing else after. Any advice would be much appreciated :)
from tkinter import *
#create a window
window =Tk()
window.title("Chappers Home Automation project")
#define a function
def test_function ():
import SendRF
#create a button
B = Button(text ="Test Button 1", command=test_function)
B.pack(padx = 100, pady = 50)
window.mainloop()
No error messages appear. The button sends the RF signal when pressed the first time, but nothing happens for further button clicks.

You can't import a module multiple times. Each additional import for the same module is a NOP. You need to functionize whatever is in sendRF, and call that function in test_function.

You can check if the function is working correctly by adding a simple print statement inside your function
from tkinter import *
#create a window
window =Tk()
window.title("Chappers Home Automation project")
#define a function
def test_function ():
import SendRF
print('CHECK')
#create a button
B = Button(text ="Test Button 1", command=test_function)
B.pack(padx = 100, pady = 50)
window.mainloop()

It is working one time, cause you alredy have the SendRf function mported, you need to close it, after to import again

Related

How to close first window when second window apparent

When I choose either on the first window and then the second window are apparent, but the first window not missing.
from tkinter import *
import subprocess
master = Tk()
master.title("CPG")
master.geometry('460x430')
master.P1 = PhotoImage(file=r"A1.png")
master.P2 = PhotoImage(file=r"A2.png")
def program1():
subprocess.Popen("E://Game Online//Steam//Apex.exe)
def program2():
subprocess.Popen("E://Game Online//Steam//Steam.exe")
def close():
master.destroy()
master.quit()
Button(master,image=master.P1, command=program1).pack(side=TOP)
Button(master,image=master.P2, command=program2).pack()
master.mainloop()
For example, running program:
There are 2 choice for my program:
choose for running by Apex Steam and
choose for running by Apex Origin.
When I choose either on the first window, the second window appears; but the first window does not disappear. How can I fix it?

Error when trying to open new window with label+image in tkinter

I ran into a probleme when coding a bigger project (at least for me as an absolute newby). I want to open a new tk-window with some text and an image by pressing a Button from another tk-window. I created a code for demonstration.
import tkinter as tk
def fct():
testwin = tk.Tk()
testbutt1 = tk.Button(testwin, text="Button1").pack()
img = tk.PhotoImage(master=testwin,file="picture.png")
imglabel = tk.Label(root,image=img)
imglabel.pack()
testbutt2 = tk.Button(testwin, text="Button2").pack()
testwin.mainloop()
root = tk.Tk()
picbutt = tk.Button(root,text="Exit",command=fct).pack()
label = tk.Label(root)
label.after(5000, fct)
root.mainloop()
This code throws error: "_tkinter.TclError: image "pyimage1" doesn't exist"
When I exchange
img = tk.PhotoImage(master=testwin,file="picture.png")
to
img = tk.PhotoImage(testwin,file="picture.png")
I get error: "images may not be named the same as the main window"
Changing import tkinter as tk to from tkinter import * sadly doesn't change anything either. It also doesnt matter if fct() is called by pressing the button or waiting 5 seconds (label.after(5000, fct))
Python stops executing code at this point. Meaning, testbutt1 shows up, testbutt2 does not.
Hope this explaination is enough to describe the probleme. Thanks for your answers :)

How to add background image to an tkinter application?

I recently made a Python watch application. But it looks very boring because of the one-color background and just nothing except of the time and the date. So I wanted to add background to the application. And I ran into a problem: If I just pack the image into a label, the other labels won't be transparent and will have their own background. I tried to use tkinter.Canvas, but it doesn't work with labels. Any suggestions?
That's the code for the first version of the app:
from tkinter import *
from time import sleep
from random import choice
import datetime
root=Tk()
BGCOLOR='#000000'
FGCOLOR='#FFFFFF'
root.overrideredirect(True)
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
root.geometry(str(w)+'x'+str(h)+'+0+0')
root.configure(bg=BGCOLOR)
date=str(datetime.datetime.now()).split()[0].split('-')
tl=Label(root,text=str(datetime.datetime.now()).split()[1].split('.')[0],font=['URW Gothic L',300], fg=FGCOLOR, bg=BGCOLOR)
dl=Label(root,text=date[2]+'.'+date[1]+'.'+date[0],font=['URW Gothic L',100],bg=BGCOLOR,fg=FGCOLOR)
tl.pack(expand=True, fill=BOTH)
dl.pack(expand=True,fill=BOTH)
Button(master=root,text='X',bg=BGCOLOR,fg=FGCOLOR,command=root.destroy).pack(side=RIGHT)
while True:
tl.config(text=str(datetime.datetime.now()).split()[1].split('.')[0])
if date!=str(datetime.datetime.now()).split()[0].split('-'):
date=str(datetime.datetime.now()).split()[0].split('-')
dl.config(text=date[2]+'.'+date[1]+'.'+date[0])
dl.update()
tl.update()
sleep(1)
Ok, so the solution itself is, to create a Canvas object and use create_text() function with "fill" and "font" arguments. The best thing about this is that you can change the position of the text

Tkinter display different frames

UPDATE : The problem was solved by removing the window.mainloop() in my second function.
I'm trying to make a game in Python 3.7 using tkinter.
The game begins with a menu (button-widgets in a frame). Clicking in the 'Play' button should open another menu using a different frame. This second menu should contain a 'back' button to return to the first menu.
Each menu is defined in a function. So to go from the main menu to the play menu I call the function playMenu(window) in the function used as command by the 'Play' button.
It looks like this :
def clickButtonPlay():
menuFrame.grid_remove()
playMenu(window)
menuFrame.grid()
In the play menu, the function used as 'back button' command put an end to the function by destroying its frame and using return.
So the program should get back to the clickButtonPlay() function and show the frame of the main menu back, but instead I get a tkinter error :
_tkinter.TclError: can't invoke "grid" command: application has been destroyed
But my frame menuFrame hasn't been destroyed, just un-grid!
Can anyone help me understand what's wrong with the code or find an easier way to do the same thing?
Thank you very much!
Here's a sample of how my program works:
mainMenu file :
import tkinter as tk
from PlayMenu import playMenu
window = tk.Tk()
window.grid()
def menu(window):
def clickButtonPlay():
menuFrame.grid_remove()
playMenu(window)
menuFrame.grid()
menuFrame = tk.Frame(window)
menuFrame.grid()
background = tk.Label(menuFrame, image= backgroundImage)
background.grid()
playButton = tk.Button(menuFrame, image= playButtonImage[0], command= clickButtonPlay)
playButton.place(relx= 0.5, rely= 0.15)
window.mainloop()
menu(window)
playMenu file :
class MyError(Exception):
pass
def _playMenu(window):
def clickButtonBack():
playMenuFrame.destroy()
raise MyError
playMenuFrame = tk.Frame(window)
playMenuFrame.grid()
background = tk.Label(playMenuFrame, image= backgroundImage)
background.grid()
backButton = tk.Button(playMenuFrame, image= backButtonImage[0], command= clickButtonBack)
backButton.place(relx=0.375, rely=0.8)
window.mainloop()
def playMenu(window):
try:
return _playMenu(window)
except MyError:
return
The problem (or at least a problem) is that you're calling mainloop more than once. Each time you call it, a new infinite loop is created. The new loop won't exit until the main window is destroyed. Once that happens, the previous loop will likely throw errors since the widgets it's managing no longer exist.

Button color not working , tkinter Python3 [duplicate]

I've been working through the Tkinter chapters in Programming Python and encountered a problem where the foreground and background colours of a button will not change. I am working on a Mac OS X 10.6 system with Python 2.6.1. The colours of a label will change, but not the colours of a button. For example:
from Tkinter import *
Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()
mainloop()
On my Mac system the colours of the label change, but the colours of the button do not. On a Windows system with Python 2.6.1 the colours of both the label and button change.
Anyone know what is going wrong?
I've checked Interface Builder and it appears that there is no option to change the foreground or background colour of a button in that tool. There is the ability to edit the foreground and background colours of a label.
The Mac OS X rendering system (Quartz?) may just not support (easily) changing the fg and bg of a button.
There is a solution for changing the background of buttons on Mac.
Use:
highlightbackground=color
For example:
submit = Button(root, text="Generate", highlightbackground='#3E4149')
This results in the following, a nice button that fits in with the background:
I think the answer is that the buttons on the mac simply don't support changing the background and foreground colors. As you've seen, this isn't unique to Tk.
You can do it with tkmacosx library from PyPI.
Installation:
For Python 2, use pip install tkmacosx.
For Python 3, use pip3 install tkmacosx.
This is how you use tkmacosx:
from tkinter import *
from tkmacosx import Button
root = Tk()
B1 = Button(root, text='Mac OSX', bg='black',fg='green', borderless=1)
B1.pack()
root.mainloop()
It works fine on Mac OS X.
For anyone else who happens upon this question as I did, the solution is to use the ttk module, which is available by default on OS X 10.7. Unfortunately, setting the background color still doesn't work out of the box, but text color does.
It requires a small change to the code:
Original:
from Tkinter import *
Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()
mainloop()
With ttk:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# background="..." doesn't work...
ttk.Style().configure('green/black.TLabel', foreground='green', background='black')
ttk.Style().configure('green/black.TButton', foreground='green', background='black')
label = ttk.Label(root, text='I am a ttk.Label with text!', style='green/black.TLabel')
label.pack()
button = ttk.Button(root, text='Click Me!', style='green/black.TButton')
button.pack()
root.mainloop()
Its quite annoying that after years this is still a problem.
Anyways, as others have mentioned, highlightbackground (the border color) can be used in place of background on a Mac. If you increase the size of the border to be huge (the size of the button or greater), you will get a nice, solid background color. This will give your button the appearance of a label.
This works if you are using place, but not if you are using something like grid. With grid, increasing the border size increases the button size automatically, unfortunately.
However, if you must use grid, you can always hack it....create your colorless grid button. Next use place to parent a background color button on top of it. This will be the button with the 'command' on it or the button you bind events to.
If you want your code to be OS independent, you can either add an 'if OS == "Mac"' statement or even add a custom function that modifies the button if its on a Mac but leaves it alone on Windows or Linux. Here's the former:
from tkinter import *
import platform
if platform.system() == "Darwin": ### if its a Mac
B = Button(text="Refersh All Windows", highlightbackground="Yellow", fg="Black", highlightthickness=30)
else: ### if its Windows or Linux
B = Button(text="Refresh All Windows", bg="Yellow", fg="Black")
B.place(x=5, y=10, width=140, height=30)
mainloop()
This worked for me:
self.gnuplot_bt = Button(
self.run_but_container, text="Plot with Gnuplot", font="Helvetica", command=self.gnuplot,
highlightbackground ="#8EF0F7", pady=2, relief=FLAT
)
I was looking as to why this doesn't work as well. I found a quick way to try and fix it is to have a label and then bind a click with the label. Then have the label change colors for a short time to mimic clicking. Here is an example.
def buttonPress(*args):
searchB.config(state = "active")
searchB.update()
time.sleep(0.2)
searchB.config(state = "normal")
## Whatever command you want
searchB = Label(main, text = "Search", bg = "#fecc14", fg = "Black", activebackground = "Red", highlightbackground="Black")
searchB.bind("<Button-1>", startSearch)
searchB.pack()
Confirm following code can change the background of tkinter Button on Mac OS X.
self.btn_open = tk.Button(self.toolbar,
text = "Open",
command=self.open,
highlightbackground = "gray")
But it cannot change bg of ttk.Button.
Not sure if anyone is still viewing this thread, but I have created a simple solution to this problem by creating my own Button class. It is available on GitHub.
import tkinter as tk
class Button():
button_frame = None
root = None
width=100
height=20
text=""
bg="white"
fg="black"
font="f 12"
bordercolor = "black"
bordersize = 3
label = None
command = None
def __init__(self,root,width=100,height=20,text="",bg="white",fg="black",font="f 12",command=None,bordercolor="black",bordersize=0):
self.root = root
self.width=width
self.height=height
self.text=text
self.bg=bg
self.fg=fg
self.font=font
self.command = command
self.bordercolor = bordercolor
self.bordersize = bordersize
self.button_frame = tk.Frame(root,width=width,height=height,bg=bg)
self.label = tk.Label(self.button_frame,text=self.text,bg=self.bg,width=self.width,height=self.height,fg=self.fg,font=self.font,highlightbackground=self.bordercolor,highlightthickness=self.bordersize)
self.label.place(anchor="center",relx=0.5,rely=0.5,relheight=1,relwidth=1)
self.label.bind("<Button-1>",self.call_command)
def call_command(self,event):
if (self.command != None):
self.command()
def place(self,anchor="nw",relx=0,rely=0):
self.button_frame.place(anchor=anchor,relx=relx,rely=rely)
def configure(self,width=width,height=height,text=text,bg=bg,fg=fg,font=font,command=command,bordercolor=bordercolor,bordersize=bordersize):
self.button_frame.configure(width=width,height=height,bg=bg)
self.label.configure(text=text,bg=bg,width=width,height=height,fg=fg,font=font,highlightbackground=bordercolor,highlightthickness=bordersize)
self.command =
Button and Label seem pretty similar to me, so I find it odd that the Label and Button work differently... even after all these years.
You can always make your own Button class which is wrapped around a Label with a border (default width is 2) and a bind call for the Button Release. You'd miss out on some of the "animation" of button press and release, but you'd get your background and foreground colors as desired.
I wrote a project called Tagged Text Widgets ('ttwidgets' on PyPI.org) which essentially does just that. I wrote the project to allow multi-font, multi-color Buttons and Labels. Essentially the project creates a compound Button or Label consisting of multiple underlying Label widgets (each with its own color/font) but acting like a single object. Those different colors and fonts are created by passing in HTML-like tagged text in lieu of regular text. And because of the underlying Labels rather than Buttons, it works around the issue on macOS.
I just tested it on macOS Sierra, and it works around the Button bg/fg color problem.
You can use it as follows:
from ttwidgets import TTButton
A TTButton supports the full interface of a Tkinter Button but with many enhancements. But for someone trying to work around the macOS color issue, just using a TTButton in lieu of a Tkinter Button suffices.

Resources