2017-08-30 165 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