python入門之Tkinter使用的方法詳解
一、前言
1.1、Tkinter是什么
Tkinter 是使用 python 進行窗口視窗設計的模塊。Tkinter模塊(“Tk 接口”)是Python的標準Tk GUI工具包的接口。作為 python 特定的GUI界面,是一個圖像的窗口,tkinter是python自帶的,可以編輯的GUI界面,用來入門,熟悉窗口視窗的使用,非常有必要。
二、準備工作
2.1、Windows演示環(huán)境搭建
安裝python3.7安裝編輯器,演示使用的Visual Studio Code
三、Tkinter創(chuàng)建窗口
3.1、創(chuàng)建出一個窗口
首先我們導入tkinter的庫
import tkinter as tk # 在代碼里面導入庫,起一個別名,以后代碼里面就用這個別名 root = tk.Tk() # 這個庫里面有Tk()這個方法,這個方法的作用就是創(chuàng)建一個窗口
如果只是執(zhí)行以上的兩句代碼,運行程序是沒有反應的,因為只有一個主函數(shù),從上到下執(zhí)行完就沒有了,這個窗口也是很快就消失了,所以現(xiàn)在我們要做的就是讓窗口一直顯示,那么我們就可以加一個循環(huán)
創(chuàng)建的窗口的名字是root ,那么我們之后使用這個root就可以操作這個窗口了。
root.mainloop() # 加上這一句,就可以看見窗口了
執(zhí)行以上的3句代碼,我們就可以看見窗口了
3.2、給窗口取一個標題
root.title('演示窗口')
3.3、窗口設置
通過以下代碼,我們可以給窗口設置長寬以及窗口在屏幕的位置
root.geometry("300x100+630+80") # 長x寬+x*y
3.3、創(chuàng)建按鈕,并且給按鈕添加點擊事件
這個庫里面有一個方法Button(),只要調用這個方法,我們就可以創(chuàng)建這個組件,創(chuàng)建的這個組件我們賦值給一個常量,以后我們就可以用這個常量來操作這個按鈕,這個方法里面的參數(shù),就是要我們寫窗口的名字Button(root) 這樣寫的意思就是 將我們創(chuàng)建的按鈕放到這個窗口上面
btn1 = tk.Button(root)
給按鈕取一個名稱
btn1["text"] = "點擊"
我們創(chuàng)建的按鈕組件,已經放到窗口里面了,但是放到窗口的哪個位置,東南西北哪個地方,我們就可以用pack()去定位(后面會介紹其它定位方法)
btn1.pack() # 按鈕在窗口里面的定位
創(chuàng)建點擊按鈕事件的彈窗,先導入messagebox,這個必須單獨導入
from tkinter import messagebox def test(e): messagebox.showinfo("窗口名稱","點擊成功")
現(xiàn)在有了按鈕,有了方法,我想要做的是一點擊按鈕,就執(zhí)行這個方法,那么就需要將按鈕和方法進行綁定
btn1.bind("<Button-1>",test) #第一個參數(shù)為:按鼠標左鍵的事件 第二個參數(shù)為:要執(zhí)行的方法的名字
按鈕組件里面有一個方法bind() 這個方法就可以實現(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"] = "點擊" # 給按鈕一個名稱 btn1.pack() # 按鈕布局 def test(e): '''創(chuàng)建彈窗''' messagebox.showinfo("窗口名稱", "點擊成功") btn1.bind("<Button-1>", test) # 將按鈕和方法進行綁定,也就是創(chuàng)建了一個事件 root.mainloop() # 讓窗口一直顯示,循環(huán)
3.4、窗口內的組件布局
3種布局管理器:pack - grid - place
pack
這個布局管理器,要么將組件垂直的排列,要么水平的排列
grid
Grid(網格)布局管理器會將控件放置到一個二維的表格里。
主控件被分割成一系列的行和列,表格中的每個單元(cell)都可以放置一個控件。
選項 | 說明 |
---|---|
column | 單元格的列號,從0開始的正整數(shù) |
columnspan | 跨列,跨越的列數(shù),正整數(shù) |
row | 單元格的行號, 從0開始的正整數(shù) |
rowspan | 跨行,跨越的行數(shù),正整數(shù) |
ipadx, ipady | 設置子組件之間的間隔,x方向或y方向,默認單位為像素,非浮點數(shù),默認0.0 |
padx, pady | 與之并列的組件之間的間隔,x方向或y方向,默認單位為像素,非浮點數(shù),默認0.0 |
sticky | 組件緊貼所在的單元格的某一腳,對應于東南西北中以及4個角。東 = “e”,南=“s”,西=“w”,北=“n”,“ne”,“se”,“sw”, “nw”; |
grid_info() 查看組件默認的參數(shù)
import tkinter as tk root = tk.Tk() # 默認按鈕 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 水平方向內邊距
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 垂直方向內邊距
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() # 默認按鈕 btn1 = tk.Button(root) btn1["text"] = "默認按鈕演示效果" 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布局管理器可以通過坐標精確控制組件的位置,適用于一些布局更加靈活的場景
選項 | 說明 |
---|---|
x,y | 組件左上角的絕對坐標(相當于窗口) |
relx ,rely | 組件左上角的坐標(相對于父容器) |
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、復選按鈕_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="運行", 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("運行成功")
5.2、輸入框(Entry)內容獲取
def interface(self): """"界面編寫位置""" self.entry00 = tk.StringVar() self.entry00.set("默認信息") self.entry0 = tk.Entry(self.root, textvariable=self.entry00) self.entry0.grid(row=1, column=0) self.Button0 = tk.Button(self.root, text="運行", 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", "默認信息") def event(self): '''清空輸入框''' self.w1.delete(1.0, "end")
5.3、獲取復選按鈕(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="復選框", 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): '''按鈕事件,獲取復選框的狀態(tài),1表示勾選,0表示未勾選''' a = self.v1.get() self.w1.insert(1.0, str(a)+'\n') def Check_box(self): '''復選框事件''' 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、清除復選框勾選狀態(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="復選框", 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): '''按鈕事件,清除復選框勾選狀態(tài)''' self.Checkbutton01.deselect() def Check_box(self): '''復選框事件''' if self.v1.get() == 1: self.w1.insert(1.0, "勾選"+'\n') else: self.w1.insert(1.0, "未勾選"+'\n')
5.6、文本框(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) def event(self): a = self.w1.get('0.0', 'end') print(a)
六、Tkinter使用多線程
6.1、為什么要使用多線程
以下為單線程運行
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')
單線程下,主線程需要運行窗口,如果這個時候點擊“確定”按鈕,主線程就會去執(zhí)行event方法,那界面就會出現(xiàn)“無響應”狀態(tài),如果要界面正常顯示,那我們就需要用到多線程(threading)
多線程,完整代碼
import tkinter as tk import threading # 導入多線程模塊 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) # 線程守護,即主進程結束后,此線程也結束。否則主進程結束子進程不結束 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, '運行中'+'\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文件之間的調用
8.1、準備工作
a.py文件 - -界面邏輯+線程b.py 文件 - -業(yè)務邏輯以上文件在同一個目錄下
8.2、方法
# a.py 文件 import tkinter as tk import threading from b import logic # 調用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文件的類單獨寫一個方法''' a = self.entry00.get() w1 = self.w1 logic().event(a, w1) def start(self): '''子線程無法直接執(zhí)行b的類,需要把b文件單獨寫一個方法,然后執(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): '''調用main的方法''' self.w1 = w1 x = 1 self.main(a, x)
總結
到此這篇關于python入門之Tkinter使用的文章就介紹到這了,更多相關python Tkinter使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python tkinter模塊彈出窗口及傳值回到主窗口操作詳解
- python3使用tkinter實現(xiàn)ui界面簡單實例
- Python升級提示Tkinter模塊找不到的解決方法
- Python Tkinter簡單布局實例教程
- python的tkinter布局之簡單的聊天窗口實現(xiàn)方法
- Python Tkinter基礎控件用法
- python TKinter獲取文本框內容的方法
- python 實現(xiàn)在tkinter中動態(tài)顯示label圖片的方法
- 對Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解
- 對python Tkinter Text的用法詳解
相關文章
Python配置文件管理之ini和yaml文件讀取的實現(xiàn)
本文主要介紹了Python配置文件管理之ini和yaml文件讀取,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02Python的加密模塊之hashlib 與 base64詳解及常用加密方法
我們來學習一下 Python 中的加密模塊,加密模塊在工作中被廣泛應用,比如數(shù)據(jù)的傳入 不希望被捕獲,通過把數(shù)據(jù)加密。這樣即使被捕獲也無法獲取到數(shù)據(jù)的真實信息,今天我們就來學習一下關于加密的方法,感興趣的朋友跟隨小編一起看看吧2023-02-02