Python Tkinter: How do I avoid global variables in a changing class? - python

I'm doing a large project at the moment to help me learn my first programming language (Python) and I've run into some unknown territory. I am aware that it's generally bad to use global variables and there are better solutions, but I can't figure it out for my situation.
I've made the code below as a simple example of what I want to achieve. What's the best way to do this instead of using the global variable?
Also, are there any general errors I've made in my code below?
Thanks in advance
from tkinter import *
root = Tk()
display_number = 5
class NumberBox():
def __init__(self):
global display_number
self.number_label = Label(root, text=display_number)
self.number_label.pack()
self.engine()
def engine(self):
self.number_label.config(text=display_number)
root.after(10, self.engine)
def change_number(operation):
global display_number
if operation == "add":
display_number += 1
if operation == "subtract":
display_number -= 1
Button(root, text="Add Class", command=lambda: NumberBox()).pack()
Button(root, text="Number UP", command=lambda: change_number("add")).pack()
Button(root, text="Number DOWN", command=lambda: change_number("subtract")).pack()
for _ in range(5):
NumberBox()
root.mainloop()

I made several changes:
Create a new class NumberBoxesList to avoid global variables and make program's logic more visible
Remove root.after method: this method should not be used for updates immediately following user's actions
Use import tkinter as tk : import * is bad practice
Result:
import tkinter as tk
class NumberBox():
def __init__(self, root, display_number):
self.number_label = tk.Label(root, text=display_number)
self.number_label.pack()
def changeNumber(self, display_number):
self.number_label.config(text=display_number)
class NumberBoxesList():
def __init__(self, root, start_number = 5):
self.number = start_number
self.root = root
self.boxes = []
tk.Button(root, text="Add Class", command= self.addBox).pack()
tk.Button(root, text="Number UP", command=lambda: self.change_number("add")).pack()
tk.Button(root, text="Number DOWN", command=lambda: self.change_number("subtract")).pack()
def addBox(self):
self.boxes.append(NumberBox(self.root, self.number))
def change_number(self, operation):
if operation == "add":
self.number += 1
if operation == "subtract":
self.number -= 1
for box in self.boxes:
box.changeNumber(self.number)
root = tk.Tk()
boxList = NumberBoxesList(root)
for _ in range(5):
boxList.addBox()
root.mainloop()

