本片文章详细的介绍tkinter的菜单控件。通过学习本篇文章,你将完全掌握tkinter中菜单的使用。废话不多说,开始干货。

概念

在介绍tkinter菜单之前,我先会介绍3个概念以及这3个概念之间的关系。以便更好的理解后文的表述。

菜单栏(menu bar)、菜单(menu)、菜单项(menu item),三者之间的层次关系如下:

下面这个实例图,更加的直观:

对于菜单栏(menu bar)来讲,菜单(menu)是其"菜单项";对于菜单(menu)来讲,菜单项(menu item)是其"菜单项"。其实代码层面上就只有menu和menu item的概念,这里增加了一个菜单栏的逻辑概念是为了方便表述

在创建菜单栏(menu bar)时,它的master是主窗口对象,然后通过主窗口对象的config函数将两者(菜单栏、主窗口)关联起来;在创建菜单(menu)时,这个菜单是 ‘menu’ 还是 ‘menu item’,根据不同的业务需求都是可以实现的。通常来讲,这个菜单对应的是’menu’,就像上图中的Edit菜单,它就是一个’menu’对象,它的master是菜单栏(menu bar)。Edit菜单的菜单项(menu item)是通过相应的方法(后面会具体介绍)将其与菜单关联起来;使用不同的关联方法,菜单的显示效果不同。可能初次看完这段表述,不能完全理解,没关系,继续往下看,等看完后面的实际代码的例子,再回过头来看这段话,就能完全明白了。

菜单(menu)

创建菜单控件

class Menu(master, cnf=..., **kw)
master 表示父窗口或父菜单
kw 表示菜单控件的各种属性选项

菜单属性

下面介绍菜单的14种属性

属性 作用
activebackground The background color that will appear on a choice when it is under the mouse.
设置活动背景色。当鼠标移动到菜单选项上时,该菜单项的背景色。
activeborderwidth Specifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel.
设置活动边框宽度。当鼠标移动到菜单选项上时,以指定宽度显示该选项的边框。默认宽度是一个像素。最终的效果就是菜单选项的宽度改变。
activeforeground The foreground color that will appear on a choice when it is under the mouse.
设置活动前景色。当鼠标移动到菜单选项上时,该菜单项文字显示的颜色。
bg The background color for choices not under the mouse.
设置背景色。
bd The width of the border around all the choices. Default is 1.
设置边框宽度。注意:该效果只有在tearoff子窗口才能显示出来
cursor The cursor that appears when the mouse is over the choices, but only when the menu has been torn off.
光标的显示样式。只有当 tearoff为1时,在tearoff子窗口中,光标的显示样式就会发生变化。
disabledforeground The color of the text for items whose state is DISABLED.
当菜单项的状态为DISABLED时,文字的显示颜色。
font The default font for textual choices.
设置字体
fg The foreground color used for choices not under the mouse.
设置前景色,也就是文字的颜色
postcommand You can set this option to a procedure, and that procedure will be called every time someone brings up this menu.
给菜单设置回调函数,当点击该菜单时,调用回调。
relief The default 3-D effect for menus is relief=RAISED.
设置3D效果。设置过’flat’, ‘groove’, ‘raised’, ‘ridge’, ‘solid’, 'sunken’都没有效果。
selectcolor Specifies the color displayed in checkbuttons and radiobuttons when they are selected.
针对checkbutton和radiobutton,如果选中这两种类型的菜单项,标识的颜色会显示为selectcolor设置的颜色。
tearoff Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.
tearoff的作用就是可以将每个菜单分离出去,单独形成一个子窗口。通常将tearoff设置为0。
title Normally, the title of a tear-off menu window will be the same as the text of the menubutton or cascade that lead to this menu. If you want to change the title of that window, set the title option to that string.
设置tear-off子窗口的标题。如果tearoff为0,不用设置该属性。

