• Python tkinter-- 第16章 菜单(Menu)菜单项选项


    16.3 菜单项选项
    创建菜单项时的选项,比如定义快捷键等等。

    选项描述
    accelerator定义快捷键。比如accelerator=’^x’。同时还需要绑定键盘输入,才能实现快捷键的功能
    activebackground鼠标经过时背景颜色。
    activeforeground鼠标经过时文本颜色
    background背景颜色。默认是系统指定颜色
    bitmap在菜单项中显示bitmap图片
    columnbreak在新的一列开始显示菜单项。
    command菜单项的回调函数
    compound图文混合的时候,摆放文本的方式。
    font设置字体
    foreground定义菜单项文本的颜色
    image在菜单项中显示gif等格式的图片
    label显示文本信息
    menu只在添加层叠菜单的时候有效。就是把下级的子菜单与上一级父菜单关联
    offvaluecheckbutton 没有被选中的时候,默认值是0。可以通过设置这个参数修改为其他数值
    onvaluecheckbutton被选中的时候,默认值是1。可以通过设置这个参数修改为其他数值
    selectcolor设置checkbutton或者radiobutton标识的颜色
    selectimage在checkbutton或者radiobutton使用image作为提示信息的时候,可以设定选中的情况下,使用不同于未被选中的图片
    state菜单项的状态。
    underline设置下划线。使用方法是underline=index。index就是字符的位置,会在该位置的字符下出现一个下划线。
    valueradiobutton菜单项的数值。radiobutton是一组菜单项组成的。同一组radiobutton使用相同的变量,但是有不同的数值。
    variable与checkbutton或者radiobutton关联的变量。
    16.3.1 accelerator
    定义快捷键。需要绑定键盘输入。注意,绑定字符是大小写敏感的。
    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file(*args):
        b1['text']='打开文件'
        print(*args)
    def close_file():
        b1['text']='关闭文件'
        
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    
    filemenu.add_command(label='打开文件',
                         command=open_file,accelerator='^o')
    filemenu.add_command(label='关闭文件',command=close_file)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    root.bind('',open_file)
    root.config(menu=menubar)
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    16.3.2 activebackground
    鼠标经过时的背景颜色。见16.1.1
    16.3.3 activeforeground
    鼠标经过时的文本颜色。见16.1.3
    16.3.4 background
    菜单项的背景颜色。见16.1.4
    16.3.5 bitmap
    显示bitmap图片。优先级高于文本。不过此种格式的图像文件已经很少使用了。不建议在程序中使用这种方法。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file(*args):
        b1['text']='打开文件'
        print(*args)
    def close_file():
        b1['text']='关闭文件'
        
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    
    filemenu.add_command(label='打开文件',
                         command=open_file,bitmap='info')
    filemenu.add_command(label='关闭文件',command=close_file)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    root.bind('',open_file)
    
    root.config(menu=menubar)
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    结果:
    在这里插入图片描述
    16.3.6 columnbreak
    正常情况下,菜单项是显示在一列中的,不过设置columnbreak=True之后,会重新开始一列来显示后面的菜单项。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file(*args):
        b1['text']='打开文件'
        print(*args)
    def close_file():
        b1['text']='关闭文件'
    def saveas():
        b1['text']='另存为'
    
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    
    filemenu.add_command(label='打开文件',
                         command=open_file,columnbreak=True)
    filemenu.add_command(label='关闭文件',
                         command=close_file,columnbreak=True)
    filemenu.add_command(label='另存为',command=saveas)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    root.config(menu=menubar)
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    结果:
    在这里插入图片描述
    16.3.7 command
    菜单项的处理函数,前面已经说明了。
    16.3.8 compound
    定义图文混排的方法。见第4章Label中的说明。
    16.3.9 font
    设置菜单项的显示字体。见16.1.8中的说明。
    16.3.10 foreground
    定义菜单项文本的颜色。见16.1.9。
    16.3.11 hidemargin
    默认的情况下,菜单项的左右两侧都留有一定的空白空间。如果hidemargin=True,就会去掉这些留下的空白空间。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file(*args):
        b1['text']='打开文件'
        print(*args)
    def close_file():
        b1['text']='关闭文件'
        
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar,tearoff=False)
    
    filemenu.add_command(label='打开文件',
                         command=open_file,hidemargin=True)
    filemenu.add_command(label='关闭文件',
                         command=close_file,hidemargin=True)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    root.bind('',open_file)
    root.config(menu=menubar)
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    结果:
    在这里插入图片描述
    在这里插入图片描述
    说明:同一列的菜单项必须都设置hidemargin=True,才能去掉保留的空白空间。
    16.3.12 image
    在菜单条上显示.gif等格式的图片
    代码如下:

    p=tk.PhotoImage(file='a.gif')
    filemenu.add_command(label='打开文件',command=open_file,image=p)
    
    • 1
    • 2

    在这里插入图片描述
    16.3.13 label
    显示菜单项的文本信息。
    16.3.14 menu
    在设置层叠菜单的时候有效。就是把下级子菜单和上一级的父菜单相关联。
    16.3.15 offvalue 和 onvalue
    设置checkbutton在没有选中和选中情况的下的数值。默认情况,没被选中的值是0,被选中的值是1。offvalue对应没有选中时候的数值,而onvalue代表被选中时候的数值。可见Checkbutton控件的说明。
    16.3.16 selectcolor
    设置checkbutton或者radiobutton的标识颜色。详细说明见16.1.12。
    16.3.17 selectimage
    在checkbutton或者radiobutton的情况下,如果菜单项的状态是选中,可以通过selectimage来设定显示不同的图片和未被选中的状态相区分。
    16.3.18 state
    设置菜单项的状态。可以是tk.NORMAL,tk.DISABLED或者tk.ACTIVE。
    16.3.19 underline
    设置下划线在指定的字符下。underline是一个索引值,这个索引就是字符的位置。下划线的作用一般是提醒使用者,该菜单项的快捷键是什么。
    使用方式是underline=index。
    16.3.20 value
    radiobutton菜单项的数值。radiobutton是一组菜单项组成的。同一组radiobutton使用相同的变量,但是有不同的数值。
    16.3.21 variable
    与checkbutton或者radiobutton关联的变量,可以获取两种菜单项的状态。

  • 相关阅读:
    数据库安全:Hadoop 未授权访问-命令执行漏洞.
    牛客网刷题记录 || 链表
    2023年【四川省安全员A证】考试资料及四川省安全员A证考试试卷
    ICC2: secondary pg pin的作用与连接
    4-3端口服务版本和操作系统版本
    80W美团架构师整理分享出了Spring5企业级开发实战文档
    java-net-php-python-ssm仿猫眼电影计算机毕业设计程序
    Arrays.asList():使用指南
    UUCTF WP
    K8s(Kubernetes)学习(六)——Ingress
  • 原文地址:https://blog.csdn.net/weixin_42272768/article/details/100809120