Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3. from random import randint
  4. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  5. from matplotlib.figure import Figure
  6. import time
  7. import threading
  8. from datetime import datetime
  9.  
  10. import queue
  11.  
  12. continuePlotting = False
  13.  
  14. def change_state():
  15. global continuePlotting
  16. if continuePlotting == True:
  17. continuePlotting = False
  18. else:
  19. continuePlotting = True
  20.  
  21. def data_points():
  22. yList = []
  23. for x in range (0, 20):
  24. yList.append(random.randint(0, 100))
  25.  
  26. return yList
  27.  
  28. def app():
  29. # initialise a window and creating the GUI
  30. root = Tk()
  31. root.config(background='white')
  32. root.geometry("1000x700")
  33.  
  34. lab = Label(root, text="Live Plotting", bg = 'white').pack()
  35.  
  36. fig = Figure()
  37.  
  38. def process_queue():
  39. try:
  40. msg = q.get(0)
  41. # Show result of the task if needed
  42. root.after(1000, process_queue)
  43. print("All okay")
  44. graph.draw()
  45. root.update_idletasks()
  46. except queue.Empty:
  47. root.after(1000, process_queue)
  48. print("Not okay")
  49.  
  50. q = queue.Queue()
  51. root.after(100, process_queue)
  52.  
  53. ax = fig.add_subplot(111)
  54. ax.set_ylim(0,100)
  55. ax.set_xlim(1,30)
  56. ax.grid()
  57.  
  58. graph = FigureCanvasTkAgg(fig, master=root)
  59. graph.get_tk_widget().pack(side="top",fill='both',expand=True)
  60.  
  61. # Updated the Canvas
  62. def plotter():
  63. while continuePlotting:
  64. ax.cla()
  65. ax.grid()
  66. ax.set_ylim(0,100)
  67. ax.set_xlim(1,20)
  68.  
  69. dpts = data_points()
  70. ax.plot(range(20), dpts, marker='o', color='orange')
  71.  
  72. q.put("Task finished")
  73.  
  74. time.sleep(1)
  75.  
  76. def gui_handler():
  77. change_state()
  78. threading.Thread(target=plotter).start()
  79.  
  80. b = Button(root, text="Start/Stop", command=gui_handler, bg="red", fg="white")
  81. b.pack()
  82.  
  83. root.mainloop()
  84.  
  85. if __name__ == '__main__':
  86. app()
Add Comment
Please, Sign In to add comment