如何在Tkinter中通过菜单栏方法更改页面? [英] How to change page through my menu bar methods in tkinter?

查看:935
本文介绍了如何在Tkinter中通过菜单栏方法更改页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想弄清楚的是,如何使菜单栏的按钮"更改页面?

Basically what I'm trying to figure out is, how do I make my menu bar "buttons" change the page?

我已经制作了下拉菜单,以及每个页面的类.问题是,如果我创建一个单独的按钮来更改页面,那么它起作用了,但是当我尝试通过菜单栏进行操作时却没有效果吗?

I've already made the dropdown menu, and the classes with each page. The thing is it works if I create a separate button to change the page, but not when i'm trying to do it through the menu bar?

我找到的每个教程都是通过按钮进行的​​,只有按钮是..

every tutorial I find is through the buttons, and only the buttons..

我正在将Python 3.6与tkinter一起使用.

I am using python 3.6, with tkinter.

import tkinter as tk


class MyApp(tk.Tk):

    def __init__(self, *args, **kwargs, ):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        menu = tk.Menu(container)

        betting = tk.Menu(menu, tearoff=0)
        menu.add_cascade(menu=betting, label="Pages")
        betting.add_command(label="PageOne",
                            command=lambda: controller.show_frame(PageOne))
        betting.add_command(label="PageTwo",
                            command=lambda: controller.show_frame(PageTwo))

        tk.Tk.config(self, menu=menu)

        for F in (Startpage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Startpage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class Startpage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Home")
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="page 1",
                        command=lambda: controller.show_frame(PageOne))
        button1.pack()
        button2 = tk.Button(self, text="Page Two",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()


#   *****   PAGES   *****

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Back to Home")
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(Startpage))
        button1.pack()
        button2 = tk.Button(self, text="Page Two",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page Two")
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(Startpage))
        button1.pack()
        button2 = tk.Button(self, text="Page One",
                        command=lambda: controller.show_frame(PageOne))
        button2.pack()


app = MyApp()
app.geometry("1200x600")
app.mainloop()

现在我知道,我现在还没有解析"controller& parent",但是我已经尝试了很多次,对我来说什么都不起作用...

Now I know, I haven't parsed the "controller & parent", right now, but I've tried and tried, and nothing works for me...

推荐答案

控制器为MyApp.在使controllerselfMyApp的定义内.因此,您需要调用self.show_frame而不是controller.show_frame.

The controller is MyApp. Within the definition of MyApp that makes controller be self. Thus, you need to call self.show_frame rather than controller.show_frame.

betting.add_command(label="PageOne",
                    command=lambda: self.show_frame(PageOne))
betting.add_command(label="PageTwo",
                    command=lambda: self.show_frame(PageTwo))

这篇关于如何在Tkinter中通过菜单栏方法更改页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