furas

Python - tkinter - thread (reddit.com)

Oct 27th, 2016
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #
  2. # https://www.reddit.com/r/learnpython/comments/59nj50/while_loop_freezing_tkinter_gui_threading_issue/
  3. # http://pastebin.com/q877u6gf
  4. #
  5.  
  6. import tkinter as tk
  7. import threading
  8.  
  9. # --- constants --- (UPPER_CASE)
  10.  
  11. START = 1
  12. STOP  = 0
  13. EXIT  = -1
  14.  
  15. # --- functions ---
  16.  
  17. def test_loop():
  18.     global looping
  19.  
  20.     while True:
  21.         if looping == START:
  22.             print("test")
  23.         if looping == EXIT:
  24.             break
  25.            
  26. def start():
  27.     global looping
  28.  
  29.     looping = START
  30.  
  31. def stop():
  32.     global looping
  33.  
  34.     looping = STOP
  35.  
  36. # --- main ---
  37.  
  38. looping = STOP
  39.  
  40. root = tk.Tk()
  41.  
  42. text = tk.StringVar()
  43. text.set("Here's some text!")
  44.  
  45. leftFrame = tk.Frame(root)
  46. leftFrame.pack(side=tk.LEFT)
  47.  
  48. rightFrame = tk.Frame(root)
  49. rightFrame.pack(side=tk.RIGHT)
  50.  
  51. playButton = tk.Button(leftFrame, text="Play", fg="blue", command=start)
  52. playButton.pack(side=tk.TOP)
  53.  
  54. stopButton = tk.Button(rightFrame, text="Stop", fg="red", command=stop)
  55. stopButton.pack(side=tk.BOTTOM)
  56.  
  57. message = tk.Message(root, textvariable=text, relief=tk.RAISED)
  58. message.pack()
  59.  
  60. thread = threading.Thread(target=test_loop)
  61. thread.daemon = True
  62. thread.start()
  63.  
  64. root.mainloop()
  65.  
  66. looping = EXIT
Add Comment
Please, Sign In to add comment