日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?tkinter庫實現登錄注冊基本功能_python

作者:破壁人x ? 更新時間: 2023-02-06 編程語言

tkinter庫作為python的標準庫之一,它的功能性十分強大,下面我將使用tkinter庫制作一個簡易的注冊登錄窗口(很難看就是了)。

一、各種組件的布局

制作之前需要大致明白各個窗體的大致位置,登錄注冊嘛 自然就要有用戶名和密碼的標簽以及需要輸入文字的文本框。

'創建用戶名的標簽'
Label(root,text='用戶名',width = 6,font=('華文行楷',20)).place(x=1,y=1)
'用戶名后面的輸入框'
user = Entry(root,width=20,textvariable = user1).place(x=100,y=1)   
'創建密碼的標簽'
Label(root,text = '密碼',width = 6,font=('華文行楷',20)).place(x=1,y=45)
'密碼后面的輸入框'
password = Entry(root,width =20,textvariable = password1 ).place(x=100,y=45)
'創建兩個按鈕 用command參數使得在點擊按鈕時運行對應的函數'
Button(root,text = '登錄',width = 8,command= login).place(x=40,y=100)
Button(root,text = '注冊',width = 8,command=zhuce).place(x=110,y=100)

在這個基礎上我還加了登錄 注冊兩個按鈕 用于后面的登錄注冊的操作

二、制作過程中的理解

如果想要做到登錄注冊的操作,首先需要有一個儲存用戶名和密碼的東西,登錄時輸入的文本如果在這個東西里面說明用戶名密碼都正確 就能完成登錄,注冊時輸入的文本也能夠儲存在里面,要做到這些,字典是個不二選擇,因為它有著‘鍵值:內容’的結構,非常適合儲存用戶名和密碼

users_pw = {'admin':'123456'} #一個空字典 默認用戶名密碼為admin 123456

創建一個空字典,我們可以設置一組數據作為默認用戶名密碼。

創建了一個儲存數據 的字典還不夠,我們要做到登錄注冊的效果還需要’登錄‘,’注冊‘這兩個按鈕的作用,當我們點擊注冊按鈕時,程序會捕捉到這個操作,并且執行相應的操作。

'鼠標點擊登錄時會執行的操作'
def login():
    if users in users_pw :
        if passwords == users_pw[users]:
            print('登錄成功')
        else:
            print('登錄失敗')
    else:
        print('沒有該用戶')
 
Button(root,text = '登錄',width = 8,command= login).place(x=40,y=100)
Button(root,text = '注冊',width = 8,command=zhuce).place(x=110,y=100)

注冊同理 當然這段代碼是執行不了的只是提供個思路 具體后面會說 通過按鈕Button方法中的command參數我們就可以在點擊按鈕時調用我們事先寫好的函數。

三、制作過程中遇到的難點

在制作過程中遇到過一些難題,比如如何獲取文本框中用戶輸入的東西。

四、解決問題的方法

我查閱了很多資料,終于找到了方法,就是在Entry控件中的textvariable參數 它可以把這個輸入框對象的某個屬性綁定給一個變量 然后通過<變量>.StringVar() 方法獲取輸入框中的內容。

users = user1.get()      #獲取user1中存儲的文本框中的內容
user = Entry(root,width=20,textvariable = user1).place(x=100,y=1)      
#textvariable參數的使用是把文本框中的內容傳遞給變量的關鍵

這個難題解決后,之后的工作也就簡單了,只需要把每個控件的位置,參數調整一下就可以了。

完整代碼以及運行結果如下:

#place布局.py
from tkinter import *  #導入tkinter庫
users_pw = {'admin':'123456'}   #一個空字典 默認用戶名密碼為admin 123456
'點擊登錄按鈕執行的函數'
def login():
    def close():
        window.destroy()
    users = user1.get()
    passwords = password1.get()
    if users in users_pw :
        if passwords == users_pw[users]:
            window = Tk()
            window.title ('恭喜!')
            window.geometry('200x180+200+200')
            label = Label(window,text = '登錄成功',font=('華文行楷',20))
            label.pack()
            button = Button(window,text ='關閉',font=('華文行楷',20),command=close)
            button.pack()
 
        else:
            window = Tk()
            window.title('錯誤!')
            window.geometry('200x180+200+200')
            label = Label(window,text='登錄失敗',font=('華文行楷',20))
            label.pack()
            button = Button(window,text='關閉',font=('華文行楷',20), command=close)
            button.pack()
    else:
        print('沒有該用戶')
'點擊注冊按鈕執行的函數'
def zhuce():
    def close():
        window.destroy()
    users = user1.get()      #獲取user1中存儲的文本框中的內容
    passwords = password1.get() #同上
    if (users =='') or (passwords == ''):
        window = Tk()
        window.title('錯誤!')
        window.geometry('200x180+200+200')
        label = Label(window, text='注冊失敗', font=('華文行楷', 20))
        label.pack()
        button = Button(window, text='關閉', font=('華文行楷', 20), command=close)
        button.pack()
 
    elif users in users_pw:
        window = Tk()
        window.title('錯誤!')
        window.geometry('200x180+200+200')
        label = Label(window, text='注冊失敗已有此用戶名', font=('華文行楷', 20))
        label.pack()
        button = Button(window, text='關閉', font=('華文行楷', 20), command=close)
        button.pack()
    else:
        users_pw[users] = passwords
        window = Tk()
        window.title('恭喜!')
        window.geometry('200x180+200+200')
        label = Label(window, text='注冊成功', font=('華文行楷', 20))
        label.pack()
        button = Button(window, text='關閉', font=('華文行楷', 20), command=close)
        button.pack()
'窗口創建'
root = Tk()
root.title('登錄')
root.geometry('300x300')
root.config(bg = '#ffcc00')
'創建兩個文本輸入框 并用StringVar()把文本框中的內容跟user1 password1綁定'
user1 = StringVar ()
password1 =StringVar()
Label(root,text='用戶名',width = 6,font=('華文行楷',20)).place(x=1,y=1)
user = Entry(root,width=20,textvariable = user1).place(x=100,y=1)      #textvariable參數的使用是把文本框中的內容傳遞給變量的關鍵
Label(root,text = '密碼',width = 6,font=('華文行楷',20)).place(x=1,y=45)
password = Entry(root,width =20,textvariable = password1 ).place(x=100,y=45)
'創建兩個按鈕 用command參數使得在點擊按鈕時運行對應的函數'
Button(root,text = '登錄',width = 8,command= login).place(x=40,y=100)
Button(root,text = '注冊',width = 8,command=zhuce).place(x=110,y=100)
root.mainloop()  #顯示窗口

原文鏈接:https://blog.csdn.net/fuhao6363/article/details/128207637

欄目分類
最近更新