I’m very new to python and I am stuck trying to create module while using tkinter. I have a main module that I want to use for the top menu and another module with reports that also use tkinter objects.
The first script is main.py as below
from Tkinter import *
from wind import *
menu=Tk()
menu.geometry('200x200')
Button(menu,text="push",command=wind.gui).pack()
menu.mainloop()
The second script is wind.py as below.
from Tkinter import *
class wind:
@staticmethod
def getting():
print y2,y2.get()
@staticmethod
def gui():
global y2
main=Tk()
main.geometry('300x300+100+100')
y2=StringVar()
Entry(main, textvariable=y2, width=40).pack()
Button(main, text="Run", command=wind.getting).pack()
main.mainloop()
The code seems to work fine. However, the value of y2 from the tkinter button comes empty. Any help would be greatly appreciated!!
Bryan’s right there. Your use of two mainloops looks a tad odd to me. There are cases where you fire off a secondary loop in Tkinter, but not like this.
Also not sure where your y2 is defined, but global in a module refers to that module’s global space, not the overall program’s global space. At least that’s how I interpret this note about the globals() function and how it probably pertains to the global keyword.
I made the following changes to wind.py and got a more reasonable response from the program:
Notice I made y2 an actual global level variable of the wind.py module, made main be a Toplevel() widget and commented out the spurious mainloop() at the end of gui() (since you’re already in a main loop from the first window).