- 環(huán)境python3.x 3.x之后tkinter自帶璃吧,jupyter notebook/pycharm
最簡單的菜單實例
顯示兩個菜單窗口,hello俯树,quit帘腹,點擊hello時打印你好,點擊quit時推出程序许饿,直接上代碼阳欲。
from tkinter import *
root =Tk()
def callback():
print("你好!")
menubar =Menu(root)#創(chuàng)建菜單
menubar.add_command(label ="hello", command =callback)#創(chuàng)建menu菜單顯示及點擊后返回命令command函數(shù)指定
menubar.add_command(label ="quit", command =root.quit)#root.quit是退出tkinter環(huán)境陋率,當(dāng)想退出圖形交互窗口時用胸完。
root.config(menu = menubar)
mainloop()
執(zhí)行情況:
實例2--鼠標(biāo)右鍵彈出菜單,直接上代碼翘贮。
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來設(shè)定圖形交互界面尺寸大小
frame.pack()
def popup(event):
menubar.post(event.x_root,event.y_root)
frame.bind("<Button-3>",popup)#運用bind()將函數(shù)popup與鼠標(biāo)右鍵綁定
mainloop()
執(zhí)行情況:點擊右鍵時出現(xiàn)菜單,點擊分割線顯示獨立窗口。
到這里狸页,你將可以做出這樣的界面了锨能。
實現(xiàn)菜單內(nèi)套菜單不在是夢,你也可以做自己的小軟件了芍耘。
代碼如下:
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,"默認(rèn)文本...") #插入序號為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("<Button-3>",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實例,當(dāng)你要設(shè)置一個按鈕斋竞,并將菜單賦予這個按鈕倔约,那么久要用到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()
如有不當(dāng)之處望不吝指出浸剩,在此由衷感謝,望共同學(xué)習(xí)鳄袍,也希望我的寫作能幫到更多喜歡python編程的新人绢要,謝謝!