2017-06-20 52 views
1

我正在学习Tkinter,这个程序是我进入Tkinter之前的最后一个项目的一部分。我想知道这个主菜单如何在Tkinter中显示。从Python基础到Tkinter

我试图寻找类似的东西,但是当我参考了主菜单,我通常有主菜单,文件,编辑,查看,...

我的想法是,当你在1类型 - 6,你的窗口被清除,并出现一些其他信息,并有一个退出按钮和返回按钮。如果我不清楚,请告诉我。英语不是我的母语,所以...

print("    %s %s"%(nombre,apellido)) 
print("    __________________________________ ") 
print("    |   Menú principal   |") 
print("    | 1.Dato breve sobre mi carrera |") 
print("    | 2.Cursos y tareas    |") 
print("    | 3.Grupos de apoyo    |") 
print("    | 4.Calendario Academico   |") 
print("    | 5.Feriados      |") 
print("    | 6.Salir       |") 
print("    |__________________________________|") 
op=int(input("    Elija una opcion:")) 

回答

2

在这里,我们走吧。 如果这是你想要的,请接受它作为答案。

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() 
+0

是的,我想在Tkinter做一个菜单。 –

+0

如果我能看到,我会很感激。 –

+0

如何在每个框架中添加退出按钮?我试图使用sys.exit()(导入sys),但没有。 :( –