python 菜单栏界面切换_如何在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..

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

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

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值