tkinter - Python threads and global variables -


i have class variables declared globals in first method. subsequent method starts thread, , problem python doesn't recognize global variables after t.start(). here how program works: 1) user can click "yes"-button on tkinter window 2) program starts upload data database. step takes while (2-5 minutes) , prevent ui freezing during upload, program starts thread performs sql stuff. @ same time, program clears widgets window , replaces them new widgets (a progress bar , text field). 3) after upload completed, program again refresh tkinter window new buttons , scrollbox.

here code snippet:

class application(tk.frame):      def __init__(self, parent):       #do init here..      def initui(self):         global text1, text2, button_no, button_yes, progress_bar #here globals         frame1 = tk.frame(self)         frame1.pack()         text1 = tk.label(self, text="do want upload new log file?", background="white")         button_yes = tk.button(self, text="yes", command=self.removebuttonyes)         button_no = tk.button(self, text="no", command=self.removebuttonno)         text1.pack()         button_yes.pack()         button_no.pack()         self.pack(fill=tk.both, expand=1)      def removebuttonno(self): #do here      def removebuttonyes(self):         text1.pack_forget() #first 3 lines clear original 3 widgets         button_no.pack_forget()         button_yes.pack_forget() #then add text progress bar         text2 = tk.label(self, text="transferring data. please wait...", background="white")         text2.pack()         progress_bar = ttk.progressbar(self, orient="horizontal", length=100, mode="indeterminate")         progress_bar.pack()         progress_bar.start(100)         #initialize thread prevent ui freezing during sql inserts         t = threading.thread(target=self.writelogtodatabase)         t.start()      def writelogtodatabase(self):         #open db connection, upload data , close db         self.clearui() #call method clear progress bar , info text      def clearui(self):              text2.pack_forget()         progress_bar.pack_forget() 

it throws following error message:

exception in thread thread-1: traceback (most recent call last):   file "c:\python27\lib\threading.py", line 810, in __bootstrap_inner     self.run()   file "c:\python27\lib\threading.py", line 763, in run     self.__target(*self.__args, **self.__kwargs)   file "c:\python27\test\testdb2.py", line 94, in writelogtodatabase     self.clearui()   file "c:\python27\test\testdb2.py", line 98, in clearui     text2.pack_forget() nameerror: global name 'text2' not defined 

why? can see, can call variables outside method declared. has threading - thing, not familiar with?

unless don't forget text2 , progress bar widgets, show in last window not desired functionality.

you should add global text2 on removebuttonyes method (and progress_bar, otherwise you'll have same problem again). it's useless add global text2 statement in function doesn't define variable.

also don't see advantage of using global variable in case, except it's easy create bugs. why don't use instance attribute self.text2 , self.progress_bar?


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -