May-09-2019, 12:45 PM
Esteemed Forum Participants and Lurkers:
Platform: Linux Mint 18.3 Mate 64-bit, Python 3.5.2 & Idle3
I am working on a Python 3 program using TKinter which is essentially a keystroke driven state machine. I want to segregate the different states in 'modules'. The 'root' TKinter window is global to the whole program, but I can't seem to make it visible to a module, EXCEPT I CAN pass it to a function in a module as a parameter.
How do I make it available to the function as a global object which is what I REALLY want to learn how to do?
Main Program:
Traceback (most recent call last):
File "/media/mint/python/keystroke.py", line 19, in <module>
setup.setup()
File "/media/mint/python/setup.py", line 18, in setup
Glabel1 = tk.Label(Groot, text=prompt, width=len(prompt), bg='yellow')
NameError: name 'Groot' is not defined
As I mentioned, I can get this to work by passing 'Groot' as a parameter to setup.setup(), but how can I use 'Groot' as a global WITHOUT having to pass it as a parameter?
Thank you for any and all comments, suggestions, and assistance in this effort.
Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA
Platform: Linux Mint 18.3 Mate 64-bit, Python 3.5.2 & Idle3
I am working on a Python 3 program using TKinter which is essentially a keystroke driven state machine. I want to segregate the different states in 'modules'. The 'root' TKinter window is global to the whole program, but I can't seem to make it visible to a module, EXCEPT I CAN pass it to a function in a module as a parameter.
How do I make it available to the function as a global object which is what I REALLY want to learn how to do?
Main Program:
# Module Test for Globals and Modules import tkinter as tk Groot = tk.Tk() # Create the GLOBAL root window Glabel1 = [] # Global Label identifier GCOUNT = 0 # Global Keystroke counter # Pull in the "setup" module import setup # Create and load the display label setup.setup() # ######### Keyboard 'Keypress' event handler ######### def key(event): global GCOUNT GCOUNT += 1 msg = '%d %r %d %d' %(GCOUNT,event.keysym,event.keysym_num,event.state) print(msg) # ####################################################### # Bind the Keypress event to the 'key' event handler Groot.bind_all('<Key>', key) # Exit to the running event-driven app Groot.mainloop()Test Module:
# MODULE "setup.py" in moduletest import tkinter as tk # GLOBALS: global Groot global Glabel1 def setup(): # Create and load the display label into the window prompt = ' Press any key ' Glabel1 = tk.Label(Groot, text=prompt, width=len(prompt), bg='yellow') Glabel1.pack() return()Here is the error message:
Traceback (most recent call last):
File "/media/mint/python/keystroke.py", line 19, in <module>
setup.setup()
File "/media/mint/python/setup.py", line 18, in setup
Glabel1 = tk.Label(Groot, text=prompt, width=len(prompt), bg='yellow')
NameError: name 'Groot' is not defined
As I mentioned, I can get this to work by passing 'Groot' as a parameter to setup.setup(), but how can I use 'Groot' as a global WITHOUT having to pass it as a parameter?
Thank you for any and all comments, suggestions, and assistance in this effort.
Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA