本節是Tkinter系列的最后一節,是一個綜合性的小實踐項目。
感謝莫煩大佬的教程,跟着他的教程一節一節的看過來的。大家可以百度能搜索到。
這個小項目是個典型的登錄窗口,其中包含了前面章節的很多知識點
比如canvas,Label,Entry等等,效果如圖,點擊Login和Sign up都會有對應的響應函數,大家可以試一試
代碼如下,其中需要注意的幾點
1.引入了pickle庫,這個是python的自帶庫,不需要另外的安裝,可以用來存儲數據
2.點擊Sign up,主窗口彈出了次級窗口 window_sign_up = tk.Toplevel(window)
import tkinter as tk import pickle import tkinter.messagebox # 定義窗口 window = tk.Tk() window.title('Welcome to my Tkinter Test') # 窗口title window.geometry('450x300') # 窗口尺寸 # 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') # user infomation - Label tk.Label(window, text='User name').place(x=50, y=150) tk.Label(window, text='Password').place(x=50, y=190) # user infomation - Entry var_user_name = tk.StringVar() var_user_name.set('lalal@qq.com') var_user_pwd = tk.StringVar() entry_user_name = tk.Entry(window, textvariable=var_user_name) entry_user_name.place(x=160, y=150) entry_user_pwd = tk.Entry(window, textvariable=var_user_pwd, show='*') entry_user_pwd.place(x=160, y=190) # Login and singn up button ''' Login部分 ''' def user_login(): user_name = entry_user_name.get() user_pwd = entry_user_pwd.get() # 第一次訪問時,文件肯定是不存在的,所以設置異常捕獲 # 沒有讀取到文件時,創建一個文件,並將管理員用戶密碼寫入 try: with open('users_info.pickle', 'rb') as user_file: user_info = pickle.load(user_file) except FileNotFoundError: with open('users_info.pickle', 'wb') as user_file: user_info = {'admin': 'admin'} pickle.dump(user_info, user_file) # 判斷user_name是否在user_info里面 if user_name in user_info: # 如果在,則判斷密碼是否正確,正確則進入系統,不正確則提示密碼錯誤 if user_pwd == user_info[user_name]: tk.messagebox.showinfo(title='welcome', message='How are you? ' + user_name) else: tk.messagebox.showerror(message="Error,your password is wrong,try again") # user_name不在user_info里面,則提示注冊 else: is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not sign up yet. Sign up today?') if is_sign_up == True: user_sign_up() else: tk.messagebox.showinfo(title='Bye', message='Bye~~') ''' sign up部分 ''' def user_sign_up(): def sign_to_system(): # 從signup窗口取出值 np = new_pwd.get() npf = new_pwd_confirm.get() nn = new_name.get() with open('users_info.pickle', 'rb') as user_file: exist_user_info = pickle.load(user_file) # 判斷兩次密碼是否一致 if np != npf: tk.messagebox.showerror(title='Eorror', message='兩次密碼輸入不一致') # 判斷用戶名是否存在 elif nn in exist_user_info: tk.messagebox.showerror(title='Error', message='用戶名已存在') # 將用戶名密碼寫入文件,並存在user_file中 else: exist_user_info[nn] = np with open('users_info.pickle', 'wb') as user_file: pickle.dump(exist_user_info, user_file) tk.messagebox.showinfo(title='Congratulations', message='注冊成功') # 銷毀窗口 window_sign_up.destroy() # 在主窗口下定義次級窗口 window_sign_up = tk.Toplevel(window) window_sign_up.geometry('350x200') window_sign_up.title('Sign up window') # 定義Label tk.Label(window_sign_up, text='User name:').place(x=10, y=10) # 定義entry - name new_name = tk.StringVar() new_name.set('hahah@qq.com') entry_new_name = tk.Entry(window_sign_up, textvariable=new_name) entry_new_name.place(x=150, y=10) # 定於 entry - pwd new_pwd = tk.StringVar() tk.Label(window_sign_up, text='Password').place(x=10, y=50) entry_new_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*') entry_new_pwd.place(x=150, y=50) # 定於 entry - pwd - confirm new_pwd_confirm = tk.StringVar() tk.Label(window_sign_up, text='Confirm Password').place(x=10, y=90) new_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*') new_pwd_confirm.place(x=150, y=90) # 定義Button btn_confirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_system) btn_confirm_sign_up.place(x=150, y=130) btn_login = tk.Button(window, text='Login', command=user_login) btn_login.place(x=170, y=230) btn_sign_up = tk.Button(window, text='Sign up', command=user_sign_up) btn_sign_up.place(x=270, y=230) window.mainloop()