23.圖形互動tkinter--menu

weixin_33724059發表於2018-09-06
  • 環境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()

執行情況:


8730384-a07d12fcaa228c22.png
menu1

例項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("<Button-3>",popup)#運用bind()將函式popup與滑鼠右鍵繫結

mainloop()

執行情況:點選右鍵時出現選單,點選分割線顯示獨立視窗。


8730384-d4be338651fa74d0.png
menu2

到這裡,你將可以做出這樣的介面了。

8730384-e163e27799c7c67e.png
menu3

實現選單內套選單不在是夢,你也可以做自己的小軟體了。


8730384-eb4b999e0d1652f9.png
menu4

程式碼如下:

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("<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例項,當你要設定一個按鈕,並將選單賦予這個按鈕,那麼久要用到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()
8730384-3f12d0e15bb5503e.png
menu5

如有不當之處望不吝指出,在此由衷感謝,望共同學習,也希望我的寫作能幫到更多喜歡python程式設計的新人,謝謝!

相關文章