创建一个Tkinter按钮以“清除”输出

时间:2013-08-15 18:18:23

标签: python tkinter

我有一个非常基本的程序,它会吐出一串值,但我不太清楚如何清除这些值。目前我设置它,以便我退出窗口并启动一个新窗口,这样我就不会一直重写新值。有没有一种简单的方法来添加另一个按钮,只是说“清除”,并确实这样做?我的代码如下:

def create_widgets(self):
        self.entryLabel = Label(self, text="Please enter a list of numbers:")
        self.entryLabel.grid(row=0, column=0, columnspan=2)   


    self.listEntry = Entry(self)
    self.listEntry.grid(row=0, column=2, sticky=E)

    self.entryLabel = Label(self, text="Please enter an index value:")
    self.entryLabel.grid(row=1, column=0, columnspan=2, sticky=E)

    self.indexEntry = Entry(self)
    self.indexEntry.grid(row=1, column=2)

    self.runBttn = Button(self, text="Run Function", command=self.psiFunction)
    self.runBttn.grid(row=2, column=0, sticky=W)

    self.answerLabel = Label(self, text="Output List:")
    self.answerLabel.grid(row=2, column=1, sticky=W)

    self.clearBttn = Button(self, text="Clear Output", command=)
    self.clearBttn.grid(row=3, column=0, sticky=W)

def clear():
    config.self.entryLabel(text="")

    tk.Button(text="write", command=write).grid()
    tk.Button(text="clear", command=clear).grid()

    self.clearBttn = Button(self, text="Clear Output", command=clear)
    self.clearBttn.grid(row=3, column=0, sticky=W)

1 个答案:

答案 0 :(得分:1)

你在这里问了两个不同的问题。我将解决第一个问题,因为这就是你的意思。要更改标签,只需使用config方法更新其文本:

import Tkinter as tk

root = tk.Tk()

label = tk.Label()
label.grid()

def write():
    label.config(text="Blah"*6)

def clear():
    label.config(text="")

tk.Button(text="write", command=write).grid()
tk.Button(text="clear", command=clear).grid()

root.mainloop()