Guest User

Untitled

a guest
Oct 22nd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import tkinter as tk
  2. import sys
  3.  
  4.  
  5. class ExampleApp(tk.Tk):
  6. def __init__(self):
  7. tk.Tk.__init__(self)
  8. toolbar = tk.Frame(self)
  9. toolbar.pack(side="top", fill="x")
  10. b1 = tk.Button(self, text="print to stdout", command=self.print_stdout)
  11. b2 = tk.Button(self, text="print to stderr", command=self.print_stderr)
  12. b1.pack(in_=toolbar, side="left")
  13. b2.pack(in_=toolbar, side="left")
  14. self.text = tk.Text(self, wrap="word")
  15. self.text.pack(side="top", fill="both", expand=True)
  16. self.text.tag_configure("stderr", foreground="#b22222")
  17.  
  18. sys.stdout = TextRedirector(self.text, "stdout")
  19. sys.stderr = TextRedirector(self.text, "stderr")
  20.  
  21. def print_stdout(self):
  22. '''Illustrate that using 'print' writes to stdout'''
  23. print("this is stdout")
  24.  
  25. def print_stderr(self):
  26. '''Illustrate that we can write directly to stderr'''
  27. sys.stderr.write("this is stderr\n")
  28.  
  29.  
  30. class TextRedirector(object):
  31. def __init__(self, widget, tag="stdout"):
  32. self.widget = widget
  33. self.tag = tag
  34.  
  35. def write(self, str):
  36. self.widget.configure(state="normal")
  37. self.widget.insert("end", str, (self.tag,))
  38. self.widget.configure(state="disabled")
  39.  
  40.  
  41. if __name__ == '__main__':
  42. app = ExampleApp()
  43. app.mainloop()
Add Comment
Please, Sign In to add comment