Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # threading.py
- # fun challenge: list how many possible issues can time.sleep(seconds) cause
- from Tkinter import Tk, Label, Button, StringVar
- import time
- import random
- import threading
- root = Tk()
- root.title("Threading Test")
- root.geometry("300x200")
- class Cv(): 0
- cv = Cv()
- cv.list_of_threads = 0
- def of_threads(n):
- cv.list_of_threads += n
- my_threads.config(text='Number Of Threads = '+str(cv.list_of_threads))
- def five_seconds():
- my_label.config(text='Now Temporarily In This* Function...') # <<< ???
- of_threads(+1)
- print('!!!')
- time.sleep(5)
- of_threads(-1)
- if not cv.list_of_threads:
- my_label.config(text="Hello World !!!")
- print('...')
- def random_number():
- rnd = str(random.randint(0,999999)).zfill(6)
- my_random.config(text='Random Number = '+rnd)
- print(rnd)
- if __name__ == "__main__":
- my_label = Label(root, text="Hello World !!!")
- my_label.pack(pady=5)
- my_threads = Label(root, text="Number Of Processes = 0")
- my_threads.pack(pady=5)
- my_button_Z = Button(root, text="Pick Random Number",
- command=random_number, bg='yellow')
- my_button_Z.pack(pady=2,fill='x')
- my_button_A = Button(root, text="5 Seconds Without Threading",
- command=five_seconds, fg='white', bg='red')
- my_button_A.pack(pady=2,fill='x')
- my_button_B = Button(root, text="5 Seconds With Threading",
- command=lambda: threading.Thread(target=five_seconds).start(), bg='green')
- my_button_B.pack(pady=2,fill='x')
- my_random = Label(root, text="---")
- my_random.pack(pady=5)
- root.mainloop()
Add Comment
Please, Sign In to add comment