May 2017
Beginner to intermediate
444 pages
11h 31m
English
By adding args=[8] to the thread constructor and modifying the targeted method to expect arguments, we can pass arguments to the threaded methods. The parameter to args has to be a sequence, so we will wrap our number in a Python list:
def method_in_a_thread(self, num_of_loops=10): print('Hi, how are you?') for idx in range(num_of_loops): sleep(5) self.scrol.insert(tk.INSERT, str(idx) + 'n')
In the following code, run_thread is a local variable, which we only access within the scope of the method inside which we created run_thread:
# Running methods in Threads def create_thread(self): run_thread = Thread(target=self.method_in_a_thread, args=[8]) run_thread.start()
By turning the local variable into a member, we can then ...
Read now
Unlock full access