上面了解了菜单的15种属性的作用,有些童鞋会说,好像知道了,但是到底要怎么使用呢?还是心里没底。接下来会针对每一个属性给出一个小例子,来加深对菜单属性的理解与使用。

activebackground

def build_file_menu(menu_bar):# 设置 活动背景色为蓝色,当鼠标移动到菜单项时,该菜单项的背景变成蓝色。file_menu = Menu(menu_bar,activebackground='blue')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New")file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

activeforeground

def build_file_menu(menu_bar):# 设置 活动前景色为红色,当鼠标移动到菜单项时,该菜单项的文字变成红色。file_menu = Menu(menu_bar,activebackground='blue',activeforeground='red')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New")file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

activeborderwidth

def build_file_menu(menu_bar):# 设置 活动边框宽度为20file_menu = Menu(menu_bar,activebackground='blue',activeforeground='red',activeborderwidth=20)# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New")file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

bg

def build_file_menu(menu_bar):# 将背景色设置成绿色file_menu = Menu(menu_bar,activebackground='blue',activeforeground='red',activeborderwidth=20,bg='green')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New")file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

bd

def build_file_menu(menu_bar):# bd设置菜单项边框宽度file_menu = Menu(menu_bar,bd=20,bg='green')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New")file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

cursor

该属性是设置光标的显示样式。且只有在tearoff子窗口中,将鼠标移动到菜单项上时,就会显示相应的光标样式。样式有3种,“arrow”(默认),“watch”, “cross”。

arrow就是常见的光标箭头;

watch 是一个圈一直在转动;

cross是一个 十字;

因为不好截图截不到光标,所以这里就不展示了,可以自己运行代码看看实际的效果。

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,cursor="arrow")# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New")file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

disabledforeground

当菜单项的状态为DISABLED时,文字的显示颜色。

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,disabledforeground="blue")# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open")file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

本例中,file_menu设置了disabledforeground为蓝色,将"New"菜单项的state设置为DISABLED, "New"菜单项的文字变成了蓝色。

font

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,disabledforeground="blue")# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open", font=("Courier", 20, "bold"))file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

本例中,将"Open"菜单项的字体设置为"Courier"、大小为20、加粗。效果如下:

fg

设置文字的颜色

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,disabledforeground="blue", fg='red')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open", font=("Courier", 20, "bold"))file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

本例中,将菜单项的文字设置为红色,效果如下:

postcommand

给菜单设置回调函数,当点击该菜单时,调用回调。

def postcmd():print("call postcmd")# 创建File menu
def build_file_menu(menu_bar):file_menu = Menu(menu_bar,postcommand=postcmd)# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open", font=("Courier", 20, "bold"))file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

简单起见,postcmd函数的实现只是简单的执行了一个打印,运行该程序后,每次点击"File"菜单时,postcmd都会被调用。读者可以自己运行代码查看效果。

relief

设置3D效果,但是在我的环境中没有发现明显的显示效果。支持6种效果:RAISED=‘raised’

SUNKEN=‘sunken’,FLAT=‘flat’,RIDGE=‘ridge’,GROOVE=‘groove’,SOLID = ‘solid’。这里仅列出使用方法

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,postcommand=postcmd, relief=tk.SOLID)# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open", font=("Courier", 20, "bold"))file_menu.add_command(label="Save")file_menu.add_command(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

selectcolor

针对checkbutton和radiobutton,如果选中这两种类型的菜单项,标识的颜色会显示为selectcolor设置的颜色。

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,postcommand=postcmd, relief=tk.SOLID, selectcolor='green')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open", font=("Courier", 20, "bold"))# 添加check button菜单项file_menu.add_checkbutton(label="Save")# 添加radio button菜单项file_menu.add_radiobutton(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

下图中 绿色 的勾,即是selector指定的颜色。

tearoff

前面介绍其他属性时,其实我们已经用到过tearoff属性,tkinter默认 tearoff是打开的状态,也就是可以变成tearoff子窗口。

