Python+tkinter制作經(jīng)典登錄界面和點擊事件
前言
Tkinter(即 tk interface) 是 Python 標準 GUI 庫,簡稱 “Tk”;從本質(zhì)上來說,它是對 TCL/TK 工具包的一種 Python 接口封裝。
Tkinter 是 Python 自帶的標準庫,因此無須另行安裝,它支持跨平臺運行,不僅可以在 Windows 平臺上運行,還支持在 Linux 和 Mac 平臺上運行。
Tkinter 編寫的程序,也稱為 GUI 程序,GUI (Graphical User Interface)指的是“圖形用戶界面”,它是計算機圖形學(xué)(CG)的一門分支,主要研究如何在計算機中表示圖形,以及利用計算機進行圖形的計算、處理和顯示等相關(guān)工作。
GUI 這一概念并非 Python 語言獨有,它屬于計算機科學(xué)技術(shù)領(lǐng)域中的一個概念,比如使用 C/C++ 語言開發(fā)的 Qt、GTK、Electron 等都屬于 GUI 軟件包
環(huán)境使用
Python 3.8
Pycharm
模塊使用
tkinter
PIL
代碼部分
導(dǎo)入模塊
import tkinter as tk import tkinter.messagebox from PIL import Image, ImageTk
先做一個大小合適的窗口
root = tk.Tk() root.title('軟件登陸界面') root.geometry('369x200+500+500') root.mainloop()
賬號密碼輸入框
# 用戶登陸 tk.Label(root, text='用戶登陸', font=('微軟雅黑', 20)).grid(row=0, column=0, columnspan=10) # 登陸賬號 tk.Label(root, text='登陸賬號:', font=('微軟雅黑', 15)).grid(row=1, column=0, padx=10) # 賬號輸入框 account_va = tk.StringVar() tk.Entry(root, textvariable=account_va).grid(row=1, column=1, padx=5) # 登陸密碼 tk.Label(root, text='登陸密碼:', font=('微軟雅黑', 15)).grid(row=2, column=0, padx=10) # 密碼輸入框 password_va = tk.StringVar() tk.Entry(root, textvariable=password_va, show='*').grid(row=2, column=1, padx=5)
點擊按鈕
# 登陸賬號 tk.Label(root, text='登陸賬號:', font=('微軟雅黑', 15)).grid(row=1, column=0, padx=10) # 注冊賬號 tk.Button(root, text='忘記密碼',font=('微軟雅黑'), relief="flat").grid(row=2, column=2, padx=10) # 登陸按鈕 tk.Button(root, text='登陸', font=('微軟雅黑'), bg='red', fg='white', width=10, relief="flat").grid(row=3, column=0, columnspan=10) tk.Label(root, text='公共用戶名:admin 登陸密碼:123456', fg='gray').grid(row=4, column=0, columnspan=10, pady=15)
點擊事件綁定
登錄
def Land(): if account_va.get() == 'admin' and password_va.get() == '123456': tkinter.messagebox.showinfo(title='溫馨提示', message='哈哈哈哈哈, 騙你的, 怎么會把密碼告訴你呢') tkinter.messagebox.showinfo(title='溫馨提示', message='你可以點擊注冊會員試試') else: tkinter.messagebox.showerror(title='警告', message='你的賬號密碼有問題, 也可以點擊注冊會員')
忘記密碼
def ForgetPassword(): tkinter.messagebox.showerror(title='錯誤', message='你根本就沒有密碼, 你心里沒數(shù)?')
注冊
def RegisterAnAccount(): top = tk.Toplevel() top.title("掃碼添加") top.geometry('640x750+500+500') # 導(dǎo)入圖片 image = Image.open('img.png') tk_image = ImageTk.PhotoImage(image) # 在標簽里放入圖片 tk.Label(top, image=tk_image).pack() top.mainloop()
最后效果
到此這篇關(guān)于Python+tkinter制作經(jīng)典登錄界面和點擊事件的文章就介紹到這了,更多相關(guān)Python tkinter登錄界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python用matplotlib庫畫圖中文和負號顯示為方框的問題解決
matplotlib中畫圖的時候會遇到負號顯示為方框的問題,下面這篇文章主要給大家介紹了關(guān)于Python用matplotlib庫畫圖中文和負號顯示為方框的問題解決,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07淺析python實現(xiàn)布隆過濾器及Redis中的緩存穿透原理
本文帶你了解了位圖的實現(xiàn),布隆過濾器的原理及 Python 中的使用,以及布隆過濾器如何應(yīng)對 Redis 中的緩存穿透,相信你對布隆過濾器已經(jīng)有了一定的認識2021-09-09