使用python3 Mountain Lion在tkinter中制作工具栏/菜单

时间:2012-10-08 21:52:58

标签: python tkinter

在修补tkinter后,我似乎无法让我的窗户看起来 我多么希望它看起来。但总的来说,我不确定文件编辑视图是什么 布局是指。这是工具栏还是菜单?

到目前为止,我的gui看起来比我想要的少得多。我应该放弃吗? tkinter一起吗?

有没有人有一个代码剪切,提供一般的osx布局?那将是一个很大的帮助。 也许我只是没有从概念上理解gui编程方面。

感谢

我想将菜单添加到以下代码

from tkinter import *
from tkinter import ttk


def undef(*args):
    pass
def undef2(*args):
    pass

root = Tk()


root.title("KDM Checker Beta ")

mainframe = ttk.Frame(root, padding="5 5 5 5")
mainframe.grid(column=12, row=12, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)






countryvar = StringVar()
country = ttk.Combobox(mainframe, textvariable=countryvar)
country['values'] = ('dolby', 'sony', 'doremi')
country.grid(column=1, row = 1)


DATE = StringVar()
VENUE = StringVar()
UUID = StringVar()
SERVER_SERIAL = StringVar()

DATE_entry = ttk.Entry(mainframe, width=8, textvariable=DATE)
DATE_entry.grid(column=3, row=4, sticky=(W, E))

VENUE_entry = ttk.Entry(mainframe, width=8, textvariable=VENUE)
VENUE_entry.grid(column=3, row=8, sticky=(W, E))

UUID_entry = ttk.Entry(mainframe, width=8, textvariable=UUID)
UUID_entry.grid(column=3, row=16, sticky=(W, E))



state = StringVar()
mount = ttk.Radiobutton(mainframe, text='dolby', variable=state, value='dolby')






ttk.Label(mainframe, textvariable=DATE).grid(column=1, row=4, sticky=(W, E))
ttk.Label(mainframe, textvariable=VENUE).grid(column=1, row=8, sticky=(W, E))
ttk.Label(mainframe, textvariable=UUID).grid(column=1, row=16, sticky=(W, E))


ttk.Label(mainframe, text="KDM Window").grid(column=1, row=4, sticky=E)
ttk.Label(mainframe, text="Venue").grid(column=1, row=8, sticky=E)
ttk.Label(mainframe, text="UUID").grid(column=1, row=16, sticky=E)

for child in mainframe.winfo_children(): child.grid_configure(padx=3, pady=9)

DATE_entry.focus()

root.bind('<Return>', undef)

root.mainloop()

1 个答案:

答案 0 :(得分:5)

要创建菜单栏,您需要创建Menu类的实例,然后将其用作主应用程序的menubar属性的值。从那里,您可以创建其他菜单并使用add_cascade将它们附加到菜单栏。

例如:

import tkinter as tk
import sys

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._create_menubar()
        self.text = tk.Text()
        self.text.pack(side="top", fill="both", expand=True)

    def _create_menubar(self):
        # create a menu for the menubar and associate it
        # with the window
        self.menubar = tk.Menu()
        self.configure(menu=self.menubar)

        # create a File menu and add it to the menubar
        file_menu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Quit", command=self.on_quit)

        # create a Edit menu and add it to the menubar
        edit_menu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="Edit", menu=edit_menu)
        edit_menu.add_command(label="Cut", underline=2, command=self.on_cut)
        edit_menu.add_command(label="Copy", underline=0, command=self.on_copy)
        edit_menu.add_command(label="Paste", underline=0, command=self.on_paste)

        # create a View menu and add it to the menubar
        view_menu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="View", menu=view_menu)
        view_menu.add_cascade(label="Whatever", command=self.on_whatever)

    def log(self, s):
        self.text.insert("end", s + "\n")
        self.text.see("end")

    # not good programming style, but I'm trying to keep 
    # the example short
    def on_cut(self): self.log("cut...")
    def on_copy(self): self.log("copy...")
    def on_paste(self): self.log("paste...")
    def on_quit(self): sys.exit(0)
    def on_whatever(self): self.log("whatever...")

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