May-10-2019, 06:30 AM
(May-10-2019, 04:28 AM)Webtest Wrote: I did also find the following ELEGANT and sanctioned solution, which works great for my program:It may work,buy is far from great
There is a reason why classes is almost always used and preferred in GUI programming.
Then there is no use of Globals ,
self
act like magic transporter of values.So a basic example with a class from doc,if i put in counter as you have it then it look like this.
See that there is no Global used to make this counter.
import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.button_clicks = 0 self.pack() self.create_widgets() def create_widgets(self): self.hi_there = tk.Button(self) self.hi_there["text"] = "Click me" self.hi_there["command"] = self.say_hi self.hi_there.pack(side="top") self.quit = tk.Button(self, text="QUIT",fg="blue",command=self.master.destroy) self.quit.pack(side="bottom") def say_hi(self): self.button_clicks += 1 print(f"hi there,you have clicked {self.button_clicks} times now") root = tk.Tk() app = Application(master=root) app.mainloop()