2012-10-11 74 views
0


我在Tkinter GUI的這個Python程序中遇到了一些與我的「BackButton」有關的問題。 MainFrame是一個包含動態內容的框架。
在兩個MenuPoints(設備配置和SCPI命令)中,我想實現一個BackButton,它將我帶回到前一個Frame/Window中,在這種情況下是MainFrame/MainMenu。我與SCPIMenu的主框架有點混淆,我在這裏輸入'???'。
有沒有任何想法如何實現這一點?謝謝你的時間。帶BackButton的Tkinter框架到MainMenu

class View(Tk): 
    def __init__(self): 
     Tk.__init__(self) 
     self.title('Device Configurator') 
     self.geometry('400x400') 
     self.resizable(0,0) 

     self.MainFrame = Frame(self, bd = '2', relief = RIDGE) 
     self.MainFrame.pack(expand = True, fill="both", padx = 5, pady = 20) 

     menubar = Menu(self) 
     filemenu = Menu(menubar, tearoff=0) 
     filemenu.add_command(label='Configure Devices', command = None) 
     filemenu.add_command(label='Exit', command=self.quit) 
     menubar.add_cascade(label='File', menu=filemenu) 
     infomenu = Menu(menubar, tearoff = 0) 
     infomenu.add_command(label='About', command = None) 
     menubar.add_cascade(label='Info', menu = infomenu) 
     self.config(menu = menubar) 

    def mainMenu(self): 
     configButton = Button(self.MainFrame, text = 'Device Configuration') 
     configButton.place(x=200, y=150,anchor=CENTER) 

     SCPIButton = Button(self.MainFrame, text = 'SCPI Command', command = self.SCPIMenu) 
     SCPIButton.place(x=200, y=200,anchor=CENTER) 

    def SCPIMenu(self): 
     self.SCPIFrame = Frame(???, bd = '2', relief = RIDGE) 
     self.SCPIFrame.pack(expand = True, fill="both", padx = 5, pady = 20) 
     BackButton = Button(???, text = 'Back', command = self.mainMenu) 
     BackButton.place(x=350, y=330, anchor=CENTER) 






##The controller understands both the model and the view. 
class Controller(object): 
    def __init__(self): 
     self.view = View() 
     self.view.mainMenu() 
     self.view.mainloop() 


c = Controller() 
+0

你可能會看看這個問題隱藏部件(http://stackoverflow.com/q/3819354)。您可以在將屏幕視爲繪製屏幕的方法(並最終刪除之前的內容)或「框架」之間作出選擇,即在開始時填充,然後顯示或隱藏。後者得到了我的偏好(因爲它對我來說更多的是GUI /狀態,而不是程序方式,並且可能會限制依賴關係)。 – FabienAndre

+0

當你最初按下「SCPI命令」按鈕時,你想要發生什麼?您是否希望將窗口的全部內容替換爲SCPIMenu框架,還是希望在單獨的彈出窗口或其他位置顯示該框架? –

+0

我想用SCPIMenu框架替換窗口的全部內容,當我點擊後退按鈕時,我想回到主菜單,所以MainFrame。 – eljobso

回答

0

我建議你創造一個在創建初始框架,並把它在你初始化(個體經營)的方法。

這樣一來,當你調用YOUE MAINMENU命令在你的按鈕,你的框架將重置爲初始設置

我只是重用你的代碼,repasted它在你的方法,還沒有嘗試過,但你shoudl能夠做類似的事情

def mainMenu(self): 

    self.MainFrame = Frame(self, bd = '2', relief = RIDGE) 
    self.MainFrame.pack(expand = True, fill="both", padx = 5, pady = 20 

    configButton = Button(self.MainFrame, text = 'Device Configuration') 
    configButton.place(x=200, y=150,anchor=CENTER) 

    SCPIButton = Button(self.MainFrame, text = 'SCPI Command', command = self.SCPIMenu) 
    SCPIButton.place(x=200, y=200,anchor=CENTER) 
    self.MainFrame = Frame(self, bd = '2', relief = RIDGE) 
    self.MainFrame.pack(expand = True, fill="both", padx = 5, pady = 20)