tkinter+pickle+python的一個登錄界面設計


1.代碼:

#導出模塊
import tkinter as tk
from tkinter import messagebox
import pickle

#定義登錄的窗口、標題、大小和位置
window = tk.Tk()
window.title('Welcome to Python')
window.geometry('800x500+800+200') 
#window.configure(bg='pink') #設置主窗口的configure的一個參數:bg(背景顏色)
window.config(bg='pink') #等同上面的configure=config

# 定義登錄窗口的一塊畫布canvas,放置一張圖片,welcome image
canvas = tk.Canvas(window, bg='black',height=140, width=550)
image_file = tk.PhotoImage(file='welcome1.gif') #默認目錄,本機是home/xgj/
image = canvas.create_image(100,0, anchor='nw', image=image_file) #坐標x=0,y=0,錨定在西北角,就是左上角
canvas.pack(side='top') #pack為頂格布局

# 登錄窗口的界面:user information
#2個標簽label
tk.Label(window, bg='pink',font=10,text='User name: ').place(x=50, y= 150)
tk.Label(window, bg='pink',font=10,text='Password: ').place(x=50, y= 250)

#定義輸入框和取值函數
var_usr_name = tk.StringVar()
var_usr_name.set('example@qq.com') #初始化信息
#對應的2個輸入框entry
entry_usr_name = tk.Entry(window, font=10,textvariable=var_usr_name)
entry_usr_name.place(x=260, y=150)

var_usr_pwd = tk.StringVar() #初始化為空
entry_usr_pwd = tk.Entry(window,font=10, textvariable=var_usr_pwd, show='*') #密碼顯示*,為不可見
entry_usr_pwd.place(x=260, y=250)

#登陸后的主窗口
def main():
    window = tk.Tk()
    window.title('xgj main GUI')
    window.geometry('1050x800+800+0')
    #---如果要設計main界面和窗口功能可以繼續定義---
    #---目前只是一個登錄和注冊界面的---
    #---如果設計,可以繼續添加---

#定義登錄函數
def usr_login(): #初次登錄,如果先點擊這個,就不會報錯,沒有usrs_info.pickle這個文件
    #獲取值的賦值
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    try:  #下面使用pickle模塊進行數據的管理和使用
          #如果沒有這個文件usrs_info.pickle,則會rb創建一個新的;如果有,則打開這個文件
          #在登錄窗口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='Welcome', message='How are you? ' + usr_name)
            window.destroy() #登錄界面的窗口銷毀、退出
            main() #進入主界面
        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: #如果是按鈕yes
            usr_sign_up() #進入usr_sign_up(注冊窗口)

#定義注冊界面register
def usr_sign_up(): #小bug,初次登錄或者注冊,如果先點擊這個就會報錯,沒有usrs_info.pickle文件
    def sign_to_Python():
        nn = new_name.get()
        np = new_pwd.get()
        npf = new_pwd_confirm.get()
        
        with open('usrs_info.pickle', 'rb') as usr_file:
            exist_usr_info = pickle.load(usr_file)
        
        #bug,在注冊界面,由於默認usr,密碼和確認密碼是空格,也能注冊,這是一個bug
        if np==npf=='': #增加一個判斷,不能是空格
            tk.messagebox.showerror('Error', 'Password and confirm password must not be empty!') 
        elif 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) #與下面的設置一樣,但有一個區別,就是set()初始值可以有
    #window_sign_up=tk.Tk() #與上面的設置一樣,但是new_name.set('example@python.com') #初始值傳不過去
    window_sign_up.title('Sign up window')
    window_sign_up.geometry('800x500+800+200')

    new_name = tk.StringVar()
    #如果采取tk.Toplevel(window),則下面的初始值可以傳過去,但是tk.Tk() 時,下面的初始值竟然傳不過去?
    new_name.set('example@qq.com') #初始值
    #定義label標簽和對應的entry---user name
    tk.Label(window_sign_up,font=10, text='User       name           : ').place(x=10, y= 10)
    entry_new_name = tk.Entry(window_sign_up,font=10,textvariable=new_name)
    entry_new_name.place(x=300, y=10)
    #定義label標簽和對應的entry---Password 
    new_pwd = tk.StringVar()
    tk.Label(window_sign_up, font=10,text='Password                   : ').place(x=10, y=60)
    entry_usr_pwd = tk.Entry(window_sign_up,font=10, textvariable=new_pwd, show='*')
    entry_usr_pwd.place(x=300, y=60)
    #定義label標簽和對應的entry---Confirm password
    new_pwd_confirm = tk.StringVar()
    tk.Label(window_sign_up, font=10,text='Confirm password: ').place(x=10, y= 110)
    entry_usr_pwd_confirm = tk.Entry(window_sign_up, font=10,textvariable=new_pwd_confirm, show='*')
    entry_usr_pwd_confirm.place(x=300, y=110)
    #定義按鈕---Sign up---就是注冊按鈕
    
    btn_comfirm_sign_up = tk.Button(window_sign_up,font=10, text='Sign up', command=sign_to_Python)
    btn_comfirm_sign_up.place(x=300, y=170)

# 登錄界面的2個按鈕---login and sign up button
btn_login = tk.Button(window,bg='yellow', font=10,text='Login', command=usr_login)
btn_login.place(x=260, y=350)
btn_sign_up = tk.Button(window, bg='yellow',font=10,text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=500, y=350)

window.mainloop()
View Code

 

2. 存在的問題bug:

2.1 就是如下圖所示,第1次打開登錄界面時,因為系統還沒有這個文件:usrs_info.pickle

2.2 所以,如果直接進入sign up(第2個按鈕,右下的那個按鈕),彈出一個sign up界面,是不能注冊資料,要報錯的

2.3 第1次,需要 login按鈕(第1個按鈕,坐下的那個按鈕)

圖1

3.圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM