案例說(shuō)明
現(xiàn)在,通過(guò)一個(gè)簡(jiǎn)易的用戶登錄窗口的實(shí)現(xiàn),快速上手tkinter。
可以在登錄窗口輸入用戶名、密碼進(jìn)行登錄,用戶名、密碼符合要求,則顯示OK消息,反之則顯示Error消息。如下面圖片所示:
熟悉常用的基本組件
俗話說(shuō)“磨刀不誤砍柴工”,在進(jìn)行案例代碼編寫前,先花點(diǎn)時(shí)間完成下面6個(gè)代碼段的練習(xí),熟悉這些基本組件的使用方法。
定義并顯示主窗口
# 導(dǎo)入tkinter庫(kù)
import tkinter
window = tkinter.Tk()
# 設(shè)置窗口title
window.title('my window')
# 設(shè)置窗口大小
window.geometry('200x120')
# 顯示主窗口
window.mainloop()
運(yùn)行結(jié)果如下:
lable標(biāo)簽
import tkinter
window = tkinter.Tk()
window.title('my window')
window.geometry('200x180')
l = tkinter.Label(window,
text='This is Label!', # 標(biāo)簽的文字
bg='pink', # 背景顏色
font=('Arial', 12), # 字體和字體大小
width=15, height=2 # 標(biāo)簽長(zhǎng)寬
)
# 固定窗口位置
l.pack()
window.mainloop()
運(yùn)行結(jié)果如下:
pack&grid&place 組件布局
import tkinter
window = tkinter.Tk()
window.title('my window')
window.geometry('200x180')
# pack布局
l1 = tkinter.Label(window,text='This is Label!',bg='pink',width=15, height=2)
l1.pack(side='bottom')
# grid布局
# for i in range(4):
# for j in range(3):
# tkinter.Label(window, text=1).grid(row=i, # 行
# column=j, # 列
# padx=10, # 單元格左右間距
# pady=10 # 單元格上下間距
# )
### place布局
l2 = tkinter.Label(window, text='This is Label2!', justify=tkinter.RIGHT, width=50)
l2.place(x=40, y=50, # 設(shè)置x,y坐標(biāo)
width=100, height=30 # 設(shè)置長(zhǎng)寬
)
window.mainloop()
運(yùn)行結(jié)果如下:
注釋pack布局代碼,去除grid布局代碼注釋,然后運(yùn)行,運(yùn)行結(jié)果如下:
Button按鈕
import tkinter
window = tkinter.Tk()
window.title('my window')
window.geometry('200x120')
# 定義button
b = tkinter.Button(window,
text='退出', # 按鈕的文字
bg='pink', # 背景顏色
width=15, height=2, # 設(shè)置長(zhǎng)寬
command=window.quit # 響應(yīng)事件:關(guān)閉窗口
)
b.pack()
window.mainloop()
運(yùn)行結(jié)果如下:
2.5. Entry&Text輸入框與文本框
import tkinter
window = tkinter.Tk()
window.title('my window')
window.geometry('200x200')
def plus():
n1 = e1.get() # 獲取輸入框1的值
n2 = e2.get() # 獲取輸入框2的值
t.delete(1.0, 'end') # 清除文本框內(nèi)容
t.insert('insert', int(n1)+int(n2)) # 將結(jié)果添加到文本框顯示
l1 = tkinter.Label(window, text='輸入數(shù)字1')
l1.pack()
# 定義輸入框1
e1 = tkinter.Entry(window, width=15)
e1.pack()
# 定義輸入框2
l2 = tkinter.Label(window, text='輸入數(shù)字2')
l2.pack()
e2 = tkinter.Entry(window, width=15)
e2.pack()
b1 = tkinter.Button(window, text='計(jì)算兩數(shù)之和', command=plus)
b1.pack()
# 定義文本框
t = tkinter.Text(window,
state='normal', # 有disabled、normal 兩個(gè)狀態(tài)值,默認(rèn)為normal
width=15, height=2
)
t.pack()
b2 = tkinter.Button(window, text='退出', command=window.quit)
b2.pack()
window.mainloop()
運(yùn)行結(jié)果如下:
messagebox消息對(duì)話框
import tkinter
import tkinter.messagebox
window = tkinter.Tk()
window.title('my window')
window.geometry('200x100')
def say_hello():
tkinter.messagebox.showinfo(title='my message', message='hello!')
# 定義消息對(duì)話框
b = tkinter.Button(window, text='click', command=say_hello)
b.pack()
window.mainloop()
運(yùn)行結(jié)果如下:
案例代碼實(shí)現(xiàn)
import tkinter
import tkinter.messagebox
# 用戶登錄驗(yàn)證
def login():
name = entryName.get()
pwd = entryPwd.get()
if name == 'admin' and pwd == '123456':
tkinter.messagebox.showinfo(title='userlogin', message='ok')
else:
tkinter.messagebox.showerror(title='userlogin', message='Error')
# 清空用戶輸入的用戶名和密碼
def cancel():
varName.set('')
varPwd.set('')
window = tkinter.Tk()
window.title('my window')
window.geometry('200x120')
varName = tkinter.StringVar(value='')
varPwd = tkinter.StringVar(value='')
# 創(chuàng)建標(biāo)簽
labelName = tkinter.Label(window, text='User Name', justify=tkinter.RIGHT, width=80)
# 將標(biāo)簽放到窗口上
labelName.place(x=10, y=5, width=80, height=20)
# 創(chuàng)建文本框,并設(shè)置關(guān)聯(lián)的變量
entryName = tkinter.Entry(window, width=80, textvariable=varName)
entryName.place(x=100, y=5, width=80, height=20)
labelPwd = tkinter.Label(window, text='User Pwd:', justify=tkinter.RIGHT, width=80)
labelPwd.place(x=10, y=30, width=80, height=20)
# 創(chuàng)建密碼文本框
entryPwd = tkinter.Entry(window, show='*', width=80, textvariable=varPwd)
entryPwd.place (x=100, y=30, width=80, height=20)
# 創(chuàng)建按鈕組件,同時(shí)設(shè)置按鈕事件處理函數(shù)
buttonOk = tkinter.Button(window, text='Login', command=login)
buttonOk.place(x=30, y=70, width=50, height=20)
buttonCancel = tkinter.Button(window, text='Cancel', command=cancel)
buttonCancel.place(x=90, y=70, width=50, height=20)
# 啟動(dòng)消息循環(huán)
window .mainloop()