• Python tkinter-- 第16章 菜单(Menu)方法


    16.2 方法

    方法描述
    :add(type, **options)增加一个菜单项。
    Type的可选数值包括:“command”, “cascade” (submenu), “checkbutton”, “radiobutton”, “separator”
    add_cascade(**options)增加一个层叠(cascade)菜单项
    add_checkbutton(**options)增加一个带检查按钮(checkbutton)菜单项
    add_command(**options)增加一个命令(command)菜单项
    add_radiobutton(**options)增加一个带无线按钮(radiobutton)菜单项
    add_separator(**options)增加一个分割条(separator)
    config(**options)配置菜单属性。具体见菜单属性说明。
    delete(index1, index2=None)删除1个或者多个菜单项。
    index1:要删除的菜单项的起始值
    index2:要删除的菜单项的终止值。如果没有设定该值,则只删除index1指定的菜单项。
    entrycget(index, option)获得菜单项属性
    entryconfig(index, **options)
    entryconfigure(index, **options)
    更改菜单项属性
    index(index)将菜单项的索引转换为整型数,不管指定的index是何种类型。
    insert(index, itemType, **options)插入指定类型的菜单项。
    insert_cascade(index, **options)添加子菜单(层叠菜单)。
    insert_checkbutton(index, **options)添加检查按钮菜单(checkbutton)。
    insert_command(index, **options)添加命令菜单(command)
    insert_radiobutton(index, **options)添加无线按钮菜单(radiobutton)。
    insert_separator(index, **options)添加分割条。
    invoke(index)调用index索引的菜单项功能
    post(x,y)在坐标(x,y)指定的位置显示弹出菜单
    type(index)返回指定菜单项的类型。
    unpost()去掉posted菜单
    yposition(index)返回index指定菜单项的垂直偏移量。用在弹出菜单中。
    **16.2.1 add( itemType, cnf={}, kw):
    根据源代码的注释,这是一个内部函数,不过也可以使用。鉴于可移植性的考虑,不建议使用该函数。
    itemType的类型有:
    (1)cascade
    (2)checkbutton
    (3)command
    (4)radiobutton
    (5)separator

    相应的类型都有add_xxx函数。比如add_cascade等。
    **16.2.2 add_cascade(cnf={}, kw)
    增加一个层叠菜单,就是子菜单。比如前面的例子就使用了这个方法。
    menubar.add_cascade(label=‘文件’, menu=filemenu)
    此代码就是把filemenu定义的菜单,作为层叠菜单加入到menubar定义的顶层菜单之中。
    **16.2.3 add_checkbutton(cnf={}, kw)
    添加一个checkbutton类型的菜单项。
    filemenu.add_checkbutton(label=‘打开文件’, command=open_file, variable=openVar)
    checkbutton 类型的菜单项可以多选,就是可以有多个checkbutton类型的菜单项被选中。这个是与radiobutton菜单项的区别。radiobutton菜单项只能有一个被选中。
    checkbutton 菜单项被选中后,会在文本标识的前面出现一个’√’。再次点击该菜单项,则’√’消失,表示不再被选中。
    在这里插入图片描述
    **16.2.4 add_command(cnf={}, kw)
    增加一个命令菜单项。选项有:
    (1)label
    使用文本标签
    (2)bitmap
    使用图像,早期的bitmap类型的。现在已经几乎不使用了。
    (3)image
    使用图片作为菜单项标识。
    (4)command
    菜单项的回调函数。点击菜单并释放按键后,会调用这个函数

    具体例子见16.1的说明
    **16.2.5 add_radiobutton(cnf={}, kw)
    增加一个radiobutton菜单项。Radiobutton与checkbutton非常类似,只不过radiobutton同时只能选中一组菜单项中的一个,而checkbutton可以选中多个。
    **16.2.6 add_separator(cnf={}, kw)
    增加一个分隔条。就是一条水平的线,把菜单命令分成不同组。
    16.2.7 config(cnf={}, **kw)
    动态修改菜单项的属性。具体属性说明见16.1。另外不是所有的属性都可以动态修改。
    16.2.8 delete(index1, index2=None)
    删除菜单项。index1是起始的菜单项,而index2是结束的菜单项。如果没有输入index2,就只删除index1代表的一个菜单项。注意:删除是不包括index2代表的菜单项的。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file():
        b1['text']='打开文件'
    def close_file():
        b1['text']='关闭文件'
    
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    
    filemenu.add_command(label='打开文件',command=open_file)
    filemenu.add_command(label='关闭文件',command=close_file)
    filemenu.add_separator()
    filemenu.add_command(label='测试1')
    filemenu.add_command(label='测试2')
    filemenu.add_command(label='测试3')
    filemenu.add_command(label='测试4')
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    def delete():
        filemenu.delete(3,7)
    
    b3=tk.Button(root,text='Delete',command=delete)
    b3.pack(side='left')
    
    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
    • 29
    • 30
    • 31
    • 32
    • 33

    16.2.9 entrycget(index, option)
    获取index菜单项的属性。注意,这里获取的是用add_xxx添加的菜单项,而不是用tk.Menu声明的菜单。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file():
        b1['text']='打开文件'
    def close_file():
        b1['text']='关闭文件'
    
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    
    filemenu.add_command(label='打开文件',command=open_file)
    filemenu.add_command(label='关闭文件',command=close_file)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    def cget():
        b1['text']=filemenu.entrycget(1,'label')
    
    b3=tk.Button(root,text='CGet',command=cget)
    b3.pack(side='left')
    
    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.2.10 entryconfigure(index, cnf=None, kw)
    更改菜单项属性。主要用于动态修改菜单。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file():
        b1['text']='打开文件'
    def close_file():
        b1['text']='关闭文件'
    def saveas_file():
        b1['text']='另存为'
        
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    
    filemenu.add_command(label='打开文件',command=open_file)
    filemenu.add_command(label='关闭文件',command=close_file)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    
    
    def config():
        filemenu.entryconfigure(1,label='另存为',command=saveas_file)
    
    b3=tk.Button(root,text='Config',command=config)
    b3.pack(side='left')
    
    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
    • 29
    • 30
    • 31

    entryconfig 与entryconfigure的作用是一样的。
    16.2.11 index(index)
    返回index的整数形式位置值或者索引值。注意计数是从0开始,tearoff和separator都算占用位置的。index可以是tk.END等非数字形式的索引。
    **16.2.12 insert( index, itemType, cnf={}, kw)
    insert是个内部函数。类似于前面的add()函数。请参考16.2.1节的说明
    **16.2.13 insert_cascade(index, cnf={}, kw)
    在指定位置插入一个层叠菜单。这个函数与add_cascade()类似,不过add_cascade()只能顺序添加,而insert_cascade()支持在指定的位置添加。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file():
        b1['text']='打开文件'
    def close_file():
        b1['text']='关闭文件'
    def saveas_file():
        b1['text']='另存为' 
    menubar = tk.Menu(root)
    filemenu=tk.Menu(menubar)
    c=tk.Menu(menubar)
    c.add_command(label='另存为',command=saveas_file)
    filemenu.add_command(label='打开文件',command=open_file)
    filemenu.add_command(label='关闭文件',command=close_file)
    menubar.add_cascade(label='文件', menu=filemenu)
    menubar.add_command(label='退出',command=root.destroy)
    def insert_c():
        filemenu.insert_cascade(index=2,label='插入',menu=c)
    
    b3=tk.Button(root,text='Insert_Cascade',command=insert_c)
    b3.pack(side='left')
    
    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
    • 29

    结果:
    在这里插入图片描述
    在这里插入图片描述
    **16.2.14 insert_checkbutton( index, cnf={}, kw)
    在指定位置插入一个checkbutton菜单项。用法类似add_checkbutton(),见16.2.3。
    **16.2.15 insert_command(index, cnf={}, kw)
    在指定位置插入一个命令菜单项。用法类似add_command(),见16.2.4
    **16.2.16 insert_radiobutton( index, cnf={}, kw)
    在指定位置插入一个radiobutton菜单项。用法类似add_radiobutton(),见16.2.5
    **16.2.17 insert_separator(index, cnf={}, kw)
    在指定位置插入一个separator菜单项。用法类似add_separator(),见16.2.6
    16.2.18 invoke(index)
    调用index指定位置的菜单项的回调函数。等同于点击了该菜单项。
    比如:filemenu.invoke(1)
    16.2.19 post( x, y)
    在坐标(x,y)处弹出菜单,就是实现弹出式菜单。一般是点击右键。由于(x,y)的坐标是屏幕的坐标,因此需要先取得窗口的坐标,再和event中的鼠标坐标相加,就是窗口的坐标到屏幕的坐标的变换。

    import tkinter as tk
    from tkinter import ttk
    
    root=tk.Tk()
    root.geometry('320x240')
    b1=tk.Label()
    b1.pack()
    def open_file():
        b1['text']='打开文件'
    def close_file():
        b1['text']='关闭文件'
    
    filemenu=tk.Menu(root)
    filemenu.add_command(label='打开文件',command=open_file)
    filemenu.add_command(label='关闭文件',command=close_file)
    
    def pop_up(event):
        root.update()
        x= root.winfo_rootx()
        y=root.winfo_rooty()
        filemenu.post(x+event.x,y+event.y)
    
    root.bind('',pop_up)    
    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

    结果:
    在这里插入图片描述
    16.2.20 type(index)
    返回指定菜单项的类型:
    (1)Cascade
    (2)command
    (3)checkbutton
    (4)radiobutton
    (5)seperator

    比如:filemenu.type(1)
    16.2.21 yposition(index)
    返回index位置的垂直坐标。

  • 相关阅读:
    蓝桥杯物联网竞赛_STM32L071_19_输出方波信号(PWM)
    Spark学习(5)-Spark Core之RDD
    MySQL及MySQLworkbench安装教程
    无重复字符的最长子串
    Banana Pi BPI-W3 NAS 开源路由器开发板采用瑞芯微 RK3588设计,板载8G内存和32G eMMC存储
    【技术支持案例】S32K146的hard fault问题处理
    黑客/网络安全【零基础自学】
    阿里前端面试问到的vue问题
    JavaScript系列之获取变量数据类型
    Java 基础之异常体系
  • 原文地址:https://blog.csdn.net/weixin_42272768/article/details/100808991