前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python tkinter(3) menu 例子

python tkinter(3) menu 例子

作者头像
用户5760343
发布2022-05-13 10:53:50
6620
发布2022-05-13 10:53:50
举报
文章被收录于专栏:sktj

1、menubutton

image.png

image.png

2、optionmenu from tkinter import * root = Tk()

var1 = StringVar() var2 = StringVar() opt1 = OptionMenu(root, var1, 'spam', 'eggs', 'toast') # like Menubutton opt2 = OptionMenu(root, var2, 'ham', 'bacon', 'sausage') # but shows choice opt1.pack(fill=X) opt2.pack(fill=X) var1.set('spam') var2.set('ham')

def state(): print(var1.get(), var2.get()) # linked variables Button(root, command=state, text='state').pack() root.mainloop() 3、menu例子

!/usr/local/bin/python

""" Tk8.0 style main window menus menu/tool bars packed before middle, fill=X (pack first=clip last); adds photo menu entries; see also: add_checkbutton, add_radiobutton """

from tkinter import * # get widget classes from tkinter.messagebox import * # get standard dialogs

class NewMenuDemo(Frame): # an extended frame def init(self, parent=None): # attach to top-level? Frame.init(self, parent) # do superclass init self.pack(expand=YES, fill=BOTH) self.createWidgets() # attach frames/widgets self.master.title("Toolbars and Menus") # set window-manager info self.master.iconname("tkpython") # label when iconified

代码语言:javascript
复制
def createWidgets(self):
    self.makeMenuBar()
    self.makeToolBar()
    L = Label(self, text='Menu and Toolbar Demo')
    L.config(relief=SUNKEN, width=40, height=10, bg='white')
    L.pack(expand=YES, fill=BOTH)

def makeToolBar(self):
    toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
    toolbar.pack(side=BOTTOM, fill=X)
    Button(toolbar, text='Quit',  command=self.quit    ).pack(side=RIGHT)
    Button(toolbar, text='Hello', command=self.greeting).pack(side=LEFT)

def makeMenuBar(self):
    self.menubar = Menu(self.master)
    self.master.config(menu=self.menubar)    # master=top-level window
    self.fileMenu()
    self.editMenu()
    self.imageMenu()

def fileMenu(self):
    pulldown = Menu(self.menubar)
    pulldown.add_command(label='Open...', command=self.notdone)
    pulldown.add_command(label='Quit',    command=self.quit)
    self.menubar.add_cascade(label='File', underline=0, menu=pulldown)

def editMenu(self):
    pulldown = Menu(self.menubar)
    pulldown.add_command(label='Paste',   command=self.notdone)
    pulldown.add_command(label='Spam',    command=self.greeting)
    pulldown.add_separator()
    pulldown.add_command(label='Delete',  command=self.greeting)
    pulldown.entryconfig(4, state=DISABLED)
    self.menubar.add_cascade(label='Edit', underline=0, menu=pulldown)

def imageMenu(self):
    photoFiles = ('ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif')
    pulldown = Menu(self.menubar)
    self.photoObjs = []
    for file in photoFiles:
        img = PhotoImage(file='../gifs/' + file)
        pulldown.add_command(image=img, command=self.notdone)
        self.photoObjs.append(img)   # keep a reference
    self.menubar.add_cascade(label='Image', underline=0, menu=pulldown)

def greeting(self):
    showinfo('greeting', 'Greetings')
def notdone(self):
    showerror('Not implemented', 'Not yet available')
def quit(self):
    if askyesno('Verify quit', 'Are you sure you want to quit?'):
        Frame.quit(self)

if name == 'main': NewMenuDemo().mainloop() # if I'm run as a script 4、

!/usr/local/bin/python

""" same but add photos in toolbar using PIL to generate images """

from tkinter import * # get widget classes from tkinter.messagebox import * # get standard dialogs

class NewMenuDemo(Frame): # an extended frame def init(self, parent=None): # attach to top-level? Frame.init(self, parent) # do superclass init self.pack(expand=YES, fill=BOTH) self.createWidgets() # attach frames/widgets self.master.title("Toolbars and Menus") # set window-manager info self.master.iconname("tkpython") # label when iconified

代码语言:javascript
复制
def createWidgets(self):
    self.makeMenuBar()
    self.makeToolBar()
    L = Label(self, text='Menu and Toolbar Demo')
    L.config(relief=SUNKEN, width=40, height=10, bg='white')
    L.pack(expand=YES, fill=BOTH)

#def makeToolBar(self):
#    toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
#    toolbar.pack(side=BOTTOM, fill=X)
#    Button(toolbar, text='Quit',  command=self.quit    ).pack(side=RIGHT)
#    Button(toolbar, text='Hello', command=self.greeting).pack(side=LEFT)

def makeToolBar(self, size=(40, 40)):
    from PIL.ImageTk import PhotoImage, Image     # if jpegs or make new thumbs
    imgdir = r'../PIL/images/'
    toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
    toolbar.pack(side=BOTTOM, fill=X)
    photos = 'ora-lp4e-big.jpg', 'PythonPoweredAnim.gif', 'python_conf_ora.gif'
    self.toolPhotoObjs = []
    for file in photos:
        imgobj = Image.open(imgdir + file)        # make new thumb
        imgobj.thumbnail(size, Image.ANTIALIAS)   # best downsize filter
        img = PhotoImage(imgobj)
        btn = Button(toolbar, image=img, command=self.greeting)
        btn.config(relief=RAISED, bd=2)
        btn.config(width=size[0], height=size[1])
        btn.pack(side=LEFT)
        self.toolPhotoObjs.append((img, imgobj))  # keep a reference
    Button(toolbar, text='Quit', command=self.quit).pack(side=RIGHT, fill=Y)

