here2share

# threading.py

Mar 17th, 2021 (edited)
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. # threading.py
  2.  
  3. # fun challenge: list how many possible issues can time.sleep(seconds) cause
  4.  
  5. from Tkinter import Tk, Label, Button, StringVar
  6. import time
  7. import random
  8. import threading
  9.  
  10. root = Tk()
  11. root.title("Threading Test")
  12. root.geometry("300x200")
  13.  
  14. class Cv(): 0
  15. cv = Cv()
  16.  
  17. cv.list_of_threads = 0
  18.  
  19. def of_threads(n):
  20.     cv.list_of_threads += n
  21.     my_threads.config(text='Number Of Threads = '+str(cv.list_of_threads))
  22.  
  23. def five_seconds():
  24.     my_label.config(text='Now Temporarily In This* Function...') # <<< ???
  25.     of_threads(+1)
  26.     print('!!!')
  27.     time.sleep(5)
  28.     of_threads(-1)
  29.     if not cv.list_of_threads:
  30.         my_label.config(text="Hello World !!!")
  31.     print('...')
  32.  
  33. def random_number():
  34.     rnd = str(random.randint(0,999999)).zfill(6)
  35.     my_random.config(text='Random Number = '+rnd)
  36.     print(rnd)
  37.  
  38. if __name__ == "__main__":
  39.     my_label = Label(root, text="Hello World !!!")
  40.     my_label.pack(pady=5)
  41.    
  42.     my_threads = Label(root, text="Number Of Processes = 0")
  43.     my_threads.pack(pady=5)
  44.  
  45.     my_button_Z = Button(root, text="Pick Random Number",
  46.         command=random_number, bg='yellow')
  47.     my_button_Z.pack(pady=2,fill='x')
  48.  
  49.     my_button_A = Button(root, text="5 Seconds Without Threading",
  50.         command=five_seconds, fg='white', bg='red')
  51.     my_button_A.pack(pady=2,fill='x')
  52.  
  53.     my_button_B = Button(root, text="5 Seconds With Threading",
  54.         command=lambda: threading.Thread(target=five_seconds).start(), bg='green')
  55.     my_button_B.pack(pady=2,fill='x')
  56.  
  57.     my_random = Label(root, text="---")
  58.     my_random.pack(pady=5)
  59.  
  60.     root.mainloop()
Add Comment
Please, Sign In to add comment