Python tkinter-- 第16章 菜单(Menu)属性

52 篇文章 130 订阅

第16章 菜单(Menu)
菜单是GUI界面非常重要的一个组成部分。几乎所有的应用都会用到菜单。tkinter也有菜单控件。菜单控件分为三种:

  1. 顶层菜单(Toplevel)
    这种菜单是直接位于标题下面的固定菜单。
    2.下拉菜单(pulldown)
    当一个菜单有多个功能或者多个选择时,比如我们使用wps 文字处理软件,其中的文件菜单就有很多的功能,“打开文件”,“关闭文件”等等。窗口的大小是有限的,不能把所有的菜单项都做成顶层菜单,这个时候就需要下拉菜单。
  2. 弹出菜单(popmenu)
    弹出的菜单,最常见是使用点击右键,在鼠标位置处弹出一个菜单。

创建菜单的过程如下;
(1)创建顶层菜单
menubar=tk.Menu(root)
(2)创建菜单项
menubar.add_command(label=’Quit’,command=root.destroy)
(3)把创建的菜单与窗口关联
root.config(menubar)
在这里插入图片描述
结合其他的必要的代码,我们实现了一个顶层菜单以及一个菜单项。

创建下拉菜单的步骤如下:

(1)创建顶层菜单
menubar=tk.Menu(root)
(2)创建子菜单或者下拉菜单
filemenu=tk.Menu(menubar)
(3)子子菜单中添加菜单项
filemenu.add_command(label=’打开文件’,command=open_file)
filemenu.add_command(label=’关闭文件’,command=close_file)
(4)关联级联菜单
menubar.add_cascade(label=‘文件’, menu=filemenu)
(5)关联窗口
root.config(menubar)

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)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1 属性

属性描述
activebackground鼠标经过时背景颜色。
activeborderwidth鼠标经过时边框宽度。默认值为0
activeforeground鼠标经过时文本颜色
background
bg
背景颜色。默认是系统指定颜色
borderwidth
bd
边框宽度。一般是1~2个像素值。
cursor当鼠标移动经过菜单的时候,显示的光标样式
disabledforeground菜单被禁止使用的时候的文本颜色
font菜单文字的字体。只能选择一种字体显示。
foreground
fg
菜单中文字的颜色
postcommand选择子菜单条的时候的回调函数。(不是子菜单项)
relief边框的美化效果。默认值是FLAT,其他的可选项包括:SUNKEN , RAISED, GROOVE, and RIDGE
selectcolor针对checkbutton和radiobutton的情况,如果选中这两种类型的菜单项,标识的颜色会显示为selectcolor中的颜色。
tearoff菜单能否独立到为一个窗口。
tearoffcommandtearoff被选中后的回调函数
titletearoff窗口的标题
type定义菜单项的类型:normal,tearoff,menubar三种。
16.1.1 activebackground
鼠标经过下拉菜单项时的背景颜色。
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,activebackground=’red’)

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)

root.bind('<Control-o>',open_file)
root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
说明:
(1)顶层菜单是无法设置activebackground
(2)只能设置菜单项的activebackground
比如:filemenu=tk.Menu(menubar,activebackground=‘red’)
(3)可以在.add_command中设置(见16.3节)
比如:filemenu.add_command(label=‘打开文件’,command=open_file,activebackground=‘red’)
16.1.2 activeborderwidth
鼠标经过下拉菜单项时的边框宽度。

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,activeborderwidth=20)

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)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.3 activeforeground
鼠标经过菜单项时的文本颜色。

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,activeforeground='red')

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)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.4 background(bg)
菜单的背景色。

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,bg='yellow')

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)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.5 borderwidth(bd)
边框的宽度。只有菜单tearoff=True的时候才有效。见15.1.11节。
16.1.6 cursor
鼠标经过菜单项时的光标形状。但是在菜单tearoff状态下,才有效。

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,cursor='spider',tearoff=True)

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)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.7 disabledforeground
设置菜单项为DISABLED状态下的颜色。设置需要两步,第一步是在创建tk.Menu控件的时候,设置disabledforeground。第二步是设置菜单项为DISABLED。

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,disabledforeground='red')

filemenu.add_command(label='打开文件',
                     command=open_file,state=tk.DISABLED)
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.8 font
设置菜单中文本的字体。
(1)顶层菜单的字体不能更改,只能使用系统设定的
(2)子菜单,可以统一设置一种字体。每个子菜单可以设置不同的字体
filemenu=tk.Menu(menubar,font=(‘宋体’,20,‘bold’))
(3)子菜单中的菜单项可以设置不同的字体
filemenu.add_command(label=‘打开文件’,command=open_file,font=(‘宋体’,20,‘bold’))
具体使用要看程序的需要。不过一般一个子菜单中,使用的字体应该相同。

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,
                     font=('宋体',20,'bold'))
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)
root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.9 foreground(fg)
与background类似,不过foreground是设置菜单项的文本颜色。

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,fg='red')

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)
root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.10 postcommand
当菜单被点击时的回调函数。一般用于子菜单,可以实现动态调整菜单项。比如某些菜单项要禁止使用,或者增加/删除某些菜单项等等。

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 pt():
    b1['text']='文件'
    
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar,postcommand=pt)

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)

root.config(menu=menubar)
root.mainloop()

16.1.11 relief
设置菜单项的3D效果。不过只有在菜单tearoff=True的时候才有效。
在这里插入图片描述
16.1.12 selectcolor
针对checkbutton或者radiobutton的情况。选中时候的标志颜色。

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 copy():
    b1['text']='复制'
def paste():
    b1['text']='粘贴'

openVar = tk.IntVar()
closeVar = tk.IntVar()
editVar = tk.IntVar()
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar,selectcolor='red')
filemenu.add_checkbutton(label='打开文件',
                         command=open_file,
                         variable=openVar)
filemenu.add_checkbutton(label='关闭文件',
                         command=close_file,
                         variable=closeVar)
menubar.add_cascade(label='文件', menu=filemenu)

editmenu = tk.Menu(menubar)
editmenu.add_radiobutton(label='复制', command=copy,
                         variable=editVar, value=1)
editmenu.add_radiobutton(label='粘贴', command=paste,
                         variable=editVar, value=2)
menubar.add_cascade(label='编辑', menu=editmenu) 
menubar.add_command(label='退出',command=root.destroy)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.13 tearoff
tearoff就是菜单能否独立为一个窗口。在每一个下拉菜单中,如果tearoff=True,则第一个菜单如下:
在这里插入图片描述
如果选择这个菜单项,会在应用程序的窗口之外,弹出一个独立的菜单界面。如果tearoff=False,则没有这个菜单项。
16.1.14 tearoffcommand
tearoffcommand 是tearoff菜单项被触发时的回调函数。该回调函数会有两个参数被传入:主窗口ID和新建的tearoff窗口ID。

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 pt(*args):
    print(args)
    b1['text']='Tear Off:'+str(args)
    
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar,tearoffcommand=pt)

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)

root.config(menu=menubar)
root.mainloop()

16.1.15 title
这个title是tearoff窗口的标题。一般情况下,tearoff 窗口的标题就是子菜单的名称。比如15.1.14中,tearoff窗口的标题就是‘文件’。但是可以通过设置title属性,给tearoff窗口设置一个专有的标题。

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,title='文件菜单窗口')

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)

root.config(menu=menubar)
root.mainloop()

结果:
在这里插入图片描述
16.1.16 type
定义菜单控件的类型,有三种:
(1)normal
(2)tearoff
(3)menubar

  • 13
    点赞
  • 111
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值