tearoff 打开时,点击下图中的虚线,会弹出一个tearoff子窗口。

tearoff 关闭时,下拉菜单中没有虚线。

title

设置 tearoff子窗口的title。

def build_file_menu(menu_bar):file_menu = Menu(menu_bar,title='tearofftitle')# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加菜单项file_menu.add_command(label="New", state=tk.DISABLED)file_menu.add_command(label="Open", font=("Courier", 20, "bold"))file_menu.add_checkbutton(label="Save")file_menu.add_radiobutton(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")

ok,菜单属性就先介绍到这里,每个属性的怎么使用,它的效果是怎样的?运行实例代码或者在此基础上进行改动,多动手,就能熟练掌握菜单的属性。

菜单方法

前面介绍菜单属性时,其实用到一些菜单方法,但没有做过多的解释,本节将会详细讲解菜单方法。

Menu类的方法有很多,不可能一一列举,下面仅列出一些常用的方法

  • add_command() 通常用于 给菜单添加普通的菜单项。
  • add_cascade() 通常用于 给menu对象添加一个瀑布菜单,点击menu对象就会弹出多个菜单项。
  • add_radiobutton() 给菜单添加radiobutton菜单项。
  • add_checkbutton() 给菜单添加checkbutton菜单项
  • add_separator() 给菜单添加分割线
  • add() 给菜单添加指定类型的菜单项,前面5中添加菜单项的方法其实都是在add函数的基础上做一个封装。
  • type() 查看菜单项的类型。通常菜单项的类型有"cascade", “checkbutton”, “command”, “radiobutton”, “separator”, “tearoff”。
def build_file_menu(menu_bar):file_menu = Menu(menu_bar)# 添加瀑布菜单menu_bar.add_cascade(label='File', menu=file_menu)# 添加普通菜单项file_menu.add_command(label="New")# 添加普通菜单项file_menu.add(tk.COMMAND, label="Open")# 添加checkbutton 菜单项file_menu.add_checkbutton(label="Save")# 添加radiobutton 菜单项file_menu.add_radiobutton(label="Save as...")file_menu.add_command(label="Close")# 添加分割线file_menu.add_separator()file_menu.add_command(label="Exit")print(file_menu.type(0))print(file_menu.type(1))print(file_menu.type(2))print(file_menu.type(3))

运行结果图:

最后的四行menu item type打印信息 即是对应 18~21行。tkinter支持的menu item type:

import tkinter as tk
tk.CASCADE
tk.CHECKBUTTON
tk.COMMAND
tk.RADIOBUTTON
tk.SEPARATOR

好了,关于tkinter的菜单的知识点就介绍到这里,如有新的知识点,会更新本文。

最后,附上本文的完整示例代码。

https://download.csdn.net/download/weixin_43708622/12565182
https://download.csdn.net/download/weixin_43708622/12565174

