Rimoun
Rimoun

Reputation: 495

How to close a tkinter window when button pressed?

def _error_message(self, message: str):
    self._error_window = tkinter.Toplevel()
    title = tkinter.Label(master = self._error_window,
        text = message, font = DEFAULT)
    title.grid(row = 0, padx = 12, pady = 12, sticky = tkinter.EW)

    ok_error_button = tkinter.Button(master=self._error_window, text='OK',
        font= DEFAULT, command = self._error_window.destroy())
    ok_error_button.grid(row = 1, padx = 10, pady = 10)

    self._error_window.grab_set()

I want to make pressing the "ok" button on a window close a window how do I do that?

Upvotes: 0

Views: 1085

Answers (2)

evamvid
evamvid

Reputation: 861

Change the name of the button's command to self.destroy Add this before you initialize the button:

def destroy(self)
    self._error_window.destroy()

Good Luck!

Upvotes: 1

Kirill
Kirill

Reputation: 1538

def quit(self):
    self._error_window.destroy()

Upvotes: 0

Related Questions