python圖形界面教程Tkinter詳解
1、Tkinter是什么
Tkinter 是使用 python 進(jìn)行窗口視窗設(shè)計(jì)的模塊。Tkinter模塊(“Tk 接口”)是Python的標(biāo)準(zhǔn)Tk GUI工具包的接口。作為 python 特定的GUI界面,是一個(gè)圖像的窗口,tkinter是python自帶的,可以編輯的GUI界面
2、Tkinter創(chuàng)建窗口
①導(dǎo)入 tkinter的庫(kù) ,創(chuàng)建并顯示窗口
import tkinter as tk # 在代碼里面導(dǎo)入庫(kù),起一個(gè)別名,以后代碼里面就用這個(gè)別名 root = tk.Tk() # 這個(gè)庫(kù)里面有Tk()這個(gè)方法,這個(gè)方法的作用就是創(chuàng)建一個(gè)窗口 root.mainloop() # 加上這一句,就可以看見(jiàn)窗口了,循環(huán)顯示窗口
②修改窗口屬性
root.title('演示窗口') #設(shè)置窗口標(biāo)題 root.geometry("300x100+630+80") # 設(shè)置窗口長(zhǎng)x寬+x*y
③創(chuàng)建按鈕
import tkinter as tk #tk代替tkinter from tkinter import messagebox root = tk.Tk() # 創(chuàng)建窗口 root.title('演示窗口') root.geometry("300x100+630+80") # 長(zhǎng)x寬+x*y btn1 = tk.Button(root) # 創(chuàng)建按鈕,并且將按鈕放到窗口里面 btn1["text"] = "點(diǎn)擊" # 給按鈕一個(gè)名稱(chēng) btn1.pack() # 按鈕布局 def test(e): '''創(chuàng)建彈窗''' messagebox.showinfo("窗口名稱(chēng)", "點(diǎn)擊成功") btn1.bind("<Button-1>", test) # 將按鈕和方法進(jìn)行綁定,也就是創(chuàng)建了一個(gè)事件 root.mainloop() # 讓窗口一直顯示,循環(huán)
④窗口內(nèi)的組件布局
3、Tkinter布局用法
①基本界面、label(標(biāo)簽)和button(按鈕)用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x200") var = tk.StringVar() #tk專(zhuān)有字符串 #root---界面(需要放在那個(gè)界面),textvariable----顯示文本 #bg----背景 font----字體格式,大小 width----寬度 height----高度 lab = tk.Label(root,textvariable=var,bg = 'green',font=('Arial',12),width=15,height=2) lab.pack() #把標(biāo)簽置入root界面布局 on_hit = True #點(diǎn)擊標(biāo)志位 def hit_me(): global on_hit if on_hit==True: on_hit = False #點(diǎn)擊交替 var.set("you hit me") else: on_hit = True var.set("") #root---界面(需要放在那個(gè)界面),text----顯示文本 #width----寬度 height----高度 command----命令(執(zhí)行哪個(gè)函數(shù)) but = tk.Button(root,text = "hitme",width=15,height=2,command=hit_me) but.pack() root.mainloop()
②entry(輸入)和text(文本)用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x200") #root---界面(需要放在那個(gè)界面),show----輸入的字符顯示為*,可以設(shè)置成show=none e = tk.Entry(root,show='*') e.pack() def insert_point(): var = e.get() t1.insert('insert',var) def insert_end(): var = e.get() t1.insert('end',var) #root---界面(需要放在那個(gè)界面),text----顯示文本 #width----寬度 height----高度 command----命令(執(zhí)行哪個(gè)函數(shù)) b1 = tk.Button(root,text = "insert point",width=15,height=2,command=insert_point) b1.pack() b2= tk.Button(root,text = "insert end",width=15,height=2,command=insert_end) b2.pack() #root---界面(需要放在那個(gè)界面),height ----文本框?qū)挾? t1 = tk.Text(root,height=2) t1.pack(fill="x") root.mainloop()
③var(變量)和list(列表)用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x200") #root---界面(需要放在那個(gè)界面),textvariable----顯示文本變量 #bg----背景 font----字體格式,大小 width----寬度 height----高度 var1 = tk.StringVar() l = tk.Label(root, bg='yellow', width=4, textvariable=var1) l.pack() def print_selection(): value = lb.get(lb.curselection()) #獲取列表選項(xiàng) var1.set(value) #設(shè)置textvariable=var1的文本值 #root---界面(需要放在那個(gè)界面),text----顯示文本 #width----寬度 height----高度 command----命令(執(zhí)行哪個(gè)函數(shù)) b1 = tk.Button(root,text = "print selection",width=15,height=2,command=print_selection) b1.pack() var2 = tk.StringVar() var2.set((11,22,33,44)) #root---界面(需要放在那個(gè)界面),listvariable----列表變量 lb = tk.Listbox(root, listvariable=var2) #列表變量var2 list_items = [1,2,3,4] for item in list_items: #列表遍歷 lb.insert('end', item) lb.insert(1, 'first') #1號(hào)位后加入first lb.insert(2, 'second') #2號(hào)位后加入second #lb.delete(2) #刪除2后的second lb.pack() root.mainloop()
④Radiobutton(選擇按鈕)用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x200") #root---界面(需要放在那個(gè)界面),textvariable----顯示文本變量 #bg----背景 font----字體格式,大小 width----寬度 height----高度 var = tk.StringVar() l = tk.Label(root, bg='yellow', width=20, text='empty') l.pack() def print_selection(): l.config(text='you have selected ' + var.get()) #Radiobutton-----選擇按鈕 #root---界面(需要放在那個(gè)界面),variable----變量 #value----值 command----命令 r1 = tk.Radiobutton(root, text='Option A', variable=var, value='A', command=print_selection) r1.pack() r2 = tk.Radiobutton(root, text='Option B', variable=var, value='B', command=print_selection) r2.pack() r3 = tk.Radiobutton(root, text='Option C', variable=var, value='C', command=print_selection) r3.pack() root.mainloop()
⑤Scale(尺度)用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x200") #root---界面(需要放在那個(gè)界面),textvariable----顯示文本變量 #bg----背景 font----字體格式,大小 width----寬度 height----高度 l = tk.Label(root, bg='yellow', width=20, text='empty') l.pack() def print_selection(v): l.config(text='you have selected ' + v) #Scale----尺度函數(shù) #root---界面(需要放在那個(gè)界面),label----標(biāo)簽名字 from_ ----尺度從哪開(kāi)始 to----尺度到哪結(jié)束 #orient----尺度方向(HORIZONTAL水平) length----顯示長(zhǎng)度(單位為像素) tickinterval----每隔多少加個(gè)尺度顯示 #resolution ----精度(精確到小數(shù)點(diǎn)后多少) command----命令 s = tk.Scale(root, label='try me', from_=5, to=11, orient=tk.HORIZONTAL, length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection) s.pack() root.mainloop()
⑥ Checkbutton(勾選項(xiàng))用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x200") #root---界面(需要放在那個(gè)界面),textvariable----顯示文本變量 #bg----背景 font----字體格式,大小 width----寬度 height----高度 l = tk.Label(root, bg='yellow', width=20, text='empty') l.pack() def print_selection(): if (var1.get() == 1) & (var2.get() == 0): l.config(text='I love only Python ') elif (var1.get() == 0) & (var2.get() == 1): l.config(text='I love only C++') elif (var1.get() == 0) & (var2.get() == 0): l.config(text='I do not love either') else: l.config(text='I love both') var1 = tk.IntVar() var2 = tk.IntVar() #Checkbutton----勾選函數(shù) #root---界面(需要放在那個(gè)界面),text----文本 variable ----變量 #onvalue----勾選標(biāo)志值 offvalue----無(wú)勾選標(biāo)志值 command----命令 c1 = tk.Checkbutton(root, text='Python', variable=var1, onvalue=1, offvalue=0, command=print_selection) c2 = tk.Checkbutton(root, text='C++', variable=var2, onvalue=1, offvalue=0, command=print_selection) c1.pack() c2.pack() root.mainloop()
⑦canvas(畫(huà)布)用法
import tkinter as tk root = tk.Tk() root.title("這是Demo") root.geometry("300x300") #Canvas----畫(huà)布 #root---界面(需要放在那個(gè)界面) #bg----背景 width----寬度,height----高度(單位像素) canvas = tk.Canvas(root, bg='white', height=200, width=200) #PhotoImage----加載圖片 #圖片路徑為file='C:\\Users\\admin\\Desktop\\Demo\\Demo\\1.gif' 注意是使用雙斜桿\\ image_file = tk.PhotoImage(file='C:\\Users\\admin\\Desktop\\Demo\\Demo\\1.gif') #create_image----圖片位置 #create_image(瞄點(diǎn)橫坐標(biāo),瞄點(diǎn)縱坐標(biāo),瞄點(diǎn)位置,加入圖片) image = canvas.create_image(0,0, anchor='nw', image=image_file) #create_line----畫(huà)線 #create_oval----畫(huà)圓 #create_arc----畫(huà)扇形 #create_rectangle----畫(huà)矩形 #canvas.create_oval x0, y0, x1, y1= 50, 50, 80, 80 line = canvas.create_line(x0, y0, x1, y1) #create_oval----畫(huà)扇形 #create_oval(坐標(biāo),坐標(biāo),坐標(biāo),坐標(biāo),填充顏色) oval = canvas.create_oval(x0, y0, x1, y1, fill='red') #create_oval----畫(huà)圓 #create_oval(坐標(biāo),坐標(biāo),坐標(biāo),坐標(biāo),開(kāi)始角度,結(jié)束角度) arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=90, extent=180) rect = canvas.create_rectangle(100, 30, 100+20, 30+20) canvas.pack() #move----移動(dòng) #canvas.move(對(duì)象, 橫坐標(biāo), 縱坐標(biāo)) def moveit(): canvas.move(rect, 2, 4) b = tk.Button(root, text='move', command=moveit).pack() root.mainloop()
⑧menubar(菜單欄)
import tkinter as tk root = tk.Tk() root.title('my window') root.geometry('300x300') #root---界面(需要放在那個(gè)界面),textvariable----顯示文本變量 #bg----背景 font----字體格式,大小 width----寬度 height----高度 l = tk.Label(root, text='', bg='yellow') l.pack() counter = 0 def do_job(): global counter l.config(text='do '+ str(counter)) counter+=1 #menubar----菜單 menubar = tk.Menu(root) #菜單加入root界面 filemenu = tk.Menu(menubar, tearoff=0) #filemenu加入菜單欄menubar下(tearoff=0 不可分割) #filemenu菜單加入標(biāo)簽和相應(yīng)執(zhí)行命令 menubar.add_cascade(label='File', menu=filemenu) filemenu.add_command(label='New', command=do_job) filemenu.add_command(label='Open', command=do_job) filemenu.add_command(label='Save', command=do_job) filemenu.add_separator() #添加分割線 filemenu.add_command(label='Exit', command=root.quit) editmenu = tk.Menu(menubar, tearoff=0)#editmenu加入菜單menubar下(tearoff=0 不可分割) #editmenu菜單加入標(biāo)簽和相應(yīng)執(zhí)行命令 menubar.add_cascade(label='Edit', menu=editmenu) editmenu.add_command(label='Cut', command=do_job) editmenu.add_command(label='Copy', command=do_job) editmenu.add_command(label='Paste', command=do_job) #submenu加入菜單filemenu下(tearoff=0 不可分割) submenu = tk.Menu(filemenu) #filemenu菜單加入標(biāo)簽和相應(yīng)執(zhí)行命令 filemenu.add_cascade(label='Import', menu=submenu, underline=0) submenu.add_command(label="Submenu1", command=do_job) #配置菜單欄 root.config(menu=menubar) root.mainloop()
⑨Frame(架構(gòu))
import tkinter as tk root = tk.Tk() root.title('my window') root.geometry('200x200') #root---界面(需要放在那個(gè)界面),textvariable----顯示文本變量 #bg----背景 font----字體格式,大小 width----寬度 height----高度 tk.Label(root, text='on the window').pack() #Frame----框架 #Frame的作用可以合理分布控件位置 #框架設(shè)置在root界面上 frm = tk.Frame(root) frm.pack() #框架設(shè)置在Frame框架上(分別在左右) frm_l = tk.Frame(frm) frm_r = tk.Frame(frm) frm_l.pack(side='left') frm_r.pack(side='right') tk.Label(frm_l, text='on the frm_l1').pack() tk.Label(frm_l, text='on the frm_l2').pack() tk.Label(frm_r, text='on the frm_r1').pack() root.mainloop()
⑩messagebox(彈窗)
import tkinter as tk import tkinter.messagebox root = tk.Tk() root.title('my window') root.geometry('200x200') def hit_me(): #tk.messagebox.showinfo(title='Hi', message='hahahaha') # return 'ok' #tk.messagebox.showwarning(title='Hi', message='nononono') #警告 return 'ok' tk.messagebox.showerror(title='Hi', message='No!! never') #錯(cuò)誤 return 'ok' #print(tk.messagebox.askquestion(title='Hi', message='hahahaha')) #詢問(wèn) return 'yes' , 'no' #print(tk.messagebox.askyesno(title='Hi', message='hahahaha')) #是否 return True, False #print(tk.messagebox.askretrycancel(title='Hi', message='hahahaha')) # 重試 return True, False #print(tk.messagebox.askokcancel(title='Hi', message='hahahaha')) # return True, False # print(tk.messagebox.askyesnocancel(title="Hi", message="haha")) # return, True, False, None tk.Button(root,text = 'hit_me',command=hit_me).pack() root.mainloop()
⑾pack、grid以及place布局用法
import tkinter as tk root = tk.Tk() root.title('my window') root.geometry('200x200') #canvas = tk.Canvas(window, height=150, width=500) #canvas.grid(row=1, column=1) #image_file = tk.PhotoImage(file='welcome.gif') #image = canvas.create_image(0, 0, anchor='nw', image=image_file) #pack布局----可以通過(guò)side控制放在上下左右 #tk.Label(window, text='1').pack(side='top') #tk.Label(window, text='1').pack(side='bottom') #tk.Label(window, text='1').pack(side='left') #tk.Label(window, text='1').pack(side='right') #grid布局----通過(guò)方格的格式,將控件分布于其中 ##for i in range(4): #for j in range(3): #tk.Label(window, text=1).grid(row=i, column=j, padx=10, pady=10) #place布局----通過(guò)設(shè)置坐標(biāo)方式,可以精確分布控件 tk.Label(root, text=1).place(x=20, y=10, anchor='nw') root.mainloop()
總結(jié)
到此這篇關(guān)于python圖形界面教程Tkinter詳解的文章就介紹到這了,更多相關(guān)python Tkinter詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中for循環(huán)可迭代對(duì)象迭代器及生成器源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python中for循環(huán)可迭代對(duì)象迭代器及生成器的源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python 把兩層列表展開(kāi)平鋪成一層(5種實(shí)現(xiàn)方式)
這篇文章主要介紹了Python 把兩層列表展開(kāi)平鋪成一層(5種實(shí)現(xiàn)方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04python編程之requests在網(wǎng)絡(luò)請(qǐng)求中添加cookies參數(shù)方法詳解
這篇文章主要介紹了python編程之requests在網(wǎng)絡(luò)請(qǐng)求中添加cookies參數(shù)方法詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10pycharm 多行批量縮進(jìn)和反向縮進(jìn)快捷鍵介紹
這篇文章主要介紹了pycharm 多行批量縮進(jìn)和反向縮進(jìn)快捷鍵介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢(shì)進(jìn)行檢測(cè)功能實(shí)現(xiàn)
2022年奧林匹克運(yùn)動(dòng)會(huì)如期舉行,以不正確的方式進(jìn)行運(yùn)動(dòng)風(fēng)險(xiǎn)在增加,人體姿勢(shì)估計(jì)是計(jì)算機(jī)視覺(jué)領(lǐng)域的重要問(wèn)題,接下來(lái)通過(guò)本文給大家介紹下使用OpenCV對(duì)運(yùn)動(dòng)員的姿勢(shì)進(jìn)行檢測(cè)功能,感興趣的朋友一起看看吧2022-02-02python的scipy實(shí)現(xiàn)插值的示例代碼
這篇文章主要介紹了python的scipy實(shí)現(xiàn)插值的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python利用 pytesseract快速識(shí)別提取圖片中的文字((圖片識(shí)別)
本文介紹了tesseract的python調(diào)用,也就是pytesseract庫(kù),其中還有一些其他的內(nèi)容并沒(méi)有涉及,僅涉及到了圖片提取文字,如果你對(duì)其感興趣,可以深入探索一下,也希望能和我探討一下2022-11-11Pandas.DataFrame刪除指定行和列(drop)的實(shí)現(xiàn)
本文主要介紹了Pandas.DataFrame刪除指定行和列(drop)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02