如何从 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")


UYOU
浏览 58回答 1
1回答

慕雪6442864

您正在将parent参数传递给MenuBar但它需要一个控制器。您还需要将 传递parent给超类,而不是controller. 你需要这样创建MenuBar:class MenuBar(tk.Menu):&nbsp; &nbsp; def __init__(self, parent, controller):&nbsp; &nbsp; &nbsp; &nbsp; tk.Menu.__init__(self, parent)&nbsp; &nbsp; ...class StartPage(tk.Frame):&nbsp; &nbsp; def __init__(self, parent, controller):&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; self.menubar = MenuBar(self)&nbsp; &nbsp; &nbsp; &nbsp; ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python