Python: how to access a class's attribute from a module
You can achieve what you want by giving a reference of the Root instance as a parameter to your functions:
Instead of assigning to the Root class:
Root.status = Tkinter.StringVar()
Assign it to the Root instance:
self.status = Tkinter.StringVar()
There is no reason for assigning it to Root class instead of to the instance self, because its ownership is part of the Root instance as well: it is the components of the Root instance (the Tkinter parts) that fire the event to update.You could then give self as a parameter to your buttonevent:
def ok(self): text_info(self.msglist.next()) buttonevent.do_event(self) buttonevent.do_stuff(self)
And you can then make text_info part of your class:
class Root(object): ... def text_info(self, msg): self.textinfo.configure(state='normal') self.textinfo.insert(Tkinter.END, msg) self.textinfo.see(Tkinter.END)
And change the buttonevent to this:
def do_event(root_instance): # do something root_instance.text_info(msg.next())
All "Root." changed as "self."main.py
# ~/main.pyimport Tkinterimport buttoneventfrom itertools import cyclecurwin=Nonemsglist = ['main_msg1\n', 'main_msg2\n', 'main_msg3\n', 'main_msg4\n']class Root(object): def __init__(self, master): self.msglist = cycle(msglist) self.master = master self.frame1 = Tkinter.Frame(master) self.frame1.pack() self.status = Tkinter.StringVar() self.status_info = Tkinter.Label(self.frame1, textvariable=self.status) self.status_info.pack() self.status.set("Set by constructor") self.curmsg='message 1\n' self.frame2 = Tkinter.Frame(master) self.frame2.pack() self.textinfo = Tkinter.Text(self.frame2, width=20, height=10) self.textinfo.insert(Tkinter.END, 'message 1\n') self.textinfo.config(font='Arial') self.textinfo.pack() self.textinfo.config(bg=master.cget('bg'), relief=Tkinter.SUNKEN) self.textinfo.configure(state='disabled') self.frame3 = Tkinter.Frame(master) self.frame3.pack() self.button = Tkinter.Button(self.frame3, text='Ok', command=self.ok) # "Ok" function defined for click event self.button.pack() def ok(self): #self.text_info(buttonevent.curmsg) self.textinfo.configure(state='normal') self.textinfo.insert(Tkinter.END, buttonevent.curmsg) # get message from buttonevent.py and set for window. Fisrst clicking will get initalized curmsg value. if you want get value after click write buttonevent.do_event() above this codes. self.textinfo.see(Tkinter.END) self.textinfo.configure(state='disabled') buttonevent.do_event() # calling buttonevent's do_event function so it means global message will be changed. next click you will see new value for message. buttonevent.do_stuff()if __name__ == '__main__': root = Tkinter.Tk() curwin=Root(root) root.mainloop()
buttonevent.py
# ~/buttonevent.pyfrom itertools import cycleimport maindo_msg = ['do_msg1\n', 'do_msg2\n', 'do_msg3\n', 'do_msg4\n']msg = cycle(do_msg)curmsg=msg.next() # Add this variable to call main.py to first click eventdef do_event(): # do something global curmsg #to edit variable value you must call it as global curmsg=msg.next() #Changing variable value for each click #Removed text_info function from main.py it is not necessary.def do_stuff(): # do something print 'doing stuff'