live for the hunt
live for the hunt

Reputation: 9

I've created a "clear" button in my GUI I'm working on using tkinter. How do I get this button to clear text?

The problem I'm running into is trying to clear the text entered, when the clear button is pressed. I understand there's something called command binding, and passing in a function that could clear the text as a reference to the command. I'm just a bit confused as to go about doing so.

Here's the relevant code:

class Window(Frame):
    def __init__(self, master):
        super().__init__(master)
        master.title('Learning Python')
        master.configure(background='black')
        master.geometry('900x200')
        master.resizable(0, 0)


class Submit(Button):
    def __init__(self, master):
        super().__init__(master)
        self.configure(text='Submit', background='black', foreground='light green', highlightthickness=0,
                       border=0)


class Clear(Button):
    def __init__(self, master):
        super().__init__(master)
        self.configure(text='Clear', background='black', foreground='light green', highlightthickness=0,
                       border=0)


class ProgressBar(ttk.Progressbar):
    def __init__(self, master):
        super().__init__(master)
        self.config(orient='horizontal', maximum=1462, mode='determinate')


class PagesRead(Label):
    def __init__(self, master):
        super().__init__(master)
        self.config(text='How many page(s) did you read?', background='black', foreground='light green')


class EntryBox(Entry):
    def __init__(self, master):
        super().__init__(master)


if __name__ == '__main__':
    root = tk.Tk()
    app = Window(root)
    bar = ProgressBar(root)
    bar.pack(fill=tk.BOTH)
    pages = PagesRead(root)
    pages.pack()
    entry = EntryBox(root)
    entry.pack()
    submit = Submit(root)
    submit.pack()
    clear = Clear(root)
    clear.pack()
    app.mainloop()

Upvotes: 0

Views: 194

Answers (2)

figbeam
figbeam

Reputation: 7176

Even if you have created a new class for the Entry, it still retains the Entry methods. So you can simply call the delete method:

clear = Clear(root)
clear.config(command=lambda:entry.delete(0, 'end'))
clear.pack()

Depending on your taste you could instead create a clear() method for the EntryBox class and call it without parameters:

class EntryBox(Entry):
    def __init__(self, master):
        super().__init__(master)

    def clear(self):
        self.delete(0, 'end') 

and then command the Button to this method:

clear = Clear(root)
clear.config(command=entry.clear)
clear.pack()

Upvotes: 0

Times
Times

Reputation: 29

class Clear(Button):
    def __init__(self, master):
        super().__init__(master)
        self.configure(
        command=clear_callback
        text='Clear',
        background='black',
        foreground='light green',
        highlightthickness=0,
        border=0
        )

Then you could do something like this:

def clear_callback(self, event=None):
    self.entry.delete(0, "end")

You can also bind events, there's some great information on this page

Explicit bindings pass an additional argument in the form of a tkinter event object, so sometimes you might need to use a kwarg, or a lambda expression to pass the extra argument

app.bind(
    "<Return>",
    lambda: clear_callback(event)
)

Hope this was helpful. Cheers.

Upvotes: 1

Related Questions