search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Python]Tkinter圖形化介面GUI教學

[Python]Tkinter圖形化介面GUI教學

關鍵字:python、tkinter、GUI、圖形化介面、教學、Linux、Window、元件、套件、label、button、text、filedialog、update

image

要使用tkinter之前要先安裝以下兩個套件,開啟終端機輸入指令即可

sudo apt-get install python3-tk
sudo apt-get install python-tk
 
接著要在程式載入套件
import tkinter as tk
 
以下就來介紹各種tkinter的元件使用方式

建立一個視窗
import tkinter as tk
window = tk.Tk()#創建視窗
window.title('window')#程式標題,中音文或符號階可
window.geometry('200x200')#視窗大小
window.configure(background='white')#視窗底色,可以用色碼表
window.mainloop()#tkinter每經過一個動作就刷新視窗一次,可放在整支程式的最尾部
image
備註:可以同時建立很多的視窗
 
Text元件
import tkinter as tk
window = tk.Tk()
window.title('window 1')
window.geometry('500x500')
window.configure(background='white')
text = tk.Text(window)
text.place(x=0,y=155,height=100,width = 600)#設定text框的座標x,y以及長寬hight,width
word = 'ABCD'#設定文字
text.insert("insert", word)#插入文字
window.mainloop()
image
備註:除了字串外也可辨識\n換行符號(entry元件無法讀取)
 
Button元件
以點擊按鈕插入text文字為例
import tkinter as tk
window = tk.Tk()
window.title('window 1')
window.geometry('500x500')
window.configure(background='white')
text = tk.Text(window)
text.place(x=0,y=100,height=100,width = 300)
#想執行的事件寫入函數
def bt():
    word = '點了按鈕'
    text.insert("insert", word)
out = tk.Button(window,text = '點擊',command = bt)#設置按鈕讀取函數bt
out.place(x=0,y=0)
window.mainloop()
image
 
 
label元件
#!/usr/bin/python3
import tkinter as tk
window = tk.Tk()
window.title('window 1')
window.geometry('500x500')
window.configure(background='white')
label = tk.Label(window)#建立標籤物件
label.place(x=0,y=0)#標籤設定位置
word = 'ABCD'
label.configure(text = word)
window.mainloop()
image
 
Scrollbar元件(滾動條)
以滾動text多行字串為例
import tkinter as tk
window = tk.Tk()
window.title('window 1')
window.geometry('500x500')
window.configure(background='white')
text = tk.Text(window)建立text物件
text.place(x=0,y=0,height=100,width = 300)#text設置座標寬高
BA = 'A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT'#設置多行字串\n是換行符號
text.insert("insert", BA)#插入字串
s = tk.Scrollbar(window)#建立Scrollbar物件
s.place(x=300,y=0,height=100,width = 30)#Scrollbar設置座標寬高
s.config(command=text.yview)#設定垂直滾動
text.config(yscrollcommand=s.set)#text設定垂直滾動條參數以s為對象
window.mainloop()
image

 

filedialog瀏覽選取目錄路徑或檔案路徑,範例以button觸發事件使用filedialog讀取路徑後顯示在label
以下是指定檔案路徑寫法
import tkinter as tk
from tkinter import filedialog
window = tk.Tk()
window.title('window 1')
window.geometry('500x500')
window.configure(background='white')
label = tk.Label(window)#建立標籤物件
label.place(x=0,y=0)#標籤設定位置
def openfile():
    path = filedialog.askopenfilename()#檔案引索
    word = tk.StringVar()
    word.set(path)
    label.configure(text = word.get())
fi = tk.Button(window,text = 'Click',command = openfile)
fi.place(x=0,y=40)
window.mainloop()
image
以下是指定目錄路徑寫法
import tkinter as tk
from tkinter import filedialog
window = tk.Tk()
window.title('window 1')
window.geometry('500x500')
window.configure(background='white')
label = tk.Label(window)#建立標籤物件
label.place(x=0,y=0)#標籤設定位置
def output():
    path = filedialog.askdirectory()#檔案引索
    word = tk.StringVar()
    word.set(path)
    label.configure(text = word.get())
fi = tk.Button(window,text = 'Click',command = output)
fi.place(x=0,y=40)
window.mainloop()
image
 
update刷新程式頁面
創建視窗時會先宣告視窗window = tk.Tk(),接著可以使用函示.update()刷新頁面,以下是把update放在迴圈裡刷新
#!/usr/bin/python3
import os,sys,subprocess
import tkinter as tk
import time
from tkinter import filedialog
window = tk.Tk()
window.title('TEST')
window.geometry('400x400')
window.configure(background='white')
text = tk.Text(window)
text.place(x=0,y=155,height=445,width = 770)
def click() :
    for i in range(10):    
        time.sleep(1)
        text.insert("insert", i)
        window.update()
out = tk.Button(window,text = '開始',command = click)
out.place(x=0,y=0)
window.mainloop()
以上[Python]Tkinter圖形化介面GUI教學介紹到這邊,之後有空在陸續更新其他元件!!!


熱門推薦

本文由 kk665403pixnetnetblog 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