python GUI 之 tkinter

写一个 登陆窗口来学习 tkinter ,还剩下一些问题

python  GUI 之 tkinter

代码暂时如下


import tkinter as tk
import webbrowser
import pickle
from tkinter import messagebox class window(tk.Tk):
def get_winfo_size(self):
return self.winfo_screenwidth(),self.winfo_screenheight() def center_window(self,width,height):
screen_width, screen_height = self.get_winfo_size()
self.width = width
self.height = height
self.pos_x = int((screen_width - width) / 2)
self.pos_y = int((screen_height - height) / 2)
self.quit()
# print(self.winfo_reqwidth(), self.winfo_reqheight())
return super().geometry('{}x{}+{}+{}'.format(width, height, self.pos_x, self.pos_y)) def get_self_size(self):
return self.width,self.height class login(): def __init__(self):
self.user_file = './user_info.pickle'
login = window()
login.resizable(0,0)
login.title('登录窗口')
login.geometry('500x300')
login.center_window(500, 300) width, height = login.get_self_size()
print(width, height) # 头部窗口
head_frame = tk.Frame(login) # 画布 画背景图片
canvas = tk.Canvas(head_frame, bg='blue', height=100, width=500)
image_file = tk.PhotoImage(file='./image/bg.gif')
image = canvas.create_image(0, 0., anchor='nw', image=image_file)
canvas.pack()
head_frame.pack() # 中间 窗口
centroid_frame = tk.Frame(login) # 账号 密码框 登录 重置按钮
self.var_account_name = tk.StringVar()
self.var_psw = tk.StringVar() self.signup_window = None account_label = tk.Label(centroid_frame, text='账号', width=4, height=2)
account_label.grid(row=0, column=0, padx=5, pady=5) account_entry = tk.Entry(centroid_frame, width=30, show=None,textvariable=self.var_account_name)
account_entry.grid(row=0, column=1, padx=5, pady=5) psw_label = tk.Label(centroid_frame, text='密码', width=4, height=2)
psw_entry = tk.Entry(centroid_frame, width=30, show='*',textvariable=self.var_psw) psw_label.grid(row=1, column=0, padx=5, pady=5)
psw_entry.grid(row=1, column=1, padx=5, pady=5) register_label = tk.Label(centroid_frame, text="还没有账号?注册", fg="blue", cursor="hand2")
register_label.grid(row=1, column=2, padx=5, pady=5)
register_label.bind('<Button-1>',self.register_callback) centroid_frame.pack() # 尾部窗口
# tail_frame = tk.Frame(login) remember_psw = tk.Checkbutton(login, text='记住密码')
remember_psw.place(x=120, y=205, anchor='nw') auto_login = tk.Radiobutton(login, text='自动登录')
auto_login.place(x=210, y=205, anchor='nw') login_btn = tk.Button(login, text='登录',command=self.login_trigger)
reset_btn = tk.Button(login, text='重置',command=self.reset_trigger) login_btn.place(x=120, y=250, anchor='nw')
reset_btn.place(x=210, y=250, anchor='nw') # tail_frame.place(x=0,y=250,anchor='nw') # hyperlink = tkHyperlinkManager.HyperlinkManager(text) login.mainloop() def login_trigger(self):
usr_name = self.var_account_name.get()
psw = self.var_psw.get()
try:
with open(self.user_file,'rb') as usr_file:
user_info = pickle.load(usr_file)
except (FileNotFoundError,EOFError) as fe:
with open(self.user_file,'wb') as usr_file:
user_info = {'admin':'admin'}
pickle.dump(user_info,usr_file) if usr_name not in user_info:
messagebox.showwarning(title='账号信息提示框',message='用户不存在')
return
else:
if psw != user_info[usr_name]:
messagebox.showerror(title='密码警告提示框',message='密码错误')
else:
messagebox.showinfo(title='登陆成功消息框',message='恭喜!登陆成功!') def register_callback(self,event):
# webbrowser.open_new(r'http://www.baidu.com')
print('register callback...') self.signup_window = window()
self.signup_window.center_window(400, 250)
self.signup_window.title('注册窗口') # 注册 账号密码
self.signup_window.var_register_usr_name = tk.StringVar()
self.signup_window.var_register_usr_psw = tk.StringVar()
self.signup_window.var_register_usr_psw_confirm = tk.StringVar() text_frame = tk.Frame(self.signup_window) register_account_label = tk.Label(text_frame,text='账号',width=4,height=2)
register_account_entry = tk.Entry(text_frame,width=30,show=None,textvariable=self.signup_window.var_register_usr_name) register_psw_label = tk.Label(text_frame,text='密码',width=4,height=2)
register_psw_entry = tk.Entry(text_frame,width=30,show='*',textvariable=self.signup_window.var_register_usr_psw) register_psw_confirm_label = tk.Label(text_frame,text='确认密码',width=8,height=2)
register_psw_confirm_entry = tk.Entry(text_frame,width=30,show='*',textvariable=self.signup_window.var_register_usr_psw_confirm) # register_account_label.place(x=0,y=20,anchor='nw')
# register_account_entry.place(x=30,y=20,anchor='nw')
# register_psw_label.place(x=0,y=60,anchor='nw')
# register_psw_entry.place(x=30,y=60,anchor='nw') register_account_label.grid(row=0,column=0,padx=15,pady=10)
register_account_entry.grid(row=0,column=1,padx=15,pady=10) register_psw_label.grid(row=1,column=0,padx=15,pady=10)
register_psw_entry.grid(row=1,column=1,padx=15,pady=10) register_psw_confirm_label.grid(row=2,column=0,padx=15,pady=10)
register_psw_confirm_entry.grid(row=2,column=1,padx=15,pady=10) text_frame.pack() # 注册按钮
register_button = tk.Button(self.signup_window,text='注册',command=self.register_user)
register_button.pack() self.signup_window.mainloop() def register_user(self):
register_usr_name = self.signup_window.var_register_usr_name.get()
register_psw = self.signup_window.var_register_usr_psw.get()
register_psw_confirm = self.signup_window.var_register_usr_psw_confirm.get() # 用户名 规则, 密码 规则 , 正则等待完善
print('register_usr_name %s' %register_usr_name)
print(register_psw , register_psw_confirm) if register_psw != register_psw_confirm:
messagebox.showerror(title='密码不相等提示框',message='两次输入密码不等!')
else:
try:
with open(self.user_file) as usr_file:
register_user_info = {register_usr_name:register_psw}
print(register_user_info)
pickle.dump(register_user_info,usr_file)
self.signup_window.destroy()
except (FileNotFoundError,EOFError) as e:
print(e) def reset_trigger(self):
self.var_account_name.set('') # 记得这里用 set 置空
self.var_psw.set('') if __name__ == '__main__':
log = login()
上一篇:HDU 4946 Area of Mushroom (几何凸包)


下一篇:C++数据结构之二叉树