如何使用Tkinter创建基于面向对象编程的菜单栏

huangapple go评论57阅读模式
英文:

How to create OOP based MenuBar with Tkinter

问题

如何使用Tkinter创建基于面向对象的菜单栏

我正在尝试使用Tkinter制作一个GUI程序,需要从菜单栏中访问许多函数。但是我找不到一种有效地将这些函数打包到每个菜单中的方法。

  • 我如何使用面向对象的方法来创建一个菜单栏,其中每个菜单都有许多函数?为菜单栏创建一个单独的类是一个好主意吗?如果是的话,如何实现它?
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)
        #
        # 我需要将许多函数添加到各自的菜单中

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

如何将我的菜单栏类添加进去

# 在这里放置了许多函数
class SomeClass:
    def __init__(self):
        # 做一些事情
        pass
    
    def open_file(self):
        pass
    
    def save_file(self):
        pass
    
    def redo(self):
        pass
    
    def exit(self):
        pass
    
    # 等等......

这是您要求的翻译。如果需要更多帮助,请随时提问。

英文:

How to create OOP based MenuBar with Tkinter

I am trying to make a GUI program using Tkinter which requires a lot of functions to be accessed from the menubar. but i cant find a way to pack those functions efficiently to each menu.

  • how do i approach to create a menubar using object oriented approach with each menu having a lot of functions? Is a seperate class for menubar good idea? if yes, how do i implement it
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.....

答案1

得分: 1

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

这个示例部分改编自你的代码,部分参考了这个页面

# 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()

注意:上述代码中可能有一些HTML转义字符(如'"),这些字符可能需要在实际代码中进行适当的处理。

英文:

You can subclass the tk.Menu widget class and use it as the MenuBar, then you would simply create each of the menus just like you are already doing and connect their callback commands in the same way but using the MenuBar class.

This example is partially adapted from your code and partially from 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()

huangapple
  • 本文由 发表于 2023年6月4日 22:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76400974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定