Python Tkinter菜单栏未显示

时间:2018-09-30 22:06:22

标签: python-3.x tkinter

我有以下未显示菜单的代码。一直有效,直到我将菜单移到全班为止。我基于this question添加了self.configure(menu=self.menu),但仍然无法正常工作。打印语句显示菜单构建代码正在运行。

我可以正确引用父母吗?

如果我错误地从父级而不是从控制器引用了事件处理程序,则错误表明这是一个Tk()对象。

我在Ubuntu上使用python3.6和tkinter8.6。

import tkinter as tk
class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.main = MainWindow(self)
        self.menu=MainMenu(self, self.main)
        self.configure(menu=self.menu)
        self.minsize(300,300)
        self.geometry("500x500")
        self.protocol('WM_DELETE_WINDOW', self.main.on_exit)
        self.main.pack(fill="both", expand=True)

class MainWindow(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
    def on_exit(self):
        print("exiting...")
        self.parent.destroy()
    def on_choose_port(self):
        print("on_choose_port selected")

class MainMenu(tk.Menu):
    def __init__(self, parent, controller):
        tk.Menu.__init__(self, master=None)
        self.master = parent
        self.controller = controller
        print("building menu bar")
        self.menubar = tk.Menu(self.master)
        self.menubar.add_cascade(label="File", menu=self.file_menu(self.menubar))
        self.menubar.add_cascade(label="Settings", menu=self.settings_menu(self.menubar))

    def file_menu(self, parent):
        print("building file menu...")
        file_menu = tk.Menu(parent)
        file_menu.add_command(label="Exit", command=self.controller.on_exit)
        return file_menu

    def settings_menu(self, parent):
        print("building settings menu...")
        settings_menu = tk.Menu(parent)
        settings_menu.add_command(label="Port", command=self.controller.on_choose_port)
        return settings_menu

if __name__ == "__main__":
    App().mainloop()

1 个答案:

答案 0 :(得分:2)

问题是structure(list(x = c(-87.001233, -87.416633, -86.999683, -86.58395, -86.998233, -86.998233, -86.998233, -86.998233, -87.416633, -87.416633, -87.416633, -87.416633, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -86.58395, -86.58395, -86.58395, -86.58395, -86.999683, -86.999683, -86.999683, -86.999683, -86.916517, -86.965333, -87.233917, -86.721362, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.766867, -86.766867, -86.766867, -86.766867, -87.233917, -87.233917, -87.233917, -87.233917, -86.965333, -86.965333, -86.965333, -86.965333, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.37455, -86.37455, -86.37455, -86.37455, -86.37455, -86.37545, -86.967667, -86.766717, -86.72265, -87.2327, -86.37545, -86.37545, -86.37545, -86.37545, -86.37545, -86.37545, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.72265, -86.967667, -86.967667, -86.967667, -86.967667, -86.967667, -86.967667, -87.2327, -87.2327, -87.2327, -87.2327, -87.2327, -87.2327, -86.766717, -86.766717, -86.766717, -86.766717, -86.766717, -86.766717, -87.001817, -87.416083, -86.582933, -86.9173, -86.9173, -86.9173, -86.9173, -86.9173, -86.9173, -86.9173, -86.9173, -86.9173, -87.001817, -87.001817, -87.001817, -87.001817, -87.001817, -87.001817, -86.582933, -86.582933, -86.582933, -86.582933, -86.582933, -86.582933, -86.99865, -86.99865, -86.99865, -86.99865, -86.99865, -86.99865, -86.99865, -86.99865, -86.99865, -87.416083, -87.416083, -87.416083, -87.416083, -87.416083, -87.416083, -87.00025, -87.00025, -87.00025, -87.00025, -87.00025, -87.00025, -87.00025, -83.9164, -82.915883, -82.452167, -83.9164, -83.9164, -83.9164, -83.9164, -82.417183, -82.417183, -82.417183, -82.417183, -82.417183, -82.417183, -82.417183, -82.417183, -82.417183, -82.915883, -82.915883, -82.915883, -82.915883, -82.452167, -82.452167, -82.452167, -82.452167, -82.983533, -82.983533, -82.983533, -82.983533, -82.983533, -82.983533, -82.983533, -82.983533, -82.783667, -82.340983, -82.783667, -82.783667, -82.783667, -82.783667, -82.06085, -82.06085, -82.06085, -82.06085), y = c(42.731083, 42.732717, 43.132683, 42.733483, 42.380783, 42.380783, 42.380783, 42.380783, 42.732717, 42.732717, 42.732717, 42.732717, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.731083, 42.733483, 42.733483, 42.733483, 42.733483, 43.132683, 43.132683, 43.132683, 43.132683, 43.599517, 44.759817, 44.14175, 44.737115, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 43.599517, 44.09025, 44.09025, 44.09025, 44.09025, 44.14175, 44.14175, 44.14175, 44.14175, 44.759817, 44.759817, 44.759817, 44.759817, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 44.737115, 45.178767, 45.178767, 45.178767, 45.178767, 45.178767, 45.177783, 44.759917, 44.090067, 44.73645, 44.139717, 45.177783, 45.177783, 45.177783, 45.177783, 45.177783, 45.177783, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.73645, 44.759917, 44.759917, 44.759917, 44.759917, 44.759917, 44.759917, 44.139717, 44.139717, 44.139717, 44.139717, 44.139717, 44.139717, 44.090067, 44.090067, 44.090067, 44.090067, 44.090067, 44.090067, 43.1333, 42.733717, 42.733333, 43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.60065, 43.1333, 43.1333, 43.1333, 43.1333, 43.1333, 43.1333, 42.733333, 42.733333, 42.733333, 42.733333, 42.733333, 42.733333, 42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 42.733083, 42.733717, 42.733717, 42.733717, 42.733717, 42.733717, 42.733717, 42.383767, 42.383767, 42.383767, 42.383767, 42.383767, 42.383767, 42.383767, 45.750033, 45.449183, 45.27775, 45.750033, 45.750033, 45.750033, 45.750033, 45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 45.516667, 45.449183, 45.449183, 45.449183, 45.449183, 45.27775, 45.27775, 45.27775, 45.27775, 45.1371, 45.1371, 45.1371, 45.1371, 45.1371, 45.1371, 45.1371, 45.1371, 44.761967, 44.453917, 44.761967, 44.761967, 44.761967, 44.761967, 44.740467, 44.740467, 44.740467, 44.740467 ), z = c(10.81, 10.84, 10.84, 0, 10.81, 10.89, 10.76, 10.86, 10.89, 10.83, 10.88, 10.8, 10.79, 10.79, 10.8, 10.82, 10.78, 10.78, 10.82, 10.86, 10.8, 10.83, 10.87, 10.87, 10.88, 10.88, 10.83, 10.8, 10.81, 10.81, 10.8, 10.83, 0, 0.02, 10.8, 10.8, 10.78, 10.79, 10.81, 10.8, 10.81, 10.82, 10.79, 10.81, 10.88, 10.89, 10.9, 10.88, 10.77, 10.78, 10.77, 10.78, 10.84, 10.83, 10.83, 10.84, 10.75, 10.77, 10.76, 10.77, 10.78, 10.79, 10.76, 10.77, 10.79, 10.78, 10.78, 10.93, 10.94, 10.91, 10.95, 10.94, 10.73, 10.79, 10.9, 0.01, 0, 10.8, 10.9, 10.83, 10.71, 10.75, 10.75, 10.89, 10.89, 10.89, 10.77, 10.66, 10.68, 10.67, 10.6, 10.71, 10.68, 10.67, 10.81, 10.73, 10.74, 10.73, 10.69, 10.69, 10.73, 10.68, 10.67, 10.77, 10.81, 10.8, 11.09, 10.94, 10.72, 10.78, 10.83, 10.82, 10.72, 10.8, 10.8, 10.73, 10.7, 10.68, 10.75, 10.78, 10.75, 10.74, 10.77, 10.75, 10.68, 10.69, 10.67, 10.76, 10.8, 10.74, 11.06, 10.89, 10.73, 10.7, 10.72, 10.71, 10.78, 10.8, 10.78, 10.7, 10.63, 10.6, 10.72, 10.7, 10.71, 10.83, 10.82, 10.75, 10.72, 10.77, 10.73, 0, 10.84, 10.84, 10.73, 10.6, 10.74, 10.77, 6, 6.33, 0, 6, 6, 6.06, 6, 6.19, 6.24, 6.21, 6.21, 6.21, 6.21, 6.21, 6.22, 6.21, 6.35, 6.34, 6.33, 6.33, 6.37, 6.36, 6.36, 6.35, 6.05, 6.01, 6.04, 6.03, 6.04, 6.04, 6.07, 6.05, 6.02, 0, 6.08, 6.09, 6.13, 6.08, 6.18, 6.18, 6.11, 6.14)), .Names = c("x", "y", "z"), class = "data.frame", row.names = c(NA, -200L)) 继承自MainMenu,但还会创建一个内部菜单,其中包含所有其他菜单项。您将Menu附加到了根窗口,但是您已经将所有层叠菜单附加到了隐藏的MainMenu菜单上。

换句话说,您需要将级联菜单添加到self.menubar而不是添加到self,并且根本不需要创建self.menubar

self.menubar