[英]how to open a new window from menu widget in tkinter
我正在尝试开发一个简单的 Gui,当单击菜单中的按钮时显示不同的页面,并且我已经能够使其他按钮工作,但菜单给了我错误。 就像其他应用程序一样,从菜单人可以导航应用程序。 当我运行代码并在文件菜单中单击 newtest 时,出现此错误
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/pi/Documents/py script/helium1/test.py", line 34, in <lambda>
command=lambda: controller.show_frame(PageOne))
AttributeError: 'Frame' object has no attribute 'show_frame'
这是代码
import tkinter as tk
from tkinter import *
import datetime
import time
import paho.mqtt.client as mqtt
LARGE_FONT= ("Verdana", 12)
def messageFunction (client, userdata, message):
topic = str(message.topic)
message = str(message.payload.decode("utf-8"))
print(topic + " " + message)
HeliumClient = mqtt.Client("xokaxvwt")
HeliumClient.username_pw_set(username = "xokaxvwt", password="MGlEiIwOHM9-")
HeliumClient.connect("m16.cloudmqtt.com", 15998)
HeliumClient.subscribe("Switch_1")
HeliumClient.subscribe("Switch_2")
HeliumClient.on_message = messageFunction
HeliumClient.loop_start()
class MenuBar(tk.Menu):
def __init__(self, parent, controller):
tk.Menu.__init__(self, controller)
self.controller = controller
fileMenu = tk.Menu(self, tearoff=0)
self.add_cascade(label="File", underline=0, menu=fileMenu)
fileMenu.add_command(label="New Test",
command=lambda: controller.show_frame(PageOne))
fileMenu.add_separator()
fileMenu.add_command(label="Exit")
class Helium(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 = {}
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)
self.controller = controller
self.menubar = MenuBar(self, parent)
self.controller.config(menu=self.menubar)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = tk.Button(self, text="Visit Page 2", command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
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, con):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
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 = Helium()
#creating Date/time
def display_time():
Date = datetime.datetime.now()
Date_Label['text'] = Date
app.after(1000, display_time)
Date_Frame = LabelFrame(app, text= "Date/time")
Date_Frame.place(x=790, y=5, height=35, width=200)
Date_Label = Label(Date_Frame)
Date_Label.grid(column=0, row=0)
app.geometry('1000x450')
app.title("Helium")
app.resizable(False, False)
display_time()
app.mainloop()
您正在将parent
参数传递给MenuBar
,但它需要一个 controller。 您还需要将parent
级传递给超类,而不是controller
。 您需要像这样创建MenuBar
:
class MenuBar(tk.Menu):
def __init__(self, parent, controller):
tk.Menu.__init__(self, parent)
...
class StartPage(tk.Frame):
def __init__(self, parent, controller):
...
self.menubar = MenuBar(self)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.