我正在嘗試開(kāi)發(fā)一個(gè)簡(jiǎn)單的 Gui,當(dāng)單擊菜單中的按鈕時(shí)顯示不同的頁(yè)面,并且我已經(jīng)能夠使其他按鈕工作,但菜單給我錯(cuò)誤。就像菜單中的其他應(yīng)用程序一樣,人們可以在該應(yīng)用程序中導(dǎo)航。當(dāng)我運(yùn)行代碼并單擊文件菜單中的 newtest 時(shí),出現(xiàn)此錯(cuò)誤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貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
您正在將parent參數(shù)傳遞給MenuBar但它需要一個(gè)控制器。您還需要將 傳遞parent給超類,而不是controller. 你需要這樣創(chuàng)建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)
...
添加回答
舉報(bào)
0/150
提交
取消