【发布时间】:2016-06-03 07:57:18
【问题描述】:
我已经在 python 中做了一个很酷的小 CLI To-Do 应用程序,现在我正在尝试围绕它构建一个基本的 GUI。主要按钮、字段到位,昨天我已经弄清楚如何将返回字符串从我的 Todo 类重定向到文本区域。 我现在的问题是如何清除文本区域?目前,如果我按下“查看按钮”,它会不断重定向文本而不删除先前的输出。我试图用
创建一个函数self.text.delete('1.0','end') + 带有字符串输出的函数,但它不起作用。你能给我一些建议吗?提前致谢!
import tkinter as tk
from tkinter import messagebox as mbox
from todo_app import ToDo, arguments
import sys
class ToDoGui:
def __init__(self, root):
self.t = ToDo()
self.root = root
self.mainframe = tk.Frame(self.root, bg="white")
self.mainframe.pack(fill=tk.BOTH, expand=True)
self.build_grid()
self.build_banner()
self.build_text_area()
self.build_buttons()
sys.stderr = TextRedirector(self.text, "stderr")
self.entry()
def build_grid(self):
self.mainframe.columnconfigure(0, weight=1)
self.mainframe.rowconfigure(0, weight=0)
self.mainframe.rowconfigure(1, weight=1)
self.mainframe.rowconfigure(0, weight=0)
def build_banner(self):
banner = tk.Label(
self.mainframe,
bg="orange",
text="PyKeep",
fg="green",
font=('Helvetica', 24)
)
banner.grid(
row=0, column=0,
sticky='ew',
padx=10, pady=10
)
def build_buttons(self):
buttons_frame = tk.Frame(self.mainframe)
buttons_frame.grid(row=3, column=0, sticky='nsew',
padx=10, pady=10)
buttons_frame.columnconfigure(0, weight=1)
buttons_frame.columnconfigure(1, weight=1)
buttons_frame.columnconfigure(2, weight=1)
buttons_frame.columnconfigure(3, weight=1)
buttons_frame.columnconfigure(4, weight=1)
self.clear_button = tk.Button(
buttons_frame,
text='Clear',
command=self.text.delete('0.0', tk.END)
)
self.view_button= tk.Button(
buttons_frame,
text='View list',
command=self.t.list_view
)
self.add_button = tk.Button(
buttons_frame,
text='Add task',
command=None
)
self.remove_button = tk.Button(
buttons_frame,
text='Remove task',
command=None
)
self.complete_button = tk.Button(
buttons_frame,
text='Complete task',
command=None
)
self.clear_button.grid(row=0, column=0, sticky='ew')
self.view_button.grid(row=0, column=1, sticky='ew')
self.add_button.grid(row=0, column=2, sticky='ew')
self.remove_button.grid(row=0, column=3, sticky='ew')
self.complete_button.grid(row=0, column=4, sticky='ew')
def entry(self):
entry_field = tk.Entry(self.mainframe, bd=2)
entry_field.grid(row=1, column=0, sticky='nwse', padx=10, pady=10)
entry_field.insert(0, 'Enter task OR number of a task')
entry_field.focus()
def build_text_area(self):
text_frame = tk.Text(self.mainframe, wrap="word")
text_frame.grid(row=2, column=0, sticky='nsew',
padx=10, pady=10)
text_frame.columnconfigure(0, weight=1)
text_frame.config(state=tk.DISABLED)
text_frame.tag_configure("stderr", foreground="#b22222")
self.text = text_frame
return self.text
class TextRedirector(object):
def __init__(self, widget, tag="stderr"):
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__':
root = tk.Tk()
ToDoGui(root)
root.mainloop()
【问题讨论】:
-
是的,我试过了。 self.clear_button = tk.Button(buttons_frame, text='Clear', command=lambda: self.text.delete(1.0, 'end') ) 不清除字段。
-
“不工作”是什么意思?要删除文本小部件,您完全按照您所说的尝试:
self.text.delete("1.0", "end")。 -
您不需要所有这些代码来重现问题。请删除与问题不严格相关的所有内容。解决问题所需的只是一个文本小部件和一个按钮,以及清除您所说的窗口的代码不起作用。见stackoverflow.com/help/mcve
标签: python user-interface tkinter text