代码运行前没报错,也能正常打开窗口,但一点击“log in”以后,pycharm就报错了(报错代码和图片在文章结尾)
thank you very much!
下面是代码:
import tkinter as tk
from tkinter import messagebox
import pickle
window = tk.Tk()
window.title('our speak')
window.geometry('800x550')
canvas = tk.Canvas(window, height=180, width=720)
image_file = tk.PhotoImage(file=r'C:\Users\HUAWEI\Desktop\logo.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')
tk.Label(window, text='User name:', font=('Arial', 18)).place(x=148, y=233)
tk.Label(window, text='Pass word:', font=('Arial', 18)).place(x=153, y=293)
var_usr_name = tk.StringVar()
var_usr_pwd = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(width=300, height=30)
entry_usr_name.place(x=310, y=235)
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='●')
entry_usr_pwd.place(width=300, height=30)
entry_usr_pwd.place(x=310, y=295)
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': 'cfy20080920'}
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name)
else:
tk.messagebox.showerror(message='Error, your password is wrong, try again.')
else:
is_sign_up = tk.messagebox.askyesno('Welcome',
'You have not signed up yet. Sign up today?')
if is_sign_up:
usr_sign_up()
def usr_sign_up():
def sign_to_mofan_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('Error', 'Password and confirm password must be the same!')
elif nn in exist_usr_info:
tk.messagebox.showerror('Error', 'The user has already signed up!')
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('Welcome!', 'You have successfully signed up!')
window_sign_up.destroy()
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('Sign up window')
new_name = tk.StringVar()
tk.Label(window_sign_up, text='User name: ').place(x=50, 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='Password: ').place(x=50, y=50)
entry_usr_psswd = tk.Entry(window_sign_up, textvariable=new_pwd, show='●')
entry_usr_psswd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').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='Sign up', command=sign_to_mofan_python)
btn_comfirm_sign_up.place(x=150, y=130)
btn_login = tk.Button(window, text='Login', font=('Arial', 12), command=usr_login, bg="light blue", height=2, width=30)
btn_login.place(x=320, y=345)
btn_sign_up = tk.Button(window, text='Sign up', font=('Arial', 10), command=usr_sign_up, bg="light blue", height=1,
width=8)
btn_sign_up.place(x=423, y=415)
window.mainloop()
报错代码:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\HUAWEI\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1892, in call
return self.func(*args)
File "D:\python\项目\our speak2.py", line 34, in usr_login
usrs_info = pickle.load(usr_file)
EOFError: Ran out of input
下面是图片原文件: