2013-07-12 26 views
2

我要創建一個tkinter GUI應用程序,我知道我希望它看起來。但在玩過tkinter之後,當你按下底部的按鈕時,我發現無法在屏幕之間切換。我知道它什麼都不做,但下面是我想要的簡單佈局,並在「myframe1」和「myframe2」之間切換,就像Apple App Store佈局一樣。這可能嗎?是否有可能在tkinter拉起不同的屏幕在同一位置

from tkinter import * 

tk = Tk() 
tk.geometry("300x300") 

myframe1 = Frame(tk,background="green",width=300,height=275) 
myframe1.pack() 

myframe2 = Frame(tk,background="cyan",width=300,height=275) 
myframe2.pack() 

btnframe = Frame(tk) 

btn1 = Button(btnframe,text="screen1",width=9) 
btn1.pack(side=LEFT) 

btn2 = Button(btnframe,text="screen2",width=9) 
btn2.pack(side=LEFT) 

btn3 = Button(btnframe,text="screen3",width=9) 
btn3.pack(side=LEFT) 

btn4 = Button(btnframe,text="screen4",width=9) 
btn4.pack(side=LEFT) 

myframe1.pack() 
btnframe.pack() 

tk.mainloop() 

回答

1

東西給你上手:

def toggle(fshow,fhide): 
    fhide.pack_forget() 
    fshow.pack() 


btn1 = Button(btnframe,text="screen1", command=lambda:toggle(myframe1,myframe2),width=9) 
btn1.pack(side=LEFT) 

btn2 = Button(btnframe,text="screen2",command=lambda:toggle(myframe2,myframe1),width=9) 
btn2.pack(side=LEFT) 
+0

你也可以使用的地方,而不是包裝,因爲地方讓你堆疊的小部件在彼此之上。然後,您可以「提起()」您想要看到的那個。 –

+0

當我把切換功能放在我的代碼中進行測試時,按鈕會在開關或按鈕之後進入頂部。即使我在切換函數中說btnframe.pack(),它仍然位於頂部 – user2155059

1

您是否正在尋找類似標籤的小部件?你可以使用forgetpack的建議here

這裏是我在我的代碼使用作品類:

class MultiPanel(): 
    """We want to setup a pseudo tabbed widget with three treeviews. One showing the disk, one the pile and 
    the third the search results. All three treeviews should be hooked up to exactly the same event handlers 
    but only one of them should be visible at any time. 
    Based off http://code.activestate.com/recipes/188537/ 
    """ 
    def __init__(self, parent): 
    #This is the frame that we display 
    self.fr = tki.Frame(parent, bg='black') 
    self.fr.pack(side='top', expand=True, fill='both') 
    self.widget_list = [] 
    self.active_widget = None #Is an integer 

    def __call__(self): 
    """This returns a reference to the frame, which can be used as a parent for the widgets you push in.""" 
    return self.fr 

    def add_widget(self, wd): 
    if wd not in self.widget_list: 
     self.widget_list.append(wd) 
    if self.active_widget is None: 
     self.set_active_widget(0) 
    return len(self.widget_list) - 1 #Return the index of this widget 

    def set_active_widget(self, wdn): 
    if wdn >= len(self.widget_list) or wdn < 0: 
     logger.error('Widget index out of range') 
     return 
    if self.widget_list[wdn] == self.active_widget: return 
    if self.active_widget is not None: self.active_widget.forget() 
    self.widget_list[wdn].pack(fill='both', expand=True) 
    self.active_widget = self.widget_list[wdn] 
相關問題