2017-08-30 169 views
0

我试图创建一个侧面菜单栏并使用多个标签作为单击按钮。但是当我执行程序时,只有一个标签被点击并且事件显示在主区域中。其他事件不起作用。在侧面菜单栏中使用tkinter在python中切换多个框架

请给我提供一个有用的解决方案。这里是我写的代码:

from Tkinter import * 
    import ttk 
    root = Tk() 
    def MainScreen(self): 
     label4 = Label(mainarea,width=100,height=80,text="Prapti Computer 
     Solutions") 
     label4.pack(expand=True,fill='both') 

    def ClientData(self): 
     label4 = Label(mainarea,width=100,height=80,text="Yo this is client data") 
     label4.pack(expand=True,fill='both') 

    def Report(self): 
     label6 = Label(mainarea,width=100,height=80,text="Report is onnnnn!") 
     label6.pack(expand=True,fill='both') 

    # sidebar 
    sidebar = Frame(root, width=400, bg='white', height=500, borderwidth=2) 
    sidebar.pack(fill='y', side='left', anchor='nw') 

    #submenus 
    label1 = Label(sidebar,width=45,height = 2 , text="HOME", relief=FLAT) 
    label1.bind("<Button-1>",MainScreen) 
    label1.grid(row=0) 

    ttk.Separator(sidebar,orient=HORIZONTAL).grid(row=1, columnspan=5) 

    label2 = Label(sidebar,width=45,height = 2 , text="CLIENT", relief=FLAT) 
    label2.bind("<Button-1>",ClientData) 
    label2.grid(row=2) 

    ttk.Separator(sidebar,orient=HORIZONTAL).grid(row=3, columnspan=5) 

    label3 = Label(sidebar,width=45,height = 2 , text="REPORT", relief=FLAT) 
    label3.bind("<Button-1>",Report) 
    label3.grid(row=4) 

    # main content area 
    mainarea = Frame(root, bg='#CCC', width=500, height=500) 
    mainarea.pack(expand=True, fill='both', side='right') 
    root.attributes('-alpha', 0.98) 

    root.mainloop() 

谢谢。

回答

0

你继续收拾东西,但你永远不会删除它们。在切换到新页面之前,您需要删除当前页面。

例如:

current_page = None 

def MainScreen(self): 
    global current_page 
    if current_page is not None: 
     current_page.pack_forget() 
    label4 = Label(mainarea,width=100,height=80,text="Prapti Computer Solutions") 
    label4.pack(expand=True,fill='both') 
    current_page = label4