python?Tkinter模塊使用方法詳解
一、前言
1.1、Tkinter是什么
Tkinter 是使用 python 進(jìn)行窗口視窗設(shè)計的模塊。Tkinter模塊(“Tk 接口”)是Python的標(biāo)準(zhǔn)Tk GUI工具包的接口。作為 python 特定的GUI界面,是一個圖像的窗口,tkinter是python自帶的,可以編輯的GUI界面,用來入門,熟悉窗口視窗的使用,非常有必要。
二、準(zhǔn)備工作
2.1、Windows演示環(huán)境搭建
- 安裝python3.7
- 安裝編輯器,演示使用的Visual Studio Code
三、Tkinter創(chuàng)建窗口
3.1、創(chuàng)建出一個窗口
首先我們導(dǎo)入tkinter的庫
import tkinter as tk # 在代碼里面導(dǎo)入庫,起一個別名,以后代碼里面就用這個別名 root = tk.Tk() # 這個庫里面有Tk()這個方法,這個方法的作用就是創(chuàng)建一個窗口
如果只是執(zhí)行以上的兩句代碼,運(yùn)行程序是沒有反應(yīng)的,因為只有一個主函數(shù),從上到下執(zhí)行完就沒有了,這個窗口也是很快就消失了,所以現(xiàn)在我們要做的就是讓窗口一直顯示,那么我們就可以加一個循環(huán)
創(chuàng)建的窗口的名字是root ,那么我們之后使用這個root就可以操作這個窗口了。
root.mainloop() # 加上這一句,就可以看見窗口了
執(zhí)行以上的3句代碼,我們就可以看見窗口了
3.2、給窗口取一個標(biāo)題
root.title('演示窗口')
3.3、窗口設(shè)置
通過以下代碼,我們可以給窗口設(shè)置長寬以及窗口在屏幕的位置
root.geometry("300x100+630+80") # 長x寬+x*y
3.3、創(chuàng)建按鈕,并且給按鈕添加點(diǎn)擊事件
這個庫里面有一個方法Button(),只要調(diào)用這個方法,我們就可以創(chuàng)建這個組件,創(chuàng)建的這個組件我們賦值給一個常量,以后我們就可以用這個常量來操作這個按鈕,這個方法里面的參數(shù),就是要我們寫窗口的名字
Button(root) 這樣寫的意思就是 將我們創(chuàng)建的按鈕放到這個窗口上面
btn1 = tk.Button(root)
給按鈕取一個名稱
btn1["text"] = "點(diǎn)擊"
我們創(chuàng)建的按鈕組件,已經(jīng)放到窗口里面了,但是放到窗口的哪個位置,東南西北哪個地方,我們就可以用pack()去定位(后面會介紹其它定位方法)
btn1.pack() # 按鈕在窗口里面的定位
創(chuàng)建點(diǎn)擊按鈕事件的彈窗,先導(dǎo)入messagebox,這個必須單獨(dú)導(dǎo)入
from tkinter import messagebox def test(e): messagebox.showinfo("窗口名稱","點(diǎn)擊成功")
現(xiàn)在有了按鈕,有了方法,我想要做的是一點(diǎn)擊按鈕,就執(zhí)行這個方法,那么就需要將按鈕和方法進(jìn)行綁定
btn1.bind("<Button-1>",test) #第一個參數(shù)為:按鼠標(biāo)左鍵的事件 第二個參數(shù)為:要執(zhí)行的方法的名字
按鈕組件里面有一個方法bind() 這個方法就可以實(shí)現(xiàn)綁定
完整代碼
import tkinter as tk from tkinter import messagebox root = tk.Tk() # 創(chuàng)建窗口 root.title('演示窗口') root.geometry("300x100+630+80") # 長x寬+x*y btn1 = tk.Button(root) # 創(chuàng)建按鈕,并且將按鈕放到窗口里面 btn1["text"] = "點(diǎn)擊" # 給按鈕一個名稱 btn1.pack() # 按鈕布局 def test(e): '''創(chuàng)建彈窗''' messagebox.showinfo("窗口名稱", "點(diǎn)擊成功") btn1.bind("<Button-1>", test) # 將按鈕和方法進(jìn)行綁定,也就是創(chuàng)建了一個事件 root.mainloop() # 讓窗口一直顯示,循環(huán)
3.4、窗口內(nèi)的組件布局
3種布局管理器:pack - grid - place
pack
這個布局管理器,要么將組件垂直的排列,要么水平的排列
grid
Grid(網(wǎng)格)布局管理器會將控件放置到一個二維的表格里。
主控件被分割成一系列的行和列,表格中的每個單元(cell)都可以放置一個控件。
選項 | 說明 |
---|---|
column | 單元格的列號,從0開始的正整數(shù) |
columnspan | 跨列,跨越的列數(shù),正整數(shù) |
row | 單元格的行號, 從0開始的正整數(shù) |
rowspan | 跨行,跨越的行數(shù),正整數(shù) |
ipadx, ipady | 設(shè)置子組件之間的間隔,x方向或y方向,默認(rèn)單位為像素,非浮點(diǎn)數(shù),默認(rèn)0.0 |
padx, pady | 與之并列的組件之間的間隔,x方向或y方向,默認(rèn)單位為像素,非浮點(diǎn)數(shù),默認(rèn)0.0 |
sticky | 組件緊貼所在的單元格的某一腳,對應(yīng)于東南西北中以及4個角。東 = “e”,南=“s”,西=“w”,北=“n”,“ne”,“se”,“sw”, “nw”; |
grid_info() 查看組件默認(rèn)的參數(shù)
import tkinter as tk root = tk.Tk() # 默認(rèn)按鈕 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid() print(btn1.grid_info()) root.title('演示窗口') root.geometry("300x150+1000+300") root.mainloop()
column 指定控件所在的列
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(column=0) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(column=1) root.title('演示窗口') root.geometry("300x100+1000+300") root.mainloop()
- columnspan 指定每個控件橫跨的列數(shù)
- 什么是columnspan?
類似excel的合并單元格
a占了兩個格子的寬度,colunmspan就是2
```python import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(column=0, columnspan=2) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(column=1, columnspan=1) root.title('演示窗口') root.geometry("300x100+1000+300") root.mainloop()
row 指定控件所在的行
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(row=0) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(row=1) # 按鈕3 btn3 = tk.Button(root) btn3["text"] = "按鈕2" btn3.grid(row=2) root.title('演示窗口') root.geometry("300x100+1000+300") root.mainloop()
- rowspan 指定每個控件橫跨的行數(shù)
- 什么是rowspan ?
類似excel的合并單元格
a占了兩個格子的高度,rowspan就是2
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(row=0, rowspan=2) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(row=2, rowspan=1) root.title('演示窗口') root.geometry("300x100+1000+300") root.mainloop()
ipadx 水平方向內(nèi)邊距
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(ipadx=20) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(ipadx=5) root.title('演示窗口') root.geometry("300x100+1000+300") root.mainloop()
ipady 垂直方向內(nèi)邊距
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(ipady=20) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(ipady=5) root.title('演示窗口') root.geometry("300x150+1000+300") root.mainloop()
padx 水平方向外邊距
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(padx=50) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(column=1, padx=20) root.title('演示窗口') root.geometry("300x150+1000+300") root.mainloop()
pady 垂直方向外邊距
import tkinter as tk root = tk.Tk() # 按鈕1 btn1 = tk.Button(root) btn1["text"] = "按鈕1" btn1.grid(pady=30) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(pady=20) root.title('演示窗口') root.geometry("300x150+1000+300") root.mainloop()
sticky 組件東南西北的方向
import tkinter as tk root = tk.Tk() # 默認(rèn)按鈕 btn1 = tk.Button(root) btn1["text"] = "默認(rèn)按鈕演示效果" btn1.grid(ipadx=50) # 按鈕2 btn2 = tk.Button(root) btn2["text"] = "按鈕2" btn2.grid(row=1, sticky="w") # 按鈕3 btn3 = tk.Button(root) btn3["text"] = "按鈕3" btn3.grid(row=1, sticky="e") root.title('演示窗口') root.geometry("300x150+1000+300") root.mainloop()
place布局管理器
place布局管理器可以通過坐標(biāo)精確控制組件的位置,適用于一些布局更加靈活的場景
選項 | 說明 |
---|---|
x,y | 組件左上角的絕對坐標(biāo)(相當(dāng)于窗口) |
relx ,rely | 組件左上角的坐標(biāo)(相對于父容器) |
width , height | 組件的寬度和高度 |
relwidth , relheight | 組件的寬度和高度(相對于父容器) |
anchor | 對齊方式,左對齊“w”,右對齊“e”,頂對齊“n”,底對齊“s” |
import tkinter as tk root = tk.Tk() but1 = tk.Button(root, text="按鈕1") but1.place(relx=0.2, x=100, y=20, relwidth=0.2, relheight=0.5) root.title('演示窗口') root.geometry("300x150+1000+300") root.mainloop()
四、Tkinter基本控件介紹
4.1、封裝
import tkinter as tk class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("500x200+1100+150") self.interface() def interface(self): """"界面編寫位置""" pass if __name__ == '__main__': a = GUI() a.root.mainloop()
4.2、文本顯示_Label
def interface(self): """"界面編寫位置""" self.Label0 = tk.Label(self.root, text="文本顯示") self.Label0.grid(row=0, column=0)
4.3、按鈕顯示_Button
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="按鈕顯示") self.Button0.grid(row=0, column=0)
4.4、輸入框顯示_Entry
def interface(self): """"界面編寫位置""" self.Entry0 = tk.Entry(self.root) self.Entry0.grid(row=0, column=0)
4.5、文本輸入框顯示_Text
# pack布局 def interface(self): """"界面編寫位置""" self.w1 = tk.Text(self.root, width=80, height=10) self.w1.pack(pady=0, padx=30) # grid布局 def interface(self): """"界面編寫位置""" self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=1, column=0)
4.6、復(fù)選按鈕_Checkbutton
def interface(self): """"界面編寫位置""" self.Checkbutton01 = tk.Checkbutton(self.root, text="名稱") self.Checkbutton01.grid(row=0, column=2)
4.7、單選按鈕_Radiobutton
def interface(self): """"界面編寫位置""" self.Radiobutton01 = tk.Radiobutton(self.root, text="名稱") self.Radiobutton01.grid(row=0, column=2)
五、組件使用方法介紹
5.1、按鈕(Button)綁定事件
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="運(yùn)行", command=self.event) self.Button0.grid(row=0, column=0) self.Button1 = tk.Button(self.root, text="退出", command=self.root.destroy, bg="Gray") # bg=顏色 self.Button1.grid(row=0, column=1, sticky="e", ipadx=10) def event(self): """按鈕事件""" print("運(yùn)行成功")
5.2、輸入框(Entry)內(nèi)容獲取
def interface(self): """"界面編寫位置""" self.entry00 = tk.StringVar() self.entry00.set("默認(rèn)信息") self.entry0 = tk.Entry(self.root, textvariable=self.entry00) self.entry0.grid(row=1, column=0) self.Button0 = tk.Button(self.root, text="運(yùn)行", command=self.event) self.Button0.grid(row=0, column=0) def event(self): """按鈕事件,獲取文本信息""" a = self.entry00.get() print(a)
5.2、文本輸入框(Text),寫入文本信息和清除文本信息
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="清除", command=self.event) self.Button0.grid(row=0, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=1, column=0) self.w1.insert("insert", "默認(rèn)信息") def event(self): '''清空輸入框''' self.w1.delete(1.0, "end")
5.3、獲取復(fù)選按鈕(Checkbutton)的狀態(tài)
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定", command=self.event) self.Button0.grid(row=0, column=0) self.v1 = tk.IntVar() self.Checkbutton01 = tk.Checkbutton(self.root, text="復(fù)選框", command=self.Check_box, variable=self.v1) self.Checkbutton01.grid(row=1, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=2, column=0) def event(self): '''按鈕事件,獲取復(fù)選框的狀態(tài),1表示勾選,0表示未勾選''' a = self.v1.get() self.w1.insert(1.0, str(a)+'\n') def Check_box(self): '''復(fù)選框事件''' if self.v1.get() == 1: self.w1.insert(1.0, "勾選"+'\n') else: self.w1.insert(1.0, "未勾選"+'\n')
5.4、清除控件
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定", command=self.event) self.Button0.grid(row=0, column=0) self.Label0 = tk.Label(self.root, text="文本顯示") self.Label0.grid(row=1, column=0) self.Entry0 = tk.Entry(self.root) self.Entry0.grid(row=2, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=3, column=0) def event(self): '''按鈕事件,清除Label、Entry、Text組件''' a = [self.Label0, self.Entry0, self.w1] for i in a: i.grid_forget()
5.5、清除復(fù)選框勾選狀態(tài)
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定", command=self.event) self.Button0.grid(row=0, column=0) self.v1 = tk.IntVar() self.Checkbutton01 = tk.Checkbutton(self.root, text="復(fù)選框", command=self.Check_box, variable=self.v1) self.Checkbutton01.grid(row=1, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=2, column=0) def event(self): '''按鈕事件,清除復(fù)選框勾選狀態(tài)''' self.Checkbutton01.deselect() def Check_box(self): '''復(fù)選框事件''' if self.v1.get() == 1: self.w1.insert(1.0, "勾選"+'\n') else: self.w1.insert(1.0, "未勾選"+'\n')
5.6、文本框(Text)內(nèi)容獲取
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定", command=self.event) self.Button0.grid(row=0, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=1, column=0) def event(self): a = self.w1.get('0.0', 'end') print(a)
六、Tkinter使用多線程
6.1、為什么要使用多線程
以下為單線程運(yùn)行
def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定", command=self.event) self.Button0.grid(row=0, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=1, column=0) def event(self): '''按鈕事件,一直循環(huán)''' a = 0 while True: a += 1 self.w1.insert(1.0, str(a)+'\n')
單線程下,主線程需要運(yùn)行窗口,如果這個時候點(diǎn)擊“確定”按鈕,主線程就會去執(zhí)行event方法,那界面就會出現(xiàn)“無響應(yīng)”狀態(tài),如果要界面正常顯示,那我們就需要用到多線程(threading)
多線程,完整代碼
import tkinter as tk import threading # 導(dǎo)入多線程模塊 class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("500x200+1100+150") self.interface() def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定", command=self.start) self.Button0.grid(row=0, column=0) self.w1 = tk.Text(self.root, width=80, height=10) self.w1.grid(row=1, column=0) def event(self): '''按鈕事件,一直循環(huán)''' a = 0 while True: a += 1 self.w1.insert(1.0, str(a)+'\n') def start(self): self.T = threading.Thread(target=self.event) # 多線程 self.T.setDaemon(True) # 線程守護(hù),即主進(jìn)程結(jié)束后,此線程也結(jié)束。否則主進(jìn)程結(jié)束子進(jìn)程不結(jié)束 self.T.start() # 啟動 if __name__ == '__main__': a = GUI() a.root.mainloop()
七、Tkinter多線程暫停和繼續(xù)
import tkinter as tk import threading from time import sleep event = threading.Event() class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("500x200+1100+150") self.interface() def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="啟動", command=self.start) self.Button0.grid(row=0, column=0) self.Button0 = tk.Button(self.root, text="暫停", command=self.stop) self.Button0.grid(row=0, column=1) self.Button0 = tk.Button(self.root, text="繼續(xù)", command=self.conti) self.Button0.grid(row=0, column=2) self.w1 = tk.Text(self.root, width=70, height=10) self.w1.grid(row=1, column=0, columnspan=3) def event(self): '''按鈕事件,一直循環(huán)''' while True: sleep(1) event.wait() self.w1.insert(1.0, '運(yùn)行中'+'\n') def start(self): event.set() self.T = threading.Thread(target=self.event) self.T.setDaemon(True) self.T.start() def stop(self): event.clear() self.w1.insert(1.0, '暫停'+'\n') def conti(self): event.set() self.w1.insert(1.0, '繼續(xù)'+'\n') if __name__ == '__main__': a = GUI() a.root.mainloop()
八、Tkinter文件之間的調(diào)用
8.1、準(zhǔn)備工作
- a.py文件 - -界面邏輯+線程
- b.py 文件 - -業(yè)務(wù)邏輯
- 以上文件在同一個目錄下
8.2、方法
# a.py 文件 import tkinter as tk import threading from b import logic # 調(diào)用b文件中的logic類 class GUI: def __init__(self): self.root = tk.Tk() self.root.title('演示窗口') self.root.geometry("500x260+1100+150") self.interface() def interface(self): """"界面編寫位置""" self.Button0 = tk.Button(self.root, text="確定執(zhí)行", command=self.start, bg="#7bbfea") self.Button0.grid(row=0, column=1, pady=10) self.entry00 = tk.StringVar() self.entry00.set("") self.entry0 = tk.Entry(self.root, textvariable=self.entry00) self.entry0.grid(row=1, column=1, pady=15) self.w1 = tk.Text(self.root, width=50, height=8) self.w1.grid(row=2, column=0, columnspan=3, padx=60) def seal(self): '''把b文件的類單獨(dú)寫一個方法''' a = self.entry00.get() w1 = self.w1 logic().event(a, w1) def start(self): '''子線程無法直接執(zhí)行b的類,需要把b文件單獨(dú)寫一個方法,然后執(zhí)行''' self.T = threading.Thread(target=self.seal) self.T.setDaemon(True) self.T.start() if __name__ == '__main__': a = GUI() a.root.mainloop()
# b.py 文件 import time class logic(): def __init__(self): pass def main(self, a, x): while True: y = int(a)+int(x) self.w1.insert(1.0, str(y)+'\n') time.sleep(1) x += 1 def event(self, a, w1): '''調(diào)用main的方法''' self.w1 = w1 x = 1 self.main(a, x)
總結(jié)
到此這篇關(guān)于python Tkinter模塊使用的文章就介紹到這了,更多相關(guān)python Tkinter使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python tkinter模塊彈出窗口及傳值回到主窗口操作詳解
- python3使用tkinter實(shí)現(xiàn)ui界面簡單實(shí)例
- Python升級提示Tkinter模塊找不到的解決方法
- Python Tkinter簡單布局實(shí)例教程
- python的tkinter布局之簡單的聊天窗口實(shí)現(xiàn)方法
- Python Tkinter基礎(chǔ)控件用法
- python TKinter獲取文本框內(nèi)容的方法
- python 實(shí)現(xiàn)在tkinter中動態(tài)顯示label圖片的方法
- python3.5 tkinter實(shí)現(xiàn)頁面跳轉(zhuǎn)
相關(guān)文章
pandas實(shí)現(xiàn)按照Series分組示例
本文主要介紹了pandas按照Series分組示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Mac在python3環(huán)境下安裝virtualwrapper遇到的問題及解決方法
這篇文章主要介紹了Mac在python3環(huán)境下安裝virtualwrapper遇到的問題及解決方法,我在使用mac安裝virtualwrapper的時候遇到了問題,搞了好長時間,,在這里總結(jié)一下分享出來,供遇到相同的問題的朋友使用,少走些彎路,需要的朋友可以參考下2019-07-07linux環(huán)境打包python工程為可執(zhí)行程序的過程
本次需求,在ubuntu上面開發(fā)的python代碼程序需要打包成一個可執(zhí)行程序然后交付給甲方,因為不能直接給源碼給甲方,所以尋找方法將python開發(fā)的源碼打包成一個可執(zhí)行程序,本次在ubuntu上打包python源碼的方法和在window上打包的有點(diǎn)類似,感興趣的朋友跟隨小編一起看看吧2024-01-01通過實(shí)例解析Python文件操作實(shí)現(xiàn)步驟
這篇文章主要介紹了通過實(shí)例解析Python文件操作實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09python實(shí)現(xiàn)FTP服務(wù)器服務(wù)的方法
本篇文章主要介紹了python實(shí)現(xiàn)FTP服務(wù)器的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04如何利用Playwright庫進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取
playwright庫是微軟開源的一個庫,這個庫的功能更加的強(qiáng)大,除了可以實(shí)現(xiàn)同步操作,同樣也可以實(shí)現(xiàn)異步的操作,這篇文章主要介紹了如何利用Playwright庫進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取,需要的朋友可以參考下2023-05-05