简体   繁体   中英

How can I close the tkinter button window after use?

from tkinter import *

master = Tk()

def managerMode():
    print ("Connecting to Manager Mode... please wait")

def employeeMode():
    print ("Connecting to Employee Mode... please wait")

b = Button(master, text="Manager Mode", command = managerMode)
b.pack()
c = Button(master,text="Employee Mode", command=employeeMode)
c.pack()

mainloop()

This is my code, and I am planning to close window that contains the types of mode when the user picks one of those options, "Manager Mode" or "Employee Mode". How can I continue closing the button window?

Use the destroy method.

import tkinter
from tkinter import ttk

class MyApp:
    def __init__(self):
        self.root = tkinter.Tk()

    def startman(self):
        self.root.destroy() # like this
        self.root = tkinter.Tk()
        self.root.title('Manager Mode')

        self.backbutton = ttk.Button(self.root, text='Back', command=self.startmenu)
        self.backbutton.grid(column=0, row=0, sticky='nsew')

        self.root.mainloop()

    def startemp(self):
        self.root.destroy() # or this
        self.root = tkinter.Tk()

        self.root.title('Employee Mode')

        self.backbutton = ttk.Button(self.root, text='Back', command=self.startmenu)
        self.backbutton.grid(column=0, row=0, sticky='nsew')

        self.root.mainloop()

    def startmenu(self):
        self.root.destroy() # or this
        self.root = tkinter.Tk()
        self.root.title('Mode Selection')

        self.manbutton = ttk.Button(self.root, text='Manager Mode', command=self.startman)
        self.empbutton = ttk.Button(self.root, text='Employee Mode', command=self.startemp)

        self.manbutton.grid(column=0, row=0, sticky='nsew')
        self.empbutton.grid(column=0, row=1, sticky='nsew')
        self.root.mainloop()

    def run(self):
        self.startmenu()

MyApp().run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM