How to create OOP based MenuBar with Tkinter

我正在try 使用Tkinter制作一个图形用户界面程序,它需要从菜单栏访问很多功能.但我找不到一种方法来有效地将这些功能打包到每个菜单中.

  • 如何使用面向对象的方法创建一个菜单栏,每个菜单都有很多功能?菜单吧分开上一节课好吗?如果是,我应该如何实施?
import tkinter as tk
class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('900x600')
        self.resizable(0, 0)
        self.InitContents()

    def InitContents(self):
        container = tk.Frame(self)
        container.pack(fill=tk.BOTH,expand=True)
        root_menu=tk.Menu(self)
        self.config(menu=root_menu)
        
        fileMenu = tk.Menu(root_menu)
        editMenu = tk.Menu(root_menu)
        viewMenu = tk.Menu(root_menu)
        settingMenu = tk.Menu(root_menu)
        root_menu.add_cascade(label='File',menu=fileMenu)
        root_menu.add_cascade(label='Edit',menu=editMenu)
        root_menu.add_cascade(label='View',menu=viewMenu)
        root_menu.add_cascade(label='Settings',menu=settingMenu)
        #
        #i need to add a lot of functions to respective Menus

if __name__ == "__main__":
    root = MainWindow()
    root.mainloop()

how do i put my class for menubar

class SomeClass:
    def __init__(self):
        #does something
        pass
    def open_file():
        pass
    def save_file():
        pass
    def redo():
        pass
    def exit():
        pass
    #etc.....

推荐答案

您可以子化tk.Menu窗口小部件类并将其用作MenuBar,然后您只需创建每个菜单,就像您已经做的那样,并以相同的方式连接它们的回调命令,但使用MenuBar类.

此示例部分改编自您的代码,部分改编自this page

# which are needed
from tkinter import *
from tkinter.ttk import *
import tkinter as tk

class MenuBar(tk.Menu):
    def __init__(self, root):
        super().__init__(root)
        self.fileMenu = tk.Menu(self)
        self.editMenu = tk.Menu(self)
        self.viewMenu = tk.Menu(self)
        self.settingMenu = tk.Menu(self)
        self.fileMenu.add_command(label ='New File', command = self.new_file)
        self.fileMenu.add_command(label ='Open...', command = self.open_file)
        self.fileMenu.add_command(label ='Save', command = self.save_file)
        self.fileMenu.add_separator()
        self.fileMenu.add_command(label ='Exit', command = root.destroy)
        self.editMenu.add_command(label ='Cut', command = None)
        self.editMenu.add_command(label ='Copy', command = None)
        self.editMenu.add_command(label ='Paste', command = None)
        self.editMenu.add_command(label ='Select All', command = None)
        self.editMenu.add_separator()
        self.editMenu.add_command(label ='Find...', command = None)
        self.editMenu.add_command(label ='Find again', command = None)
        self.viewMenu.add_command(label ='Tk Help', command = None)
        self.viewMenu.add_command(label ='Demo', command = None)
        self.viewMenu.add_separator()
        self.viewMenu.add_command(label ='About Tk', command = None)
        self.add_cascade(label='File',menu=self.fileMenu)
        self.add_cascade(label='Edit',menu=self.editMenu)
        self.add_cascade(label='View',menu=self.viewMenu)
        self.add_cascade(label='Settings',menu=self.settingMenu)

    def open_file(self):
        ...

    def save_file(self):
        ...

    def redo(self):
        ...

    def new_file(self):
        ...


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('900x600')
        self.resizable(0, 0)
        container = tk.Frame(self)
        container.pack(fill=tk.BOTH,expand=True)
        root_menu=MenuBar(self)
        self.config(menu=root_menu)

if __name__ == "__main__":
    root = MainWindow()
    root.mainloop()

Python相关问答推荐

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

覆盖Django rest响应,仅返回PK

在matplotlib动画gif中更改配色方案

对Numpy函数进行载体化

非常奇怪:tzLocal.get_Localzone()基于python3别名的不同输出?

不理解Value错误:在Python中使用迭代对象设置时必须具有相等的len键和值

运行终端命令时出现问题:pip start anonymous"

如何在给定的条件下使numpy数组的计算速度最快?

PyQt5,如何使每个对象的 colored颜色 不同?'

转换为浮点,pandas字符串列,混合千和十进制分隔符

如何并行化/加速并行numba代码?

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

判断solve_ivp中的事件

Python全局变量递归得到不同的结果

为什么常规操作不以其就地对应操作为基础?

Python pint将1/华氏度转换为1/摄氏度°°

无法在Spyder上的Pandas中将本地CSV转换为数据帧

为什么Visual Studio Code说我的代码在使用Pandas concat函数后无法访问?

与同步和异步客户端兼容的Python函数

Django更新视图未更新