inbinder
inbinder

Reputation: 722

Making toolbars/Menus in tkinter with python3 Mountain Lion

After tinkering around with tkinter I can't seem to make my window look quite how I want it to look. But overall, I'm not sure what the File Edit View layout is referred to. Is this is a toolbar or a menu?

So far my gui looks much less native osx than I would like. Should I just ditch tkinter all together?

Does anyone have a code snipped that gives the general osx layout? That would be a big help. Maybe I'm just not grasping the gui programming aspect conceptually.

thanks

I want to add menus to the following code

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

Upvotes: 2

Views: 4237

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385820

To create a menubar you need to create an instance of the Menu class, then use it as the value of the menubar attribute of your main application. From there you can create other menus and attach them to the menubar with add_cascade.

For example:

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

Upvotes: 6

Related Questions