def makeMenuBar(self):
    self.menubar = Menu(self.master)
    self.master.config(menu=self.menubar)    # master=top-level window
    self.fileMenu()
    self.editMenu()
    self.imageMenu()

def fileMenu(self):
    pulldown = Menu(self.menubar)
    pulldown.add_command(label='Open...', command=self.notdone)
    pulldown.add_command(label='Quit',    command=self.quit)
    self.menubar.add_cascade(label='File', underline=0, menu=pulldown)

def editMenu(self):
    pulldown = Menu(self.menubar)
    pulldown.add_command(label='Paste',   command=self.notdone)
    pulldown.add_command(label='Spam',    command=self.greeting)
    pulldown.add_separator()
    pulldown.add_command(label='Delete',  command=self.greeting)
    pulldown.entryconfig(4, state=DISABLED)
    self.menubar.add_cascade(label='Edit', underline=0, menu=pulldown)

def imageMenu(self):
    photoFiles = ('ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif')
    pulldown = Menu(self.menubar)
    self.photoObjs = []
    for file in photoFiles:
        img = PhotoImage(file='../gifs/' + file)
        pulldown.add_command(image=img, command=self.notdone)
        self.photoObjs.append(img)   # keep a reference
    self.menubar.add_cascade(label='Image', underline=0, menu=pulldown)

def greeting(self):
    showinfo('greeting', 'Greetings')
def notdone(self):
    showerror('Not implemented', 'Not yet available')
def quit(self):
    if askyesno('Verify quit', 'Are you sure you want to quit?'):
        Frame.quit(self)

if name == 'main': NewMenuDemo().mainloop() # if I'm run as a script

5、

!/usr/local/bin/python

""" same, but add photos to toolbar as preexisting GIFs with standard tkinter """

from tkinter import * # get widget classes from tkinter.messagebox import * # get standard dialogs

class NewMenuDemo(Frame): # an extended frame def init(self, parent=None): # attach to top-level? Frame.init(self, parent) # do superclass init self.pack(expand=YES, fill=BOTH) self.createWidgets() # attach frames/widgets self.master.title("Toolbars and Menus") # set window-manager info self.master.iconname("tkpython") # label when iconified

代码语言:javascript
复制
def createWidgets(self):
    self.makeMenuBar()
    self.makeToolBar()
    L = Label(self, text='Menu and Toolbar Demo')
    L.config(relief=SUNKEN, width=40, height=10, bg='white')
    L.pack(expand=YES, fill=BOTH)

#def makeToolBar(self):
#    toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
#    toolbar.pack(side=BOTTOM, fill=X)
#    Button(toolbar, text='Quit',  command=self.quit    ).pack(side=RIGHT)
#    Button(toolbar, text='Hello', command=self.greeting).pack(side=LEFT)

def makeToolBar(self, size=(30, 30)):
    imgdir = r'../gifs/'
    toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
    toolbar.pack(side=BOTTOM, fill=X)
    photos = 'ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif'
    self.toolPhotoObjs = []
    for file in photos:
        img = PhotoImage(file=imgdir + file)
        btn = Button(toolbar, image=img, command=self.greeting)
        btn.config(bd=5, relief=RIDGE)
        btn.config(width=size[0], height=size[1])
        btn.pack(side=LEFT)
        self.toolPhotoObjs.append(img)  # keep a reference
    Button(toolbar, text='Quit', command=self.quit).pack(side=RIGHT, fill=Y)

def makeMenuBar(self):
    self.menubar = Menu(self.master)
    self.master.config(menu=self.menubar)    # master=top-level window
    self.fileMenu()
    self.editMenu()
    self.imageMenu()

def fileMenu(self):
    pulldown = Menu(self.menubar)
    pulldown.add_command(label='Open...', command=self.notdone)
    pulldown.add_command(label='Quit',    command=self.quit)
    self.menubar.add_cascade(label='File', underline=0, menu=pulldown)

def editMenu(self):
    pulldown = Menu(self.menubar)
    pulldown.add_command(label='Paste',   command=self.notdone)
    pulldown.add_command(label='Spam',    command=self.greeting)
    pulldown.add_separator()
    pulldown.add_command(label='Delete',  command=self.greeting)
    pulldown.entryconfig(4, state=DISABLED)
    self.menubar.add_cascade(label='Edit', underline=0, menu=pulldown)

def imageMenu(self):
    photoFiles = ('ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif')
    pulldown = Menu(self.menubar)
    self.photoObjs = []
    for file in photoFiles:
        img = PhotoImage(file='../gifs/' + file)
        pulldown.add_command(image=img, command=self.notdone)
        self.photoObjs.append(img)   # keep a reference
    self.menubar.add_cascade(label='Image', underline=0, menu=pulldown)

def greeting(self):
    showinfo('greeting', 'Greetings')
def notdone(self):
    showerror('Not implemented', 'Not yet available')
def quit(self):
    if askyesno('Verify quit', 'Are you sure you want to quit?'):
        Frame.quit(self)

if name == 'main': NewMenuDemo().mainloop() # if I'm run as a script

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/usr/local/bin/python
  • !/usr/local/bin/python
  • !/usr/local/bin/python
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档