使用python的tkinter創(chuàng)建常用的登錄頁面(python用tkinter創(chuàng)建一個登錄界面)
一、要求:
1、使用python的tkinter編寫代碼
2、創(chuàng)建登錄頁面。要求:
①需要包含有圖片
②需要有賬號輸入框
③需要有密碼輸入框
④需要有提交按鈕,并進行校驗
⑤需要有清空按鈕,并能清空賬號密碼輸入信息
二、代碼如下:
import tkinter as tk #導(dǎo)入tkinter庫并設(shè)置為tkfrom tkinter import messagebox #從tkinter庫中導(dǎo)入messagebox類root=tk.Tk() #設(shè)置root窗體root.geometry('600x400') #設(shè)置root窗體的寬和高root.title('登錄頁面') #設(shè)置root窗體的標(biāo)題是登錄頁面canvas=tk.Canvas(root,width=600,height=400) #設(shè)置畫布變量canvas,置于root窗體中,寬為600,高為400canvas.place(x=10,y=10) #設(shè)置畫布的存放位置#設(shè)置圖片對象image_file,圖片位置是F:證券python代碼test1photo花朵.gifimage_file=tk.PhotoImage(file=r'F:證券python代碼test1photo花朵.gif')img=canvas.create_image(0,0,image=image_file) #在畫布里創(chuàng)建圖片,并插入image_file圖片label1=tk.Label(root,text='賬號:') #設(shè)置label1標(biāo)簽,置于root窗體中,文本是賬號label1.place(x=50,y=250) #將標(biāo)簽1置于x=50,y=250的像素點label2=tk.Label(root,text='密碼:') #設(shè)置label2標(biāo)簽,置于root窗體中,文本是密碼label2.place(x=50,y=300) #將標(biāo)簽1置于x=50,y=300的像素點v1=tk.StringVar() #設(shè)置v1為字符串對象v2=tk.StringVar() #設(shè)置v2為字符串對象username=tk.Entry(root,textvariable=v1) #設(shè)置username為輸入框?qū)ο螅糜趓oot窗體中,可變文本為v1username.place(x=100,y=250) #將username置于x=100,y=250的像素點password=tk.Entry(root,textvariable=v2) #設(shè)置password為輸入框?qū)ο?,置于root窗體中,可變文本為v2password.place(x=100,y=300) #將password置于x=100,y=300的像素點def submit(): #設(shè)置提交按鈕的方法submit if v1.get()=='zhangsan' and v2.get()=='123456': #假如賬號等于zhangsan,并且密碼等于123456 #則提示登錄成功 messagebox.showinfo(title='登錄成功',message='賬戶密碼正確,登錄成功') else: #否則 #提示登錄失敗 messagebox.showerror(title='登錄失敗',message='賬戶密碼錯誤')#定義提交按鈕submit_button,置于窗體root中,文本是提交,寬為10,當(dāng)被點擊時執(zhí)行submit命令submit_button=tk.Button(root,text='提交',width=10,command=submit)submit_button.place(x=50,y=350) #將submit_button置于x=50,y=350的像素點def clear(): #定義清空函數(shù)clear v1.set('') #將賬號清空 v2.set('') #將密碼清空 messagebox.showinfo(title='清空完成',message='賬戶密碼清空完成') #提示清空完成#定義清空按鈕clear_button,置于窗體root中,文本是清空,寬為10,當(dāng)被點擊時執(zhí)行clear命令clear_button=tk.Button(root,text='清空',width=10,command=clear)clear_button.place(x=165,y=350) #將clea_button置于x=165,y=350的像素點root.mainloop() #root窗體反復(fù)運行
三、運行結(jié)果如下圖所示: