114
114

Reputation: 926

Creating a Tkinter Button to 'clear' Output

I have a very basic program that spits out a string of values, but I am not quite sure how to clear these values. At the moment I have it set up so that I just exit the window and start a new one so that I'm not rewriting over new values all the time. Is there a simple way to add another button that just says something like 'clear' and does exactly that? My code is as below:

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)

Upvotes: 1

Views: 3966

Answers (2)

user28073772
user28073772

Reputation: 1

Use command like first define clear then use the command saying command=clear

Upvotes: 0

user2555451
user2555451

Reputation:

You kinda asked two different questions here. I'll address the first, since that is what you came in with. To change the label, just update its text using the config method:

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()

Upvotes: 2

Related Questions