logScreen.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import tkinter as tk
  2. from tkinter.ttk import Progressbar
  3. import logHandler
  4. class LogScreen(tk.Frame):
  5. def __init__(self, root):
  6. self.root = root
  7. tk.Frame.__init__(self, root)
  8. self.log_handler = logHandler.get_log_handler()
  9. self.text = tk.Frame(self,relief="sunken",borderwidth=1)
  10. self.text.pack(expand=True,fill=tk.BOTH)
  11. self.disable_refresh = False
  12. self.y_scroll = tk.Scrollbar(self.text, width=32)
  13. self.y_scroll.pack(side="right", fill="y")
  14. self.x_scroll = tk.Scrollbar(self.text, orient='horizontal', width=32)
  15. self.x_scroll.pack(side="bottom", fill="x")
  16. self.textfield = tk.Listbox(self.text, yscrollcommand=self.y_scroll.set, xscrollcommand=self.x_scroll.set)
  17. self.textfield.pack(side="left",expand=True, fill=tk.BOTH)
  18. self.y_scroll.config(command=self.textfield.yview)
  19. self.x_scroll.config(command=self.textfield.xview)
  20. self.quit_button = tk.Button(self, text="Close", command=self.close, height=2, width = 10)
  21. self.quit_button.pack(side="right", fill="both")
  22. self.disable_refresh_button = tk.Button(self,text="disable refreshing", command=self.toggle_refreshing, height=2, width=20, relief="raised", bg="green", fg="white")
  23. self.disable_refresh_button.pack(side="right", fill="both")
  24. for element in self.log_handler.get_log_list():
  25. self.textfield.insert(tk.END, element)
  26. self.log_handler.get_new_items()
  27. root.bind('<Escape>', self.close)
  28. def update(self):
  29. if not self.root.winfo_exists():
  30. return
  31. if not self.disable_refresh:
  32. lines = self.log_handler.get_new_items()
  33. if lines:
  34. for element in lines:
  35. self.textfield.insert(tk.END, element)
  36. self.textfield.see("end")
  37. def close(self):
  38. if self.root.winfo_exists():
  39. self.root.destroy()
  40. def toggle_refreshing(self):
  41. self.disable_refresh = not self.disable_refresh
  42. if self.disable_refresh:
  43. self.disable_refresh_button.config(text="Enable refresh", relief="sunken", bg="red")
  44. else:
  45. self.disable_refresh_button.config(text="Disable refresh", relief="raised", bg="green")