like this:
from tkinter import *
root = Tk()
class NumberBox():
display_number = 5
def __init__(self):
self.number_label = Label(root, text=self.display_number)
self.number_label.pack()
self.engine()
def engine(self):
self.number_label.config(text=self.display_number)
root.after(10, self.engine)
def change_number(operation):
if operation == "add":
NumberBox.display_number += 1
if operation == "subtract":
NumberBox.display_number -= 1
Button(root, text="Add Class", command=lambda: NumberBox()).pack()
Button(root, text="Number UP", command=lambda: change_number("add")).pack()
Button(root, text="Number DOWN", command=lambda: change_number("subtract")).pack()
for _ in range(5):
NumberBox()
root.mainloop()
by defining the variable in the class (but outside the __init__ it becomes owned by all instances of the class as a single variable, so changing it in one affects all instances

Related

Python Tkinter, can't get value of checkbutton as child

I created some widgets in loop. And i need to get values of em all. I coded:
from tkinter import *
class App():
def __init__(self):
self.ws = Tk()
self.frame = LabelFrame(self.ws)
self.frame.grid(row=1,column=1)
for i in range(16):
e = Label(self.frame, text=str(i + 1) + '.')
e.grid(row=i+1, column=1)
e1 = Entry(self.frame, width=8)
e1.grid(row=i+1, column=2)
e2 = Entry(self.frame)
e2.grid(row=i+1, column=3)
check = Checkbutton(self.frame, variable=BooleanVar(), onvalue=True, offvalue=False)
check.grid(row=i+1, column=4, sticky=E)
but = Button(self.ws,text='Get ALL',command=self.getall)
but.grid(row=17,column=1)
self.ws.mainloop()
def getall(self):
list = []
self.frame.update()
print('Child List:',self.frame.winfo_children())
for wid in self.frame.winfo_children():
if isinstance(wid,Entry):
list.append(wid.get())
elif isinstance(wid,Checkbutton):
self.frame.getvar(wid['variable'])
print('List:',list)
if __name__ == '__main__':
App()
It returns:
return self.tk.getvar(name)
.TclError: can't read "PY_VAR0": no such variable
If i click all checkbuttons, it doesn't error but returns empty strings..Whats wrong here?
TKinter couldn't retrieve the variables of your checkboxes, because you have created these variables on the fly inside __init__() scope, hence these variables are living in __init__() call stack only, and once __init__() finished its work, the garbage collector cleans these variables, so they are not reachable any more since they are not live in your main stack.
So, you need to keep them living in your main program stack. I have edited your code by adding a long-lived dict() for storing these checkboxes variables to be able to access them later.
from tkinter import *
class App():
def __init__(self):
self.ws = Tk()
self.frame = LabelFrame(self.ws)
self.frame.grid(row=1, column=1)
self.checkboxesValues = dict()
for i in range(16):
self.checkboxesValues[i] = BooleanVar()
self.checkboxesValues[i].set(False)
e = Label(self.frame, text=str(i + 1) + '.')
e.grid(row=i+1, column=1)
e1 = Entry(self.frame, width=8)
e1.grid(row=i+1, column=2)
e2 = Entry(self.frame)
e2.grid(row=i+1, column=3)
check = Checkbutton(self.frame, variable=self.checkboxesValues[i])
check.grid(row=i+1, column=4, sticky=E)
but = Button(self.ws, text='Get ALL', command=self.getall)
but.grid(row=17, column=1)
self.ws.mainloop()
def getall(self):
list = []
self.frame.update()
print('Child List:', self.frame.winfo_children())
for wid in self.frame.winfo_children():
if isinstance(wid, Entry):
list.append(wid.get())
elif isinstance(wid, Checkbutton):
list.append(self.frame.getvar(wid['variable']))
print('List:', list)
if __name__ == '__main__':
App()
Now, checkboxesValues variable lives in your App's object stack, so your checkboxes variables are existing in your memory as long as your object is not destroyed.

how to cancel the after method for an off delay timer in tkinter using python 3.8

I have a off delay program in which when I select the input checkbutton, the output is 1. When I deselect the input checkbutton, the output goes back to 0 after a timer (set up in a scale). For that I use the after method. That part works. My problem is that I want to reset the timer if the checkbutton is selected again before the output went to 0; but once the checkbutton is selected the first time, the after method get triggered and it doesn't stop. I'm trying to use after_cancel, but I can't get it to work. Any solution?
from tkinter import *
root = Tk()
t1= IntVar()
out = Label(root, text="0")
remain_time = IntVar()
grab_time = 1000
def start_timer(set_time):
global grab_time
grab_time = int(set_time) * 1000
def play():
if t1.get() == 1:
button1.configure(bg='red')
out.configure(bg="red", text="1")
else:
button1.configure(bg='green')
def result():
out.configure(bg="green", text="0")
out.after(grab_time,result)
button1 = Checkbutton(root,variable=t1, textvariable=t1, command=play)
time = Scale(root, from_=1, to=10, command=start_timer)
button1.pack()
time.pack()
out.pack()
root.mainloop()
Expected: when press the checkbutton before the output went to 0, reset the counter.
So you could use the .after_cencel when the value of checkbutton is 1:
from tkinter import *
root = Tk()
t1= IntVar()
out = Label(root, text="0")
remain_time = IntVar()
grab_time = 1000
def start_timer(set_time):
global grab_time
grab_time = int(set_time) * 1000
def play():
if t1.get() == 1:
button1.configure(bg='red')
out.configure(bg="red", text="1")
try: # when the first time you start the counter, root.counter didn't exist, use a try..except to catch it.
root.after_cancel(root.counter)
except :
pass
else:
button1.configure(bg='green')
def result():
out.configure(bg="green", text="0")
root.counter = out.after(grab_time,result)
button1 = Checkbutton(root,variable=t1, textvariable=t1, command=play)
time = Scale(root, from_=1, to=10, command=start_timer)
button1.pack()
time.pack()
out.pack()
root.mainloop()

How to restrict number of signs in Entry widgets with Tkinter

Hello i tried to do that in loop, but can't understand why only last created one is the only one restricted? I'd like to limit the 12 created widgets in loop to 4 signs. Can someone help me? :3
PS. Sorry if something uncleared, i ask question here for the first time.
from tkinter import *
import trace
import random
class Plansza:
def __init__(self, master):
self.frame = Frame(master, bg="brown")
self.frame.pack()
self.tab = [random.randint(1, 6),random.randint(1, 6),random.randint(1, 6),random.randint(1, 6)]
print(self.tab)
print(len(self.tab))
self.max_len = 4
self.wynik = StringVar()
self.wynik.set(self.tab)
self.goal = Entry(self.frame, width=6, font=50, fg="purple", justify=CENTER, textvariable=self.wynik, show="*")
self.goal.grid(row=0, column=1, padx=30, pady=30)
self.pokaz = Button(self.frame, text = "Pokaż", command=self.show)
self.pokaz.grid(row=0, column=2)
self.wiersz=1
print(self.wynik.get())
self.var = [1,1,1,1,1,1,1,1,1,1,1,1]
self.iter = 0
self.map()
self.sprawdz = Button(self.frame, text = "Sprawdź")
self.sprawdz.grid(row=self.wiersz+1, column=1, padx=50, pady=50)
def on_write(self, *arg):
s = self.var[self.iter].get()
if len(s) > self.max_len:
self.var[self.iter].set(s[:self.max_len])
def show(self):
self.goal.config(show="")
def map(self):
self.var[self.iter]=StringVar()
self.var[self.iter].trace_variable("w", self.on_write)
self.pole_na_odp = Entry(self.frame, width=6, font=50, fg="purple", justify=CENTER, textvariable=self.var[self.iter])
self.pole_na_odp.grid(row=self.wiersz, column=1, padx=20, pady=20)
self.wiersz+=1
self.var.append([])
self.iter+=1
if(self.wiersz<12):
self.map()
root = Tk()
b = Plansza(root)
root.mainloop()
[EDIT] I did a list but now I've got another error:
Whenever i wanna type something in my Entry widgets i got en error like this:
s = self.var[self.iter].get()
AttributeError: 'int' object has no attribute 'get'
And there are no more restrict number of sings even in last Entry widget.
(Answer to the question in your edit.)
In this line
s = self.var[self.iter].get()
it looks like you are trying to use get() to return an element of self.var. That is not how lists work. I'm pretty sure what you want is
s = self.var[self.iter]

Python - pass value back to main() based on when a button is pressed

Forgive me if this is a badly mangled way of doing things, but I'm new to development in general.
I am trying to create a window with a number of buttons using tkinter, each button having the name of a player on it using a class called from main().
I then want to be able to use the name on the button that is pressed later in the app, so I want to pass that back to main(). So, if I click on the Annie button, I want to open up a new window later called 'Options for Annie' and I'm assuming that the value 'Annie' needs to be passed back to the main function.
My main code:
<imports appear here>
def main():
players = ['Annie','Benny','Carrie','Donny']
winHome = playerWindow(root,players)
print("In main() : " + winHome.selected)
root.mainloop()
if __name__ == "__main__":
main()
My class code:
<imports appear here>
root=Tk()
class playerWindow():
def selectPlayer(self,selname):
self.selected = selname
print("In class: " + self.selected)
def __init__(self, master, players=[]):
colours = ['red','green','orange','white','yellow','blue']
self.selected = ""
r = 0
for p in players:
randcol = random.randint(0,len(colours))-1
if colours[randcol] in ('blue','green'):
fgcol='white'
else:
fgcol='black'
playername = delplayer = p
playername = Button(root, text=playername, bg=colours[randcol], fg=fgcol, width=15, command=lambda name = playername:self.selectPlayer(name)).grid(row=r,column=0)
s = ttk.Separator(root, orient=VERTICAL)
delplayer = Button(root, text='Del', bg='grey', fg='red', width=5, command=lambda name = delplayer: print("Delete Player %s" % name)).grid(row=r,column=1)
r = r + 1
Button(root, text="New Player", fg="black", command=lambda: print("New Player Functionality"), width=15).grid(row = len(players)+3,column=0)
Button(root, text="QUIT", fg="red", command=root.destroy, width=15).grid(row = len(players)+3,column=1)
What is happening is that the window gets created, the next line in main() is run (my added print statement) which is empty, obviously as main is continuing. When I press the button, the sleectPlayer function is called and works.
Somehow I need to get the value back to main() to go on to the next task using that value, however I don't seem to be able to frame the question correctly to get the answers I need.
Any help would be greatly appreciated.
I am using Python 3.5.1
You are already accessing it. I personally don't like returning to the main function, instead I suggest creating a top-level class to link back to. This should help make things flow better.
import tkinter as tk
from tkinter import ttk
import random
class PlayerWindow():
def __init__(self, master, parent, players=[]):
self._parent = parent
colours = ['red','green','orange','white','yellow','blue']
self.selected = ""
r = 0
for p in players:
randcol = random.randint(0,len(colours))-1
if colours[randcol] in ('blue','green'):
fgcol='white'
else:
fgcol='black'
playername = delplayer = p
playername = tk.Button(master, text=playername, bg=colours[randcol], \
fg=fgcol, width=15, command=lambda name = \
playername:self.selectPlayer(name)).grid(row=r,column=0)
s = ttk.Separator(master, orient=tk.VERTICAL)
delplayer = tk.Button(master, text='Del', bg='grey', fg='red', \
width=5, command=lambda name = delplayer: \
print("Delete Player %s" % name)).grid(row=r,column=1)
r = r + 1
tk.Button(master, text="New Player", fg="black", command=lambda: \
print("New Player Functionality"), width=15).\
grid(row = len(players)+3,column=0)
tk.Button(master, text="QUIT", fg="red", command=self._parent.close,
width=15).grid(row = len(players)+3,column=1)
def selectPlayer(self, selname):
self.selected = selname
print("In class: " + self.selected)
self._parent.hello() # call hello function of top-level, links back
class MyApplication(object):
def __init__(self, master):
self._master = master
players = ['Annie','Benny','Carrie','Donny']
self._player_window = PlayerWindow(master, self, players)
print("In main() : " + self._player_window.selected)
def hello(self):
name = self._player_window.selected
print("Hello, %s" % name)
def close(self):
# any other clean-up
self._master.destroy()
def main():
root = tk.Tk()
app = MyApplication(root)
root.mainloop()
if __name__ == "__main__":
main()

How can I update a text box 'live' in tkinter?

I would like to ask how would I go about maybe creating a 'LIVE' text box in python? This program is a simulator for a vending machine (code below). I want there to be a text box showing a live credit update How do you do that in tkinter?
For Example: Say there is a box for credit with 0 inside it in the middle of the window. When the 10p button is pressed the box for credit should change from '0' to '0.10'.
Is it possible to do thit in tkinter and python 3.3.2?
Thank you in advance!
import sys
import tkinter as tk
credit = 0
choice = 0
credit1 = 0
coins = 0
prices = [200,150,160,50,90]
item = 0
i = 0
temp=0
n=0
choice1 = 0
choice2 = 0
credit1 = 0
coins = 0
prices = [200,150,160,50,90]
item = 0
i = 0
temp=0
n=0
choice1 = 0
choice2 = 0
def addTENp():
global credit
credit+=0.10
def addTWENTYp():
global credit
credit+=0.20
def addFIFTYp():
global credit
credit+=0.50
def addPOUND():
global credit
credit+=1.00
def insert():
insert = Tk()
insert.geometry("480x360")
iLabel = Label(insert, text="Enter coins.[Press Buttons]").grid(row=1, column=1)
tenbutton = Button(insert, text="10p", command = addTENp).grid(row=2, column=1)
twentybutton = Button(insert, text="20p", command = addTWENTYp).grid(row=3, column=1)
fiftybutton = Button(insert, text="50p", command = addFIFTYp).grid(row=4, column=1)
poundbutton = Button(insert, text="£1", command = addPOUND).grid(row=5, column=1)
insert()
Sure you can! Just add another label to the frame, and update the text attribute whenever one of your add functions is called. Also, you can simplify that code, using one add function for all the different amounts.
def main():
frame = Tk()
frame.geometry("480x360")
Label(frame, text="Enter coins.[Press Buttons]").grid(row=1, column=1)
display = Label(frame, text="") # we need this Label as a variable!
display.grid(row=2, column=1)
def add(amount):
global credit
credit += amount
display.configure(text="%.2f" % credit)
Button(frame, text="10p", command=lambda: add(.1)).grid(row=3, column=1)
Button(frame, text="20p", command=lambda: add(.2)).grid(row=4, column=1)
Button(frame, text="50p", command=lambda: add(.5)).grid(row=5, column=1)
Button(frame, text="P1", command=lambda: add(1.)).grid(row=6, column=1)
frame.mainloop()
main()
Some more points:
note that you define many of your variables twice
you should not give a variable the same name as a function, as this will shadow the function
probably just a copy paste error, but you forgot to call mainloop and your tkinter import is inconsistent with the way you use the classes (without tk prefix)
you can do the layout right after creating the GUI elements, but note that in this case not the GUI element will be bound to the variable, but the result of the layouting function, which is None
Borrowing a framework from tobias_k's excellent answer, I would recommend you use a DoubleVar instead.
from tkinter import ttk
import tkinter as tk
def main():
frame = Tk()
frame.geometry("480x360")
credit = tk.DoubleVar(frame, value=0)
# credit = tk.StringVar(frame, value="0")
ttk.Label(frame, textvariable = credit).pack()
def add_credit(amt):
global credit
credit.set(credit.get() + amt)
# new_credit = str(int(credit.get().replace(".",""))+amt)
# credit.set(new_credit[:-2]+"."+new_credit[-2:])
ttk.Button(frame, text="10p", command = lambda: add_credit(0.1)).pack()
# ttk.Button(frame, text="10p", command = lambda: add_credit(10)).pack()
ttk.Button(frame, text="20p", command = lambda: add_credit(0.2)).pack()
# ttk.Button(frame, text="20p", command = lambda: add_credit(20)).pack()
ttk.Button(frame, text="50p", command = lambda: add_credit(0.5)).pack()
# ttk.Button(frame, text="50p", command = lambda: add_credit(50)).pack()
ttk.Button(frame, text="P1", command = lambda: add_credit(1.0)).pack()
# ttk.Button(frame, text="P1", command = lambda: add_credit(100)).pack()
frame.mainloop()
The comments in that code is an alternate implementation that will work better, if only just. This will guarantee you won't have any strange floating-point errors in your code.

Categories