在这里,我们走吧。 如果这是你想要的,请接受它作为答案。
import tkinter as tk
class FirstFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Menu")
master.geometry("500x400")
self.but1 = tk.Button(self, text= '1', command=self.opt1)
self.but1.pack()
self.but2 = tk.Button(self, text= '2', command=self.opt2)
self.but2.pack()
self.but3 = tk.Button(self, text= '3', command=self.opt3)
self.but3.pack()
self.but4 = tk.Button(self, text= '4', command=self.opt4)
self.but4.pack()
self.but5 = tk.Button(self, text= '5', command=self.opt5)
self.but5.pack()
self.but6 = tk.Button(self, text= '6', command=self.opt6)
self.but6.pack()
def opt1(self):
self.destroy()
self.app = SecondFrame(self.master)
def opt2(self):
self.destroy()
self.app = ThirdFrame(self.master)
def opt3(self):
self.destroy()
self.app = FourthFrame(self.master)
def opt4(self):
self.destroy()
self.app = FifthFrame(self.master)
def opt5(self):
self.destroy()
self.app = SixthFrame(self.master)
def opt6(self):
self.destroy()
self.app = SeventhFrame(self.master)
class SecondFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Opt1")
master.geometry("400x400")
self.opt1lab = tk.Label(self, text = 'This is option 1')
self.opt1lab.pack()
self.gobackbut1 = tk.Button(self, text = 'Go Back', command = self.goback1)
self.gobackbut1.pack()
def goback1(self):
self.destroy()
self.app = FirstFrame(self.master)
class ThirdFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Opt2")
master.geometry("400x400")
self.opt2lab = tk.Label(self, text = 'This is option 2')
self.opt2lab.pack()
self.gobackbut2 = tk.Button(self, text = 'Go Back', command = self.goback2)
self.gobackbut2.pack()
def goback2(self):
self.destroy()
self.app = FirstFrame(self.master)
class FourthFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Opt3")
master.geometry("400x400")
self.opt3lab = tk.Label(self, text = 'This is option 3')
self.opt3lab.pack()
self.gobackbut3 = tk.Button(self, text = 'Go Back', command = self.goback3)
self.gobackbut3.pack()
def goback3(self):
self.destroy()
self.app = FirstFrame(self.master)
class FifthFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Opt4")
master.geometry("400x400")
self.opt4lab = tk.Label(self, text = 'This is option 4')
self.opt4lab.pack()
self.gobackbut4 = tk.Button(self, text = 'Go Back', command = self.goback4)
self.gobackbut4.pack()
def goback4(self):
self.destroy()
self.app = FirstFrame(self.master)
class SixthFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Opt5")
master.geometry("400x400")
self.opt5lab = tk.Label(self, text = 'This is option 5')
self.opt5lab.pack()
self.gobackbut5 = tk.Button(self, text = 'Go Back', command = self.goback5)
self.gobackbut5.pack()
def goback5(self):
self.destroy()
self.app = FirstFrame(self.master)
class SeventhFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.pack()
master.title("Opt6")
master.geometry("400x400")
self.opt6lab = tk.Label(self, text = 'This is option 6')
self.opt6lab.pack()
self.gobackbut6 = tk.Button(self, text = 'Go Back', command = self.goback6)
self.gobackbut6.pack()
def goback6(self):
self.destroy()
self.app = FirstFrame(self.master)
if __name__=="__main__":
root = tk.Tk()
app=FirstFrame(root)
root.mainloop()
是的,我想在Tkinter做一个菜单。 –
如果我能看到,我会很感激。 –
如何在每个框架中添加退出按钮?我试图使用sys.exit()(导入sys),但没有。 :( –