Created
October 22, 2017 07:35
-
-
Save cryptogun/2134982c2d7cfb91f88f70d364a43f1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tkinter as tk | |
| import sys | |
| class ExampleApp(tk.Tk): | |
| def __init__(self): | |
| tk.Tk.__init__(self) | |
| toolbar = tk.Frame(self) | |
| toolbar.pack(side="top", fill="x") | |
| b1 = tk.Button(self, text="print to stdout", command=self.print_stdout) | |
| b2 = tk.Button(self, text="print to stderr", command=self.print_stderr) | |
| b1.pack(in_=toolbar, side="left") | |
| b2.pack(in_=toolbar, side="left") | |
| self.text = tk.Text(self, wrap="word") | |
| self.text.pack(side="top", fill="both", expand=True) | |
| self.text.tag_configure("stderr", foreground="#b22222") | |
| sys.stdout = TextRedirector(self.text, "stdout") | |
| sys.stderr = TextRedirector(self.text, "stderr") | |
| def print_stdout(self): | |
| '''Illustrate that using 'print' writes to stdout''' | |
| print("this is stdout") | |
| def print_stderr(self): | |
| '''Illustrate that we can write directly to stderr''' | |
| sys.stderr.write("this is stderr\n") | |
| class TextRedirector(object): | |
| def __init__(self, widget, tag="stdout"): | |
| self.widget = widget | |
| self.tag = tag | |
| def write(self, str): | |
| self.widget.configure(state="normal") | |
| self.widget.insert("end", str, (self.tag,)) | |
| self.widget.configure(state="disabled") | |
| if __name__ == '__main__': | |
| app = ExampleApp() | |
| app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment