English 中文(简体)
• 如何与Tkinter建立设在Mendon Bar的OOP公司
原标题:How to create OOP based MenuBar with Tkinter

www.un.org/Depts/DGACM/index_spanish.htm • 如何以Tkinter

我正试图利用Tkinter实施一个全球倡议方案,该方案需要从菜巴获得许多职能。 但我必须找到一种办法,把这些职能有效地包给每个菜单。

  • 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 category 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 Menu widget category and use it as the MenuBar, 然后,你将仅仅像你已经做的那样,以同样的方式,使用Mendal Bar的班子,制造每一个菜单。

这个例子部分由您的法典调整,部分从

# 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()
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签