23.圖形互動tkinter--menu
- 環境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("<Button-3>",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("<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()
如有不當之處望不吝指出,在此由衷感謝,望共同學習,也希望我的寫作能幫到更多喜歡python程式設計的新人,謝謝!
相關文章
- GoJS互動式圖形庫常用的方法(二)GoJS
- halcon運算元之如何畫各種互動ROI圖形
- 23.【Reverse】
- 23. 迭代器
- 23. 元類
- 投影互動牆的三大實現形態介紹
- 互動視訊 :新形態,新價值(附下載)
- 體感互動照片牆,大屏互動圖片牆軟體方案
- (.net core環境下)圖形驗證,人機互動,一個不夠我給你兩個
- 畫影圖形: SVG & Canvas 圖形對比SVGCanvas
- 互動視訊不能為了互動而互動
- 23. Merge K Soerted List
- vis.js可動的3d圖形JS3D
- 箱形圖(python畫圖)Python
- echarts環形圖Echarts
- 繪製圖形
- 基於動態圖互動網路的多意圖口語語言理解框架框架
- 《隱形守護者》銷售過億,互影獲上億融資,互動作品的時機到了?
- flask+pyecharts繪製的圖表增加互動FlaskEcharts
- SmartRules讓MindManager的互動圖變得更加智慧
- 23. 人物基類程式碼
- 【Leetcode】23. Merge k Sorted ListsLeetCode
- 百度地圖開發-與地圖的互動功能 06地圖
- ubuntu下圖形程式自啟動的幾種方法Ubuntu
- CSS3的過渡,動畫,圖形轉換CSSS3動畫
- 互動媒體日常——互動漫畫之對話方塊互動
- 圖形的認識
- java:繪製圖形Java
- 圖形驗證碼
- Drawable圖形定製
- Java的圖形列印Java
- matplotlib繪製圖形
- CAD列印圖形教程
- css 畫圖形大全CSS
- python 圖形初學Python
- Python|Python互動之mongoDB互動詳解PythonMongoDB
- 《隱形守護者》之後,那些火不起來的真人互動遊戲遊戲
- 兩年了,《隱形守護者》之後為何再無互動電影?