0


I have some problems with my "BackButton" in this Python program with Tkinter GUI. The MainFrame is a Frame with dynamic content.
In both MenuPoints (Device Configuration and SCPI Command) I would like to implement a BackButton which brings me back into the previous Frame/Window, in this case the MainFrame/MainMenu. I'm a little bit confused with the master Frame of the SCPIMenu, where I entered the '???'.
Are there any ideas how to implement this? Thanks for your time.

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()
eljobso
  • 352
  • 2
  • 6
  • 20
  • You may have a look at this question on hiding widgets (http://stackoverflow.com/q/3819354). You have here to make a choice between seeing your screens as method that draw them (and eventually remove what was there before), or as `Frame`s, that you populate at start, and then show or hide. The later got my preference (as it is for me more GUI / State oriented than the procedural way, and might limit dependencies). – FabienAndre Oct 11 '12 at 10:51
  • when you initially press the "SCPI Command" button, what do you want to have happen? Do you want the whole contents of the window replaced with the SCPIMenu frame, or do you want that frame in a separate popup window, or something else? – Bryan Oakley Oct 11 '12 at 14:34
  • I want the whole contents of the window replaced with the SCPIMenu Frame and when I click on the Back Button I want to go back to the Main Menu, so the MainFrame. – eljobso Oct 12 '12 at 19:19

1 Answers1

0

I would suggest you create a method where you create your initial frame and call it in you init(self).

that way, when you call youe mainMenu command in your button, your frame will reset to its original settings

I just reused your code and repasted it in your method, havent tried it, but you shoudl be able to do something similar

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)
glls
  • 2,325
  • 1
  • 22
  • 39