Python Forum
[Tkinter] HOW TO: Use Globals In Module ???
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] HOW TO: Use Globals In Module ???
#4
(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 Wink
There is a reason why classes is almost always used and preferred in GUI programming.
Then there is no use of Globals Sick ,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()
Reply


Messages In This Thread
HOW TO: Use Globals In Module ??? - by Webtest - May-09-2019, 12:45 PM
RE: HOW TO: Use Globals In Module ??? - by Webtest - May-10-2019, 04:28 AM
RE: HOW TO: Use Globals In Module ??? - by snippsat - May-10-2019, 06:30 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020