2

在修补了 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()
4

1 回答 1

6

要创建菜单栏,您需要创建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()
于 2012-10-09T01:20:16.283 回答