python - Tkinter Gui 中的类菜单

标签 python user-interface class menu tkinter

我正在开发一个 Gui,我想知道是否可以在我的脚本中将窗口的菜单属性设为一个单独的类,以获得更清晰、更易于增强的代码。

我的代码目前是:

class Application(Frame):
   """ main window application """
   def __init__(self, boss = None):
   (...)
   self.menu = Menu(self)
   self.master.config(menu = self.menu)

   self.select = Menu(self.menu)
   self.menu.add_cascade(label = 'Select', menu = self.select)
   self.select.add_command(label = 'Select all', command = self.select_all)
   ...

我更喜欢这样的东西:

class MenuBar:

    # all the content of the menu here

class Application(Frame):
   (...)
   self.menu = MenuBar(self) ?

研究成果,

最佳答案

是的,这是可能的:

import tkinter as tk
# import Tkinter as tk  # if using python 2
import sys

class MenuBar(tk.Menu):
    def __init__(self, parent):
        tk.Menu.__init__(self, parent)

        fileMenu = tk.Menu(self, tearoff=False)
        self.add_cascade(label="File",underline=0, menu=fileMenu)
        fileMenu.add_command(label="Exit", underline=1, command=self.quit)

    def quit(self):
        sys.exit(0)

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        menubar = MenuBar(self)
        self.config(menu=menubar)

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

关于python - Tkinter Gui 中的类菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3520494/

相关文章:

python - Scrapy 蜘蛛在并行进程中的多处理

python - Sublime Editor 插件记住变量

Python ctypes如何读取C代码修改的字符串

java - 我如何将我的 Retrofit 数据从 MainActivity 发送到我的 Fragment?

c++ - 全局变量的最佳实现

python - 应用gensim LDA主题建模后,如何获取每个主题概率最高的文档并将其保存在csv文件中?

jquery - 动态设置 ui datepicker max/minDate

jQuery BubblePopup 不显示在模式对话框上

java - 扩展类不能继承方法吗?

ruby - 使用 Integer 类和多种方法将 Integer 转换为 Octal