第16章 菜单(Menu) 菜单是GUI界面非常重要的一个组成部分。几乎所有的应用都会用到菜单。tkinter也有菜单控件。菜单控件分为三种:
顶层菜单(Toplevel) 这种菜单是直接位于标题下面的固定菜单。 2.下拉菜单(pulldown) 当一个菜单有多个功能或者多个选择时,比如我们使用wps 文字处理软件,其中的文件菜单就有很多的功能,“打开文件”,“关闭文件”等等。窗口的大小是有限的,不能把所有的菜单项都做成顶层菜单,这个时候就需要下拉菜单。弹出菜单(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鼠标经过时边框宽度。默认值为0activeforeground鼠标经过时文本颜色backgroundbg背景颜色。默认是系统指定颜色borderwidthbd边框宽度。一般是1~2个像素值。cursor当鼠标移动经过菜单的时候,显示的光标样式disabledforeground菜单被禁止使用的时候的文本颜色font菜单文字的字体。只能选择一种字体显示。foregroundfg菜单中文字的颜色postcommand选择子菜单条的时候的回调函数。(不是子菜单项)relief边框的美化效果。默认值是FLAT,其他的可选项包括:SUNKEN , RAISED, GROOVE, and RIDGEselectcolor针对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('',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
|