Python tkinter 学习
使用Python简易的搭建一个登录页面
tkinter是什么?
Tkinter 是使用 python 进行窗口视窗设计的模块。Tkinter模块(“Tk 接口”)是Python的标准Tk GUI工具包的接口。作为 python 特定的GUI界面,是一个图像的窗口,tkinter是python 自带的,可以编辑的GUI界面,我们可以用GUI 实现很多直观的功能,比如想开发一个计算器,如果只是一个程序输入,输出窗口的话,是没用用户体验的。所有开发一个图像化的小窗口,就是必要的。
import tkinter as tk
from tkinter import messagebox # import this to fix messagebox error
import pickle
- 窗口搭建:首先要搭建一个窗口;
window = tk.Tk()
window.title('湖南理工学院研究生院信息中心登录窗口')
window.geometry('450x300')
- canvas画布控件:显示图形元素包括线条或者文本;
# welcome image
canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='welcome.gif')
image = canvas.create_image(0,0, anchor='nw', image=image_file)
canvas.pack(side='top')
所用的图片:
3. 输入账号密码的窗口
# user information
tk.Label(window, text='账号: ',bg = 'red').place(x=100, y= 150)
tk.Label(window, text='密码: ',bg = 'red').place(x=100, y= 190)
- StringVar函数:我们在使用界面编程的时候, 有些时候是需要跟踪变量的值的变化,以保证值的变更随时可以显示在界面上。由于python无法做到这一点,所以使用了tcl的相应的对象,也就是StringVar、BooleanVar、DoubleVar、IntVar所需要起到的作用。
var_usr_name = tk.StringVar()
var_usr_name.set('812111120129')#随便写了一个初始账号,学号凑数。
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
- 定义一个 登录函数
def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}#定义一个管理员账号
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='登录成功!', message='尊敬的' + usr_name + '用户')
else:
tk.messagebox.showerror(title='登录失败!',message='密码错误,请重试!')
else:
is_sign_up = tk.messagebox.askyesno('注册提示',
'你还没有注册该账号,是否现在注册?')
if is_sign_up:
usr_sign_up()
- 注册界面
def usr_sign_up():
def sign_to_liweixiang_Python():
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
if np != npf:
tk.messagebox.showerror('错误', '请保证输入密码一致!')
elif nn in exist_usr_info:
tk.messagebox.showerror('失败', '该账号已经注册过了!')
else:
exist_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('恭喜!', '您已经成功注册!')
window_sign_up.destroy()
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册窗口')
new_name = tk.StringVar()
new_name.set('')
tk.Label(window_sign_up, text='用户名: ').place(x=10, y= 10)
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
entry_new_name.place(x=150, y=10)
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='密码: ').place(x=10, y=50)
entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='确认密码: ').place(x=10, y= 90)
entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=150, y=90)
btn_comfirm_sign_up = tk.Button(window_sign_up, text='立即注册!', command=sign_to_Mofan_Python)
btn_comfirm_sign_up.place(x=150, y=130)
- 登录与注册按钮
# login and sign up button
btn_login = tk.Button(window, text='登 录', command=usr_login)
btn_login.place(x=170, y=230)
btn_sign_up = tk.Button(window, text='注 册', command=usr_sign_up)
btn_sign_up.place(x=240, y=230)
- 持续消息循环
window.mainloop()
演示
一、初始界面
二、登录界面
初始账号,登录需要注册
三、注册页面
四、登录成功