Python登錄界面


生活中我們會進入各種登錄界面,那大家想制作一個屬於自己的登錄界面嗎?本篇文章小編就來分享如何使用Python制作簡單的登錄界面,下面是詳細的制作流程。
Python登錄界面

 

一、明確登錄界面的要素

一個簡單的登錄界面通常由以下要素組成

  1. 用戶賬號輸入框
  2. 用戶密碼輸入框
  3. 隨機驗證碼輸入框
  4. 登錄按鈕

二、創建代碼

from tkinter import *
import random
#定義函數,功能為生成有大寫字母、小寫字母、數字組成的6位隨機驗證碼
def creatAuthCode():
res1=""
res2=""
res3=""
for i in range(2):
num=random.randint(0,9)
res1+=str(num)
num=random.randint(65,91)
res2+=str(chr(num))
num=random.randint(97,123)
res3+=str(chr(num))
string=str(res1+res2+res3)
txt.set(string)
#創建根窗口
root=Tk()
root.title("登錄界面")
#root.resizable(False,False)
root.geometry("300x200")

#設置用戶賬號輸入框
ID=Label(root,text="賬號:")
ID.place(relx=0.2,rely=0.2,anchor=CENTER)
text1=Entry(root)
text1.place(relx=0.5,rely=0.2,anchor=CENTER,width=150,height=25)

#設置用戶密碼碼輸入框
password=Label(root,text="密碼:")
password.place(relx=0.2,rely=0.4,anchor=CENTER)
text2=Entry(root)
text2.place(relx=0.5,rely=0.4,anchor=CENTER,width=150,height=25)

#設置驗證碼輸入框
code=Label(root,text="驗證碼:")
code.place(relx=0.22,rely=0.6,anchor=CENTER)
code=Entry(root)
code.place(relx=0.4,rely=0.6,anchor=CENTER,width=70,height=25)

#設置獲取驗證碼的按鈕
txt=StringVar()
txt.set("獲取驗證碼")
codestr=Button(root,textvariable=txt,command=creatAuthCode,fg="red",bg="blue")
codestr.place(relx=0.8,rely=0.6,anchor=CENTER)
#設置登錄按鈕
enter=Button(root,text="登錄")
enter.place(relx=0.5,rely=0.8,anchor=CENTER)
root.mainloop()

二、運行代碼

下面我們來運行代碼,看看成果!

Python登錄界面

 

點擊按鈕獲取驗證碼

Python登錄界面


免責聲明!

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



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