Tkinter模塊是Python系統自帶的標准GUI庫,具有一套常用的圖形組件
import tkinter
from tkinter import *
import tkinter as tk
窗體是帶有標題、邊框的一個頂層容器,在其內部可以添加其他組件,使用的模塊對象都是放置在窗體對象中的。
調用pack()方法進行容器的區域布局。
win = tkinter.Tk()
win.title("2333") # 標題
win.geometry("720x480+40+40") # 大小(720x480),起始位置(40+40)
win.mainloop() # 進入主事件循環,即把窗體對象顯現出來。
label = tkinter.Label(容器名稱,顯示文字或圖像內容,顯示位置,文字字體,顏色等)
當按下應用程序中的按鈕時,應用程序能觸發某個事件從而執行相應的操作。在Python中tkinter模塊中的Button用於構建按鈕對象。
Btn = tkinter.Button(容器,text=‘按鈕上的文字’)
import tkinter
win = tkinter.Tk()
win.title("2333")
win.geometry("720x480+40+40")
label = tkinter.Label(win, text = "你是一個一個一個",font = "宋體", fg = "#0000ff")
label.pack()
def myClick():
t1 = "哼哼哼啊啊啊啊啊啊啊啊"
labelClick = tkinter.Label(win, text = t1)
labelClick.pack()
btn = tkinter.Button(win, text = "點擊我", command = myClick)
btn.pack()
win.mainloop()
Python定義了3種界面布局管理方式
Pack 布局管理方式按組件的創建順序在容器區域中排列
import tkinter
win = tkinter.Tk()
win.title("布局")
win.geometry("720x480")
L1 = tkinter.Label(win, text = "L1", bg = "red")
L1.pack(fill = 'y', side = "left")
L2 = tkinter.Label(win, text = "L2", bg = "green")
L2.pack(fill = 'both', side = "right")
L3 = tkinter.Label(win, text = "L3", bg = "blue")
L3.pack(fill = 'x', side = "left")
win.mainloop()
Place布局管理方式指定組件的坐標位置排列,也叫做絕對排列
lb = Label(root, text='hello Place')
lb.place(relx = 1,rely = 0.5,anchor = CENTER)
lb.place(x=0, y=0, anchor = NW)
網格布局,組件放置在二維表格的單元格中
Grid布局的常用屬性有row(行)、column(列)、rowspan(組件占據行數)、columnspan(組件占據列數)
from tkinter import *
win = tkinter.Tk()
win.title("學生信息收集")
win.geometry("720x480+10+10")
L1 = Label(win, text = "學生信息", font = "song -20")
L2 = Label(win, text = "學號", font = "song -20")
L3 = Label(win, text = "姓名", font = "song -20")
L4 = Label(win, text = "專業", font = "song -20")
L1.grid(row = 0, column = 1)
L2.grid(row = 1)
L3.grid(row = 2)
L4.grid(row = 3)
在python中,文本框Entry用於接收輸入的數據。文本框Entry的基本格式為:
txt = tkinter.Entry(容器名稱,width=寬度,文字字體、顏色等)
from tkinter import *
win = tkinter.Tk()
win.title("學生信息收集")
win.geometry("720x480+10+10")
L1 = Label(win, text = "學生信息", font = "song -20")
L2 = Label(win, text = "學號", font = "song -20")
L3 = Label(win, text = "姓名", font = "song -20")
L4 = Label(win, text = "專業", font = "song -20")
L1.grid(row = 0, column = 1)
L2.grid(row = 1)
L3.grid(row = 2)
L4.grid(row = 3)
e1 = Entry(win, width = 20, font = "song -20")
e2 = Entry(win, width = 20, font = "song -20")
e3 = Entry(win, width = 20, font = "song -20")
e1.grid(row = 1, column = 2)
e2.grid(row = 2, column = 2)
e3.grid(row = 3, column = 2)
b1 = Button(win, text = "提交")
b2 = Button(win, text = "取消")
b1.grid(row = 4, column = 0)
b2.grid(row = 4, column = 1)
win.mainloop()
對文本框Entry中文字內容的操作可以使用StringVar()對象來完成。StringVar()跟蹤變量值的變化,把最新的值顯示到界面上
步驟:
設計一個登錄界面
from tkinter import *
win = tkinter.Tk()
win.title("身份驗證")
win.geometry("720x480+10+10")
def myClick():
txt = txt2.get()
if(txt == "abc"):
txt3.set("oh*yeah*sir*")
else:
txt3.set("你是一個一個一個")
lab1 = Label(win, text = "請輸入用戶名", font = ('song', '16'))
lab2 = Label(win, text = "請輸入密碼", font = ('song', '16'))
txt1,txt2, txt3 = StringVar(), StringVar(), StringVar()
txt3.set("請輸入用戶名和密碼")
e1 = Entry(win, textvariable = txt1, width = 16, font = "song -16")
e2 = Entry(win, textvariable = txt2, width = 16, show = "*", font = "song -16")
b1 = Button(win, text = "提交", command = myClick, font = "song -16")
lab3 = Label(win, textvariable = txt3, relief = "ridge", width = 30, font = ("song", "16"))
lab1.grid(row = 0, column = 0)
lab2.grid(row = 1, column = 0)
e1.grid(row = 0, column = 1)
e2.grid(row = 1, column = 1)
lab3.grid(row = 2, column = 0, columnspan = 2)
b1.grid(row = 2, column = 2)
win.mainloop()
單選按鈕Radiobutton和復選框Checkbutton是一組表示多種選擇的組件
它們只有兩種狀態“選中/未選中”,其屬性和方法類似
chVarDis = tk.IntVar()
from tkinter import *
win = Tk()
win.title("單選框")
win.geometry("720x480+10+10")
txt = StringVar()
txt.set("請選擇")
lab = Label(win, textvariable = txt, relief = "ridge", width = 30)
chVarDis = IntVar()
check1 = Checkbutton(win, text = "C", variable = chVarDis, state = "disabled")
check1.select()
chVarUn = IntVar()
check2 = Checkbutton(win, text = "Java", variable = chVarUn, state = "disabled")
check2.deselect()
chVarEn = IntVar()
check3 = Checkbutton(win, text = "Python", variable = chVarEn)
check3.select()
lab.grid(row = 0, column = 0, columnspan = 3)
check1.grid(row = 4, column = 0, sticky = W)
check2.grid(row = 4, column = 1, sticky = W)
check3.grid(row = 4, column = 2, sticky = W)
win.mainloop()
from tkinter import *
win = Tk()
win.title("多選一")
win.geometry("720x480+10+10")
txt = StringVar()
txt.set("請選擇")
lab = Label(win, textvariable = txt, relief = "ridge", width = 30) #relief是設置邊框
chk = ["胡桃", "刻晴", "甘雨"]
def radCall():
radSel = radVar.get()
if radSel == 0:
txt.set(chk[0])
elif radSel == 1:
txt.set(chk[1])
elif radSel == 2:
txt.set(chk[2])
print(radSel)
radVar = IntVar()
for i in range(3):
curRad = Radiobutton(win, text = chk[i], variable = radVar, value = i, command = radCall) # 復選框中value是選中該框的返回值
curRad.grid(column = i, row = 5, sticky = W)
lab.grid(row = 0, column = 0, columnspan = 3)
win.mainloop()
from tkinter import tkk
from tkinter import ttk, scrolledtext
import tkinter as tk
win = tk.Tk()
win.title("Python組件演示")
win.geometry("1080x720")
montry = ttk.LabelFrame(win, text = "標簽框架")
montry.grid(row = 0, column = 0, padx = 100, pady = 100)
lable1 = ttk.Label(montry, text = "選擇一個數字")
lable1.grid(column = 1, row = 0)
def clickMe():
action.configure(text = "Hello" + ' '+ numberChosen.get())
action = ttk.Button(montry, text = "單擊我", command = clickMe)
action.grid(column = 2, row = 1)
num = tk.StringVar()
numberChosen = ttk.Combobox(montry, width = 12, textvariable = num, state = "readonly")
numberChosen['values'] = (1, 2, 4, 42, 100)
numberChosen.grid(column = 1, row = 1)
numberChosen.current(0)
scrolW = 30
scrolH = 3
scr = scrolledtext.ScrolledText(montry, width= scrolW, height = scrolH)
scr.grid(column = 0, columnspan = 3)
win.mainloop()
Tkinter提供了3種標准的對話框模塊
import tkinter.filedialog
文件對話框的返回值為文件路徑和文件名
在python中tkinter模塊的事件event都是由字符串描述的:組件對象.bind(event,handler)
<ButtonPress-n>
<ButtonPress-1>
表示按下鼠標的左鍵from tkinter import *
def callback(event):
print("Click at:", event.x, event.y)
s = (event.x, event.y)
txt.set(s)
win = tk.Tk()
win.title("鼠標事件")
win.geometry("1080x720")
frame = Frame(win, width = 1080, height = 720, bg = "cyan")
frame.bind("<Button-1>", callback)
frame.pack()
txt = StringVar()
L = Label(win, width = 20, textvariable = txt)
L.pack()
win.mainloop()