TKinter Python Window close down after hitting button

Question:

I have quick question about tkinter Python. I created Button added command to execute some function, but how to make, that after clicking button and function execution window would close.

def Top(self):
    self.string1=StringVar()  ###
    self.string2=StringVar()  
    self.string3=StringVar()  ###
    self.Top=Toplevel()
    self.Top.title("Database Preferences")
    L1=Label(self.Top, text="Host")
    L1.pack(side=TOP)
    self.entry1=Entry(self.Top, textvariable=self.string1)
    self.entry1.pack(side=TOP, padx=10, pady=12)
    L2=Label(self.Top, text="User")
    L2.pack(side=TOP)
    self.entry2=Entry(self.Top, textvariable=self.string2)
    self.entry2.pack(side=TOP, padx=10, pady=12)
    L3=Label(self.Top, text="Pass")
    L3.pack(side=TOP)
    self.entry3=Entry(self.Top, textvariable=self.string3)
    self.entry3.pack(side=TOP, padx=10, pady=12)
    Button(self.Top, text="ok", command=self.createini).pack(side=BOTTOM, padx=10, pady=10)



def createini(self):
    cfgfile = open("conf.ini",'w')
    self.Config = ConfigParser.ConfigParser()
    self.Config.add_section('Database')
    self.Config.set('Database',"host", self.string1.get())
    self.Config.set('Database',"user", self.string2.get())
    self.Config.set('Database',"pass", self.string3.get())
    self.Config.write(cfgfile)
    cfgfile.close()
Asked By: Thomas

||

Answers:

To destroy the main window you would call the destroy method of that window object. In your case it would be self.Top.destroy() if you want to destroy self.Top.

Answered By: Bryan Oakley

you can use destroy method which will close your tkinter window

Answered By: user17336875

If you want to close the window, you can use the .destroy() method.

Answered By: SShield
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.