文本小部件未使用删除功能清除



>我正在创建一个 tkinter gui 应用程序,我的一个框架有一个文本小部件,它调用一个函数并将其输出打印到小部件上。现在我有一个名为"activeAlarmButton"的按钮,它可以正确打印到小部件上,但是当我调用我的clearText((函数时,它不会删除内容。我已经尝试了各种格式的删除函数参数,但没有运气。每次我按下按钮时,它只会在旧输出下打印相同的输出。我的其他按钮还没有完成,我只想先让这个按钮工作。

class logsPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
activeAlarmButton = tk.Button(self, text = "Active Alarms", width=25, command = lambda: [clearText(), showActiveAlarms()])
activeAlarmButton.configure(bg = "light yellow")
activeAlarmButton.grid(column=1, row=1)
allAlarmButton = tk.Button(self, text = "All Alarms", width=25)
allAlarmButton.configure(bg = "light yellow")
allAlarmButton.grid(column=1, row=2)
backButton = tk.Button(self, text = "Go Back", width=25)
backButton.configure(bg = "light yellow")
backButton.grid(column=1, row=3)
alarmText = tk.Text(self, borderwidth=3, relief="sunken")
alarmText.configure(font=("Courier", 12), undo=True, wrap="word")
alarmText.grid(column=2, row = 1, rowspan=3)
sys.stdout = TextRedirector(alarmText, "stdout")
self.grid_rowconfigure(0, weight=2)
self.grid_rowconfigure(4, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(3, weight=1)
self.configure(background='light blue')
def clearText():
alarmText.delete('1.0', 'end')
alarmText.update()

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")
def flush(self):
pass

问题是TextRedirector类将文本小部件的状态保持为disabled。要删除文本,您首先需要完全按照TextRedirector执行的操作:将状态设置为normal,对文本小部件执行操作,然后将状态状态状态恢复为disabled

例:

def clearText():
alarmText.configure(state='normal')
alarmText.delete('1.0', 'end')
alarmText.configure(state='disabled')

最新更新