Python TkInter bind breaking -
i have simple gui uses key binds - this.
import tkinter, tkfiledialog class foo(object): def __init__(self, master): master.bind('3', self.do_bar) master.bind('9', self.load_new_config) self.load_config() if not self.conf: self.load_new_config() else: self.load_data() def load_config(self): try: self.conf = #get stuff known file except failedtogetstuff: self.conf = none def load_new_config(self): path = askopenfilename(initialdir='~') self.conf = #get stuff file in path self.load_data() def load_data(self): #get data self.conf, process , display def do_bar(self): #do stuff displayed data if __name__ == "__main__" root = tk() foo(root) root.mainloop()
now, works fine when load_config() finds looking for. can use binds , after using '9' , loading new config, works.
problem is, if load_config() fails, self.conf gets set none , load_new_conf gets called __init__
, binds no longer operational.
i figured out problem caused tkfiledialog.askopenfilename() being called within __init__
. don't understand why happens , how around it.
this code works me:
import tkinter, tkfiledialog class foo(object): def __init__(self, master): master.bind('<keypress-3>', self.do_bar) master.bind('<keypress-9>', self.load_new_config) self.load_config() if not self.conf: master.after(1, self.load_new_config) else: self.load_data() def load_config(self): try: self.conf = none#get stuff known file except failedtogetstuff: self.conf = none def load_new_config(self, e = 0): path = tkfiledialog.askopenfilename(initialdir='~') self.conf = none#get stuff file in path self.load_data() def load_data(self, e = 0): pass #get data self.conf, process , display def do_bar(self, e = 0): print 1 #do stuff displayed data if __name__ == "__main__": root = tkinter.tk() foo(root) root.mainloop()
Comments
Post a Comment