2014-04-07 64 views
1

我正在用Python Tkinter創建一個GUI。Tkinter:將stdout重定向到只讀文本

我能夠將stdout重定向到文本字段,但不知道如何使其只讀(不允許用戶編輯)。

將stdout到目前爲止腳本:

class IORedirector(object): 
    '''A general class for redirecting I/O to this Text widget.''' 
    def __init__(self,text_area): 
     self.text_area = text_area 
class StdoutRedirector(IORedirector): 
    '''A class for redirecting stdout to this Text widget.''' 
    def write(self,message): 
     self.text_area.insert("insert", message) 

self.text_box = Text(self, wrap = 'word') 
self.text_box.grid(column = 0, row = 1, columnspan = 2, sticky = 'NSWE', padx = 5, pady = 5) 
sys.stdout = StdoutRedirector(self.text_box) 

回答

1

它實際上是很容易做到的。

附加2行類StdoutRedirector內:

class StdoutRedirector(IORedirector): 
    '''A class for redirecting stdout to this Text widget.''' 
    def write(self,message): 
     self.text_area.config(state = "normal") 
     self.text_area.insert("insert", message) 
     self.text_area.config(state = "disabled") 
相關問題