- 环境python3.x 3.x之后tkinter自带,jupyter notebook/pycharm
最简单的菜单实例
显示两个菜单窗口,hello,quit,点击hello时打印你好,点击quit时推出程序,直接上代码。
from tkinter import *
root =Tk()
def callback():
print("你好!")
menubar =Menu(root)#创建菜单
menubar.add_command(label ="hello", command =callback)#创建menu菜单显示及点击后返回命令command函数指定
menubar.add_command(label ="quit", command =root.quit)#root.quit是退出tkinter环境,当想退出图形交互窗口时用。
root.config(menu = menubar)
mainloop()
执行情况:
实例2--鼠标右键弹出菜单,直接上代码。
from tkinter import *
root =Tk()
def callback():
print("你好!")
menubar =Menu(root)
menubar.add_command(label ="hello", command =callback)
menubar.add_command(label ="quit", command =root.quit)
frame =Frame(root, width =512, height =512)#tkinter中运用Frame来设定图形交互界面尺寸大小
frame.pack()
def popup(event):
menubar.post(event.x_root,event.y_root)
frame.bind("",popup)#运用bind()将函数popup与鼠标右键绑定
mainloop()
执行情况:点击右键时出现菜单,点击分割线显示独立窗口。
到这里,你将可以做出这样的界面了。
实现菜单内套菜单不在是梦,你也可以做自己的小软件了。
代码如下:
from tkinter import *
import os.path
try:
from ttk import Button, Scrollbar
except ImportError:
pass
root =Tk()
root.title('EditSave')
e =Entry(root) #输入框赋值在e变量
e.pack(padx=5,pady=5)
e.delete(0,END)
e.insert(0,"默认文本...") #插入序号为0,后为字符串
def show():
print("open:%s"%e.get())
def dele():
e.delete(0,END)
theButton1 = Button(root, text ="Open", width =10,command =show)
theButton2 = Button(root, text ="Cancl",width =10,command =dele)
theButton1.pack(padx=0,pady =0)
theButton2.pack(padx=0,pady =0)
sb =Scrollbar(root)
sb.pack(side =RIGHT,fill =Y)
#text
text =Text(root, width =100, height =50,yscrollcommand =sb.set)
text.pack(side =LEFT, fill =BOTH)
def popup(event):
menubar.post(event.x_root,event.y_root)
text.bind("",popup)
sb.config(command =text.yview)
def __open():
pass
def callback():
pass
def __save():
pass
def Newfile():
root =Tk()
text =Text(root, width =100, height =50)
text.pack()
menubar =Menu(root, bg='purple')
#File
filemenu =Menu(menubar)
filemenu.add_command(label ="New file Ctrl+N", command =Newfile)
filemenu.add_command(label ="open Ctrl+O", command =__open)
filemenu.add_command(label ="open Module Ctrl+M", command =callback)
filemenu.add_command(label ="Recent file", command =callback)
filemenu.add_command(label ="open", command =callback)
filemenu.add_command(label ="Class Broser Alt+C", command =callback)
filemenu.add_command(label ="Path Broser", command =callback)
filemenu.add_separator()#分割线
filemenu.add_command(label ="Save Ctrl+S", command =__save)
filemenu.add_command(label ="Save As Ctrl+Shift+S", command =callback)
filemenu.add_command(label ="Save Copy As Alt+Shift+S", command =callback)
filemenu.add_separator()#分割线
filemenu.add_command(label ="Close Alt+F4", command =root.quit)
menubar.add_cascade(label ="File", menu = filemenu)#cascade菜单
#Edit
editmenu =Menu(menubar,tearoff =False)
editmenu.add_command(label ="Cut", command =callback)
editmenu.add_command(label ="Copy", command =callback)
editmenu.add_command(label ="Past", command =callback)
editmenu.add_separator()#分割线
editmenu.add_command(label ="Undo", command =callback)
editmenu.add_command(label ="Redo", command =callback)
menubar.add_cascade(label ="Edit", menu = editmenu)#cascade菜单
#Format
formatmenu =Menu(menubar,tearoff =False)
formatmenu.add_command(label ="Indent Region", command =callback)
formatmenu.add_command(label ="Dedent Region", command =callback)
formatmenu.add_command(label ="Comment Out Region", command =callback)
formatmenu.add_separator()#分割线
formatmenu.add_command(label ="Strio Trailing Whitespace", command =callback)
menubar.add_cascade(label ="Format", menu = formatmenu)#cascade菜单
#Run
runmenu =Menu(menubar,tearoff =False)
runmenu.add_command(label ="Python shell", command =callback)
runmenu.add_separator()#分割线
runmenu.add_command(label ="Check Modle", command =callback)
runmenu.add_command(label ="Run Modle", command =callback)
menubar.add_cascade(label ="Run", menu = runmenu)#cascade菜单
#Options
optionsmenu =Menu(menubar,tearoff =False)
optionsmenu.add_command(label ="Configure IDLE", command =callback)
optionsmenu.add_separator()#分割线
optionsmenu.add_command(label ="Code Context", command =callback)
menubar.add_cascade(label ="Optionsmenu", menu = optionsmenu)#cascade菜单
#Window
windowmenu =Menu(menubar,tearoff =False)
windowmenu.add_command(label ="Zoom Height", command =callback)
windowmenu.add_separator()#分割线
windowmenu.add_command(label ="*Python 3.5.2 shell*", command =callback)
menubar.add_cascade(label ="Window", menu = windowmenu)#cascade菜单
#Help
helpmenu =Menu(menubar,tearoff =False)
helpmenu.add_command(label ="About IDLE", command =callback)
helpmenu.add_separator()#分割线
helpmenu.add_command(label ="IDLE HELP", command =callback)
helpmenu.add_command(label ="Python dcos", command =callback)
helpmenu.add_command(label ="Turle Demo", command =callback)
menubar.add_cascade(label ="Help", menu = helpmenu)#cascade菜单
root.config(menu = menubar)
mainloop()
menubar =Menu(root,bg="red")
#File
filemenu =Menu(menubar)
filemenu.add_command(label ="New file Ctrl+N", command =Newfile)
filemenu.add_command(label ="open Ctrl+O", command =__open)
filemenu.add_command(label ="open Module Ctrl+M", command =callback)
filemenu.add_command(label ="Recent file", command =callback)
filemenu.add_command(label ="open", command =callback)
filemenu.add_command(label ="Class Broser Alt+C", command =callback)
filemenu.add_command(label ="Path Broser", command =callback)
filemenu.add_separator()#分割线
filemenu.add_command(label ="Save Ctrl+S", command =__save)
filemenu.add_command(label ="Save As Ctrl+Shift+S", command =callback)
filemenu.add_command(label ="Save Copy As Alt+Shift+S", command =callback)
filemenu.add_separator()#分割线
filemenu.add_command(label ="Close Alt+F4", command =root.quit)
menubar.add_cascade(label ="File", menu = filemenu)#cascade菜单
#Edit
editmenu =Menu(menubar,tearoff =False)
editmenu.add_command(label ="Cut", command =callback)
editmenu.add_command(label ="Copy", command =callback)
editmenu.add_command(label ="Past", command =callback)
editmenu.add_separator()#分割线
editmenu.add_command(label ="Undo", command =callback)
editmenu.add_command(label ="Redo", command =callback)
menubar.add_cascade(label ="Edit", menu = editmenu)#cascade菜单
#Format
formatmenu =Menu(menubar,tearoff =False)
formatmenu.add_command(label ="Indent Region", command =callback)
formatmenu.add_command(label ="Dedent Region", command =callback)
formatmenu.add_command(label ="Comment Out Region", command =callback)
formatmenu.add_separator()#分割线
formatmenu.add_command(label ="Strio Trailing Whitespace", command =callback)
menubar.add_cascade(label ="Format", menu = formatmenu)#cascade菜单
#Run
runmenu =Menu(menubar,tearoff =False)
runmenu.add_command(label ="Python shell", command =callback)
runmenu.add_separator()#分割线
runmenu.add_command(label ="Check Modle", command =callback)
runmenu.add_command(label ="Run Modle", command =callback)
menubar.add_cascade(label ="Run", menu = runmenu)#cascade菜单
#Options
optionsmenu =Menu(menubar,tearoff =False)
optionsmenu.add_command(label ="Configure IDLE", command =callback)
optionsmenu.add_separator()#分割线
optionsmenu.add_command(label ="Code Context", command =callback)
menubar.add_cascade(label ="Optionsmenu", menu = optionsmenu)#cascade菜单
#Window
windowmenu =Menu(menubar,tearoff =False)
windowmenu.add_command(label ="Zoom Height", command =callback)
windowmenu.add_separator()#分割线
windowmenu.add_command(label ="*Python 3.5.2 shell*", command =callback)
menubar.add_cascade(label ="Window", menu = windowmenu)#cascade 菜单
#Help
helpmenu =Menu(menubar,tearoff =False)
helpmenu.add_command(label ="About IDLE", command =callback)
helpmenu.add_separator()#分割线
helpmenu.add_command(label ="IDLE HELP", command =callback)
helpmenu.add_command(label ="Python dcos", command =callback)
helpmenu.add_command(label ="Turle Demo", command =callback)
menubar.add_cascade(label ="Help", menu = helpmenu)#cascade菜单
root.config(menu = menubar)
mainloop()
menuButton实例,当你要设置一个按钮,并将菜单赋予这个按钮,那么久要用到menubutton方法了,代码如下:
from tkinter import *
root =Tk()
root.title("Menubutton")
def callback():
print("你好!")
mb =Menubutton(root, text="点我", relief =RAISED)
mb.pack()
openVar = IntVar()
saveVar = IntVar()
quitVar = IntVar()
filemenu = Menu(mb,tearoff =True)
filemenu.add_checkbutton(label ="Open", command =callback,variable =openVar)
filemenu.add_checkbutton(label ="Save", command =callback,variable =saveVar)
filemenu.add_separator()
filemenu.add_checkbutton(label ="Quit", command =callback,variable =quitVar)
mb.config(menu =filemenu)
mainloop()
如有不当之处望不吝指出,在此由衷感谢,望共同学习,也希望我的写作能帮到更多喜欢python编程的新人,谢谢!