Tkinter Frame with BackButton to main menu(返回按钮到主菜单)

2024-10-01 11:23:09 发布

您现在位置:Python中文网/ 问答频道 /正文


在这个使用tkintergui的Python程序中,我的“BackButton”有一些问题。主机是一个包含动态内容的框架。
在这两个菜单点(设备配置和SCPI命令)中,我都想实现一个BackButton,它将我带回上一个框架/窗口,在本例中是MainFrame/main菜单。我对SCPIMenu的主框架有点困惑,我在那里输入了“???”。
有什么办法来实现这一点吗?谢谢你的时间。在

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()

Tags: self框架viewaddinitdeflabelcommand
1条回答
网友
1楼 · 发布于 2024-10-01 11:23:09

我建议您创建一个方法,在该方法中创建初始帧并在您的init(self)中调用它。在

这样,当你在你的按钮中调用主菜单命令时,你的画面会重置为原来的设置

我只是重用了你的代码,并在你的方法中重新发布了它,还没有尝试过,但是你应该能够做一些类似的事情

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)

相关问题 更多 >