Python tkinter庫實現(xiàn)登錄注冊基本功能
tkinter庫作為python的標準庫之一,它的功能性十分強大,下面我將使用tkinter庫制作一個簡易的注冊登錄窗口(很難看就是了)。
一、各種組件的布局
制作之前需要大致明白各個窗體的大致位置,登錄注冊嘛 自然就要有用戶名和密碼的標簽以及需要輸入文字的文本框。
'創(chuàng)建用戶名的標簽' Label(root,text='用戶名',width = 6,font=('華文行楷',20)).place(x=1,y=1) '用戶名后面的輸入框' user = Entry(root,width=20,textvariable = user1).place(x=100,y=1) '創(chuàng)建密碼的標簽' Label(root,text = '密碼',width = 6,font=('華文行楷',20)).place(x=1,y=45) '密碼后面的輸入框' password = Entry(root,width =20,textvariable = password1 ).place(x=100,y=45) '創(chuàng)建兩個按鈕 用command參數(shù)使得在點擊按鈕時運行對應的函數(shù)' 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
創(chuàng)建一個空字典,我們可以設置一組數(shù)據(jù)作為默認用戶名密碼。
創(chuàng)建了一個儲存數(shù)據(jù) 的字典還不夠,我們要做到登錄注冊的效果還需要’登錄‘,’注冊‘這兩個按鈕的作用,當我們點擊注冊按鈕時,程序會捕捉到這個操作,并且執(zhí)行相應的操作。
'鼠標點擊登錄時會執(zhí)行的操作' 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)
注冊同理 當然這段代碼是執(zhí)行不了的只是提供個思路 具體后面會說 通過按鈕Button方法中的command參數(shù)我們就可以在點擊按鈕時調用我們事先寫好的函數(shù)。
三、制作過程中遇到的難點
在制作過程中遇到過一些難題,比如如何獲取文本框中用戶輸入的東西。
四、解決問題的方法
我查閱了很多資料,終于找到了方法,就是在Entry控件中的textvariable參數(shù) 它可以把這個輸入框對象的某個屬性綁定給一個變量 然后通過<變量>.StringVar() 方法獲取輸入框中的內容。
users = user1.get() #獲取user1中存儲的文本框中的內容 user = Entry(root,width=20,textvariable = user1).place(x=100,y=1) #textvariable參數(shù)的使用是把文本框中的內容傳遞給變量的關鍵
這個難題解決后,之后的工作也就簡單了,只需要把每個控件的位置,參數(shù)調整一下就可以了。
完整代碼以及運行結果如下:
#place布局.py from tkinter import * #導入tkinter庫 users_pw = {'admin':'123456'} #一個空字典 默認用戶名密碼為admin 123456 '點擊登錄按鈕執(zhí)行的函數(shù)' 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('沒有該用戶') '點擊注冊按鈕執(zhí)行的函數(shù)' 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() '窗口創(chuàng)建' root = Tk() root.title('登錄') root.geometry('300x300') root.config(bg = '#ffcc00') '創(chuàng)建兩個文本輸入框 并用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參數(shù)的使用是把文本框中的內容傳遞給變量的關鍵 Label(root,text = '密碼',width = 6,font=('華文行楷',20)).place(x=1,y=45) password = Entry(root,width =20,textvariable = password1 ).place(x=100,y=45) '創(chuàng)建兩個按鈕 用command參數(shù)使得在點擊按鈕時運行對應的函數(shù)' 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() #顯示窗口
到此這篇關于Python tkinter庫實現(xiàn)登錄注冊基本功能的文章就介紹到這了,更多相關Python tkinter登錄注冊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用Excel將數(shù)據(jù)寫入多個sheet
這篇文章主要介紹了Python使用Excel將數(shù)據(jù)寫入多個sheet,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05Python自定義聚合函數(shù)merge與transform區(qū)別詳解
這篇文章主要介紹了Python自定義聚合函數(shù)merge與transform區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05