Movatterモバイル変換
[0]ホーム
Fwd: Tkinter App Wanted
johngrayson at home.comjohngrayson at home.com
Fri Apr 13 17:07:36 EDT 2001
--- Inpython-list at y..., Brian & Colleen <greybria at d...> wrote:Is anyone willing to share the source for a small Tkinter app (maybe aneditor or file viewer) that includes a menu. I'm having problems gettinga menu to work inside an encapsulated application.#! /usr/bin/env pythonfrom Tkinter import *class App: frame=Frame(master) frame.pack() self.button=Button(frame,text="QUIT",fg="red",command=frame.quit) self.button.pack(side=LEFT) self.hi_there=Button(frame,text="Hello",command=self.say_hi) self.hi_there.pack(side=LEFT) def say_hi(self): print "Hi there, everyone!"root=Tk()app = App(root)root.mainloop()Somewhere inside the App class, I'd like to include a basic menu(File-Save As, Exit) etc. so that when I create an instance of App, allthe pieces are there.Thanks in advance.Here's a little sampler...from Tkinter import *def new_file(): print "Open new file"def open_file(): print "Open existing file"def stub_action(): print "Menu select"def makeCommandMenu(): CmdBtn = Menubutton(mBar, text='Button Commands', underline=0) CmdBtn.pack(side=LEFT, padx="2m") CmdBtn.menu = Menu(CmdBtn) CmdBtn.menu.add_command(label="Undo") CmdBtn.menu.entryconfig(0, state=DISABLED) CmdBtn.menu.add_command(label='New...', underline=0, command=new_file) CmdBtn.menu.add_command(label='Open...', underline=0, command=open_file) CmdBtn.menu.add_command(label='Wild Font', underline=0,font=('Tempus Sans ITC', 14), command=stub_action) CmdBtn.menu.add_command(bitmap="@bitmaps/RotateLeft") CmdBtn.menu.add('separator') CmdBtn.menu.add_command(label='Quit', underline=0,background='white', activebackground='green',command=CmdBtn.quit) CmdBtn['menu'] = CmdBtn.menu return CmdBtndef makeCascadeMenu(): CasBtn = Menubutton(mBar, text='Cascading Menus', underline=0) CasBtn.pack(side=LEFT, padx="2m") CasBtn.menu = Menu(CasBtn) CasBtn.menu.choices = Menu(CasBtn.menu) CasBtn.menu.choices.wierdones = Menu(CasBtn.menu.choices) CasBtn.menu.choices.wierdones.add_command(label='Stockbroker') CasBtn.menu.choices.wierdones.add_command(label='Quantity Surveyor') CasBtn.menu.choices.wierdones.add_command(label='Church Warden') CasBtn.menu.choices.wierdones.add_command(label='BRM') CasBtn.menu.choices.add_command(label='Wooden Leg') CasBtn.menu.choices.add_command(label='Hire Purchase') CasBtn.menu.choices.add_command(label='Dead Crab') CasBtn.menu.choices.add_command(label='Tree Surgeon') CasBtn.menu.choices.add_command(label='Filing Cabinet') CasBtn.menu.choices.add_command(label='Goldfish') CasBtn.menu.choices.add_cascade(label='Is it a...',menu=CasBtn.menu.choices.wierdones) CasBtn.menu.add_cascade(label='Scipts', menu=CasBtn.menu.choices) CasBtn['menu'] = CasBtn.menu return CasBtndef makeCheckbuttonMenu(): ChkBtn = Menubutton(mBar, text='Checkbutton Menus', underline=0) ChkBtn.pack(side=LEFT, padx='2m') ChkBtn.menu = Menu(ChkBtn) ChkBtn.menu.add_checkbutton(label='Doug') ChkBtn.menu.add_checkbutton(label='Dinsdale') ChkBtn.menu.add_checkbutton(label="Stig O'Tracy") ChkBtn.menu.add_checkbutton(label='Vince') ChkBtn.menu.add_checkbutton(label='Gloria Pules') ChkBtn.menu.invoke(ChkBtn.menu.index('Dinsdale')) ChkBtn['menu'] = ChkBtn.menu return ChkBtndef makeRadiobuttonMenu(): RadBtn = Menubutton(mBar, text='Radiobutton Menus', underline=0) RadBtn.pack(side=LEFT, padx='2m') RadBtn.menu = Menu(RadBtn) RadBtn.menu.add_radiobutton(label='metonymy') RadBtn.menu.add_radiobutton(label='zeugmatists') RadBtn.menu.add_radiobutton(label='synechdotists') RadBtn.menu.add_radiobutton(label='axiomists') RadBtn.menu.add_radiobutton(label='anagogists') RadBtn.menu.add_radiobutton(label='catachresis') RadBtn.menu.add_radiobutton(label='periphrastic') RadBtn.menu.add_radiobutton(label='litotes') RadBtn.menu.add_radiobutton(label='circumlocutors') RadBtn['menu'] = RadBtn.menu return RadBtndef makeDisabledMenu(): Dummy_button = Menubutton(mBar, text='Disabled Menu', underline=0) Dummy_button.pack(side=LEFT, padx='2m') Dummy_button["state"] = DISABLED return Dummy_buttonroot = Tk()mBar = Frame(root, relief=RAISED, borderwidth=2)mBar.pack(fill=X)CmdBtn = makeCommandMenu()CasBtn = makeCascadeMenu()ChkBtn = makeCheckbuttonMenu()RadBtn = makeRadiobuttonMenu()NoMenu = makeDisabledMenu()mBar.tk_menuBar(CmdBtn, CasBtn, ChkBtn, RadBtn, NoMenu)root.title('Menus')root.mainloop()John Grayson
More information about the Python-listmailing list
[8]ページ先頭