我正在尝试开发一个简单的 Gui,当单击菜单中的按钮时显示不同的页面,并且我已经能够使其他按钮工作,但菜单给我错误。就像菜单中的其他应用程序一样,人们可以在该应用程序中导航。当我运行代码并单击文件菜单中的 newtest 时,出现此错误Exception in Tkinter callbackTraceback (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 tkfrom tkinter import *import datetimeimport timeimport paho.mqtt.client as mqttLARGE_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")
1 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
您正在将parent参数传递给MenuBar但它需要一个控制器。您还需要将 传递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)
...
添加回答
举报
0/150
提交
取消