Python tkinter 制作一个经典的登录界面和点击事件
前言
Tkinter(即 tk interface) 是 Python 标准 GUI 库,简称 “Tk”;从本质上来说,它是对 TCL/TK 工具包的一种 Python 接口封装。
Tkinter 是 Python 自带的标准库,因此无须另行安装,它支持跨平台运行,不仅可以在 Windows 平台上运行,还支持在 Linux 和 Mac 平台上运行。
Tkinter 编写的程序,也称为 GUI 程序,GUI (Graphical User Interface)指的是“图形用户界面”,它是计算机图形学(CG)的一门分支,主要研究如何在计算机中表示图形,以及利用计算机进行图形的计算、处理和显示等相关工作。
GUI 这一概念并非 Python 语言独有,它属于计算机科学技术领域中的一个概念,比如使用 C/C++ 语言开发的 Qt、GTK、Electron 等都属于 GUI 软件包
环境使用
- Python 3.8
- Pycharm
模块使用
- tkinter
- PIL
代码部分
导入模块
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 = '你根本就没有密码, 你心里没数?' )
注册
def RegisterAnAccount ( ) :
top = tk .Toplevel ( )
top .title ( "扫码添加" )
top .geometry ( '640x750+500+500' )
# 导入图片
image = Image . open ( 'img.png' )
tk_image = ImageTk .PhotoImage (image )
# 在标签里放入图片
tk .Label (top , image =tk_image ) .pack ( )
top .mainloop ( )
最后效果
哔站视频号个人主页:松鼠爱吃饼干
700+ Python案例讲解视频