tkinter-menu详解相关推荐

  1. Tkinter 组件详解(十):Scale

    Tkinter 组件详解之Scale Scale(刻度)组件看起来像是一个带数据的 Scrollbar(滚动条)组件,但事实上它们是不同的两个东东.Scale 组件允许用于通过滑动滑块来选择一个范围内 ...

  2. Tkinter 组件详解(一):Label

    Tkinter 组件详解之Label Label(标签)组件用于在屏幕上显示文本或图像.Label 组件仅能显示单一字体的文本,但文本可以跨越多行.另外,还可以为其中的个别字符加上下划线(例如用于表示 ...

  3. Tkinter 组件详解(七):Entry

    Tkinter 组件详解之Entry Entry(输入框)组件通常用于获取用户的输入文本. 何时使用 Entry 组件? Entry 组件仅允许用于输入一行文本,如果用于输入的字符串长度比该组件可显示 ...

  4. Menu详解(二):利用XML生成菜单和子菜单

    前言:上篇,我们说了有关代码生成菜单和子菜单的方法,这里我们再讲讲有关利用XML生成菜单和子菜单的问题. 业精于勤,荒于嬉,行成于思,毁于随 (日拱一卒) 系列文章: 1.<Menu详解(一): ...

  5. Tkinter 组件详解(八):Listbox

    Tkinter 组件详解之Listbox Listbox(列表框)组件用于显示一个选择列表.Listbox 只能包含文本项目,并且所有的项目都需要使用相同的字体和颜色.根据组件的配置,用户可以从列表中 ...

  6. Tkinter 组件详解(五):Frame

    Tkinter 组件详解之Frame Frame(框架)组件是在屏幕上的一个矩形区域.Frame 主要是作为其他组件的框架基础,或为其他组件提供间距填充. 何时使用 Frame 组件? Frame 组 ...

  7. Tkinter 组件详解(九):Scrollbar

    Tkinter 组件详解之Scrollbar Scrollbar(滚动条)组件用于滚动一些组件的可见范围,根据方向可分为垂直滚动条和水平滚动条.Scrollbar 组件常常被用于实现文本.画布和列表框 ...

  8. Tkinter组件详解(五):Listbox和Scrollbar

    Tkinter组件详解之Listbox Listbox(列表框)组件用于显示一个选择列表.Listbox 只能包含文本项目,并且所有的项目都需要使用相同的字体和颜色.根据组件的配置,用户可以从列表中选 ...

  9. Tkinter 组件详解(四):Radiobutton

    Tkinter 组件详解之Radiobutton Radiobutton(单选按钮)组件用于实现多选一的问题.Radiobutton 组件可以包含文本或图像,每一个按钮都可以与一个 Python 的函 ...

  10. Python Tkinter模块详解(后续持续补充)

    声明:该文章是个人学习中写的,目的是总结及当作工具参考,有一定的借鉴成分,后续若有新发现则补充 目录 Tkinter简介 创建组件基本语法 Tkinter组件汇总 Variable 类 常见参数详解 ...

最新文章

  1. 句法依存分析_[NLP学习笔记]句法分析
  2. python3.7安装教程linux_linux系统安装Python 3.7.x
  3. struts2控制标签(一)选择标签,iterator标签,append标签
  4. 第七章 字典和集合[DDT书本学习 小甲鱼]【2】
  5. 4-pycharm找不到模块问题
  6. GC Blocks Lost等待事件
  7. 使用python开发网页游戏_不敢想!不敢想!我用Python自动玩转2048游戏
  8. MySQL字段类型解析
  9. delphi2010中FastReport的安装方法
  10. ResNet网络结构详解与模型的搭建
  11. Mac系统制作win10启动U盘踩坑实操
  12. 景深与光圈、拍摄距离和镜头焦距的关系
  13. sony电视遥控器android,划重点!如何用手机当“点播遥控器”?
  14. 基于神经网络 lstm的股票开盘价收盘价预测详细
  15. 如何用tushare复盘
  16. 艺赛旗(RPA)isrpa7.0 的 IE 自动 pagedown 到我们需要操作的地方
  17. 奔赴山海之前,毕业季一定要做的那些事情
  18. “光束法”和“空中三角测量”的辨析
  19. 【牛客网笔试整理】美团点评 笔试整理
  20. ChatGPT、低代码等技术出现会不会导致底层程序员失业

热门文章

  1. scrapy爬虫框架[scrapy.core.scraper]ERROR downloading Error processing
  2. vue脚手架工程搭建
  3. 数学计算机培训反思,小学数学教师远程培训学习心得体会
  4. C++ count函数
  5. mysql general log使用介绍
  6. SEM | TASK1:了解 SEM 行业基础业务知识
  7. 计算机常用涵数有哪些,计算机常用函数.doc
  8. 从数据流中获取中位数
  9. 传输层协议TCP;MSS最大段尺寸
  10. python密码传参有特殊字符如何解决_如何通过请求Python packag传递密码中的特殊字符''...