0

我正在制作一个包含多个页面的 tkinter 应用程序。我使用的代码的简单版本如下所示。我将菜单添加到应用程序的根目录,并提供了一个文件选项以及“重新加载默认值”和“退出”子选项。

当我运行这段代码时,我确实得到了菜单,但不是在 GUI 上,而是在 Mac 的主菜单栏上。而且菜单没有反应。我不确定我做错了什么!

import tkinter as tk
from tkinter import ttk
from tkinter import RAISED


LARGEFONT =("Verdana", 35)

class tkinterApp(tk.Tk):

    # __init__ function for class tkinterApp
    def __init__(self, *args, **kwargs):

        # __init__ function for class Tk
        tk.Tk.__init__(self, *args, **kwargs)

        # creating a container
        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)

        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        container2 = tk.Frame(self, relief=RAISED, borderwidth=2)
        container2.pack(side="bottom",fill="both", expand=True)
        container2.grid_rowconfigure(0, weight = 1)
        container2.grid_columnconfigure(0, weight = 1)

        def runProg():
            print("response from runProg")
            #get all entry value from page 2


        runButton = ttk.Button(container2, text="Run", command=runProg)
        runButton.pack(side="left", padx=5, pady=5)

        ######################################################
        ## Menu items
        progMenu = tk.Menu(self,tearoff=0)
        self.config(menu=progMenu)

        #create File menu
        def reload_command():
            pass

        fileMenu = tk.Menu(progMenu)
        progMenu.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Reload Defaults", command=reload_command)
        fileMenu.add_command(label="Exit", command=reload_command)
        ######################################################

        # initializing frames to an empty array
        self.frames = {}
        frame = StartPage(container, self)

        self.frames[StartPage] = frame

        frame.grid(row = 0, column = 0, sticky ="nsew")

        self.show_frame(StartPage)

    # to display the current frame passed as
    # parameter
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

# first window frame startpage

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # label of frame Layout 2
        label = ttk.Label(self, text ="Startpage", font = LARGEFONT)

        # putting the grid in its place by using
        # grid
        label.grid(row = 0, column = 2, padx = 10, pady = 10)


app = tkinterApp()
app.mainloop()

任何帮助将不胜感激!

在此处输入图像描述

4

1 回答 1

0

您可能希望将菜单更改为网格顶部的按钮,因为在 Mac 上您会遇到此错误,但在 Windows 上它工作得非常好。 在此处输入图像描述

于 2020-09-05T06:25:52.667 回答