1、使用Menu类创建一个菜单 2、add_command添加菜单项,如果该菜单是顶层菜单,则添加的菜单项依次向右添加。 如果该菜单时顶层菜单的一个菜单项,则它添加的是下拉 菜单的菜单项。 属性: label:指定菜单的名称 command:被点击时调用的方法 acceletor:快捷键 underline:是否拥有下划线 3、窗口的menu属性:指定顶级菜单...
menubar.add_cascade(label='编辑') menubar.add_cascade(label='关于') root['menu']=menubar root.mainloop() 图7.3 分割线 除了默认的点击后无显示的效果,Menu还可以设置单选框(add_radiobutton)与复选框(add_checkbutton),只需对应地替换掉add_command,例如复选框的实现: from tkinter import * root = T...
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('Command 事件绑定演示')defone(event): print('响应第一个处理程序!')deftwo(event): print('响应第二个处理程序!')root.bind('<Return>', one)root.bind('<Return>', two, add='+')root.mainloop()事件...
add_command(label='Quit', command=root.destroy) # 创建文本框,只能用字符数设置文本框的宽度 text_box = tk.Entry(root, bd=10) # 设置默认文本内容 text_box.insert(0, 'Default Text') # 占满当前布局 text_box.pack() root.config(menu=menubar) root.mainloop() 除了Entry文本框,还可以使用...
add_command(label="退出", command=root.quit) # 将菜单选项添加到菜单栏 menu_bar.add_cascade(label="文件", menu=file_menu) # 将菜单栏添加到主窗口 root.config(menu=menu_bar) # 运行主窗口的消息循环 root.mainloop() 在上面的示例中,我们创建了一个主窗口,并在菜单栏中添加了一个名为"文件"的...
import tkinter as tk def say_hello(): print("Hello World!") root = tk.Tk() menubar = tk.Menu(root) filemenu = tk.Menu(menubar, tearoff=0) filemenu.add_command(label="New") filemenu.add_command(label="Open") filemenu.add_command(label="Save") filemenu.add_separator() filemenu.add...
add_command 添加命令(label参数为显示内容) menu.add_separator 添加分隔线 menu.add_checkbutton 添加确认按钮 delete 删除 11、事件关联 代码语言:python 代码运行次数:0 运行 AI代码解释 bind(sequence,func,add)—— bind_class(className,sequence,func,add) bind_all(sequence,func,add) #Python小白学习交流...
add_command(label='查找', accelerator='Ctrl+F', command=self.find_text)self.content_text.bind('<Control-f>', self.find_text)#该语句是在_create_body_函数内部self.content_text.bind('<Control-F>', self.find_text)#该语句是在_create_body_函数内部 1. 2. 3. 核心重点在于find_tex...
file_menu.add_command(label="Close") # 添加分割线 file_menu.add_separator() file_menu.add_command(label="Exit") 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. activeforeground def build_file_menu(menu_bar): # 设置 活动前景色为红色,当鼠标移动到菜单项时,该菜单项的文字...
button = tk.Button(root, text="显示提示", command=show_message) button.pack() ``` 2. **菜单栏** 你可以为程序添加菜单栏,提供文件操作或帮助等功能: ```python menubar = tk.Menu(root) filemenu = tk.Menu(menubar, tearoff=0) filemenu.add_command(label="打开", command=show_message) ...