0

I am trying to make a cancel button when pushed would cancel a program.

class PayrollSummary:
    def __init__(self):
        window = Tk()
        window.title("Employee Payroll")
        btCancel = Button(frame1, text = "Cancel", command = self.processCancel)

    def processCancel(self):
        self.destroy()

this is the error message I get:

AttributeError: 'PayrollSummary' object has no attribute 'destroy'

InstantNut
  • 93
  • 1
  • 9

3 Answers3

3

If you want to destroy the tkinter GUI, you must call destroy on the root window. According to the error you received, you are not calling destroy on the root window. You are apparently calling it on some other object.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
2

self refers to the "PayrollSummary" instead of the tkinter dialog/window initialised with Tk().

See How do I close a tkinter window?

0

As you've not inherited Tk in your class PayrollSummary the self.destroy() will not work on an Object alone, it is a method of Tk.

If you want to use destroy the window either you have to inherit Tk

For example:

class PayrollSummary(Tk):
    def __init__(self, *ags, **kw):
        super(PayrollSummary, self).__init__(*ags, **kw)
        self.title("Employee Payroll")
        btCancel = Button(self, text = "Cancel", command = self.processCancel)
        btCancel.pack()

    def processCancel(self):
        self.destroy()

Or if you don't want to inherit Tk

class PayrollSummary:
    def __init__(self):
        self.window = Tk()
        self.window.title("Employee Payroll")
        btCancel = Button(self.window, text = "Cancel", command = self.processCancel)
        btCancel.pack()

    def processCancel(self):
        self.window.destroy()
Saad
  • 3,340
  • 2
  • 10
  • 32