1

I am trying the following code about python:

import tkinter
from tkinter import *
from tkinter.messagebox import askokcancel

class Quitter(Frame):
    def _init__(self,parent=None):
        Frame.__init__(self,parent)
        self.pack()
        widget=Button(self,text='Quit',command=self.quit)
        widget.pack(side=TOP,expand=YES,fill=BOTH)
    def quit(self):
        ans=askokcancel('Verify exit',"You want to quit?")
        if ans:Frame.quit(self)

if __name__=='__main__':Quitter().mainloop()

When executing it, I get a frame like this:

enter image description here

But where is the Quit button?

Lucy
  • 81
  • 6

1 Answers1

0

As mentioned in the comments, you have a typo in __init__().
Further, you probably want to structure your app as follows: (1) not importing tkinter in the main namespace, (2) keep track of the root/parent in the tk.Frame class, and use root.destroy() i/o quit()

import tkinter as tk
from tkinter.messagebox import askokcancel

class Quitter(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        self.widget = tk.Button(self.parent, text='Quit', command=self.quit)
        self.widget.pack()

    def quit(self):
        ans = askokcancel('Verify exit', "You want to quit?")
        if ans: 
            self.parent.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    Quitter(root)
    root.mainloop()

You will find more info in this thread.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80