您好,登錄后才能下訂單哦!
使用Python怎么實現(xiàn)登錄系統(tǒng)界面?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
import tkinter from tkinter import messagebox class Login(object): def __init__(self): # 創(chuàng)建主窗口,用于容納其它組件 self.root = tkinter.Tk() # 給主窗口設置標題內(nèi)容 self.root.title("影視資源管理系統(tǒng)(離線版)") self.root.geometry('450x300') #運行代碼時記得添加一個gif圖片文件,不然是會出錯的 self.canvas = tkinter.Canvas(self.root, height=200, width=500)#創(chuàng)建畫布 self.image_file = tkinter.PhotoImage(file='welcome_1.gif')#加載圖片文件 self.image = self.canvas.create_image(0,0, anchor='nw', image=self.image_file)#將圖片置于畫布上 self.canvas.pack(side='top')#放置畫布(為上端) #創(chuàng)建一個`label`名為`Account: ` self.label_account = tkinter.Label(self.root, text='Account: ') #創(chuàng)建一個`label`名為`Password: ` self.label_password = tkinter.Label(self.root, text='Password: ') # 創(chuàng)建一個賬號輸入框,并設置尺寸 self.input_account = tkinter.Entry(self.root, width=30) # 創(chuàng)建一個密碼輸入框,并設置尺寸 self.input_password = tkinter.Entry(self.root, show='*', width=30) # 創(chuàng)建一個登錄系統(tǒng)的按鈕 self.login_button = tkinter.Button(self.root, command = self.backstage_interface, text = "Login", width=10) # 創(chuàng)建一個注冊系統(tǒng)的按鈕 self.siginUp_button = tkinter.Button(self.root, command = self.siginUp_interface, text = "Sign up", width=10) # 完成布局 def gui_arrang(self): self.label_account.place(x=60, y= 170) self.label_password.place(x=60, y= 195) self.input_account.place(x=135, y=170) self.input_password.place(x=135, y=195) self.login_button.place(x=140, y=235) self.siginUp_button.place(x=240, y=235) # 進入注冊界面 def siginUp_interface(self): # self.root.destroy() tkinter.messagebox.showinfo(title='影視資源管理系統(tǒng)', message='進入注冊界面') # 進行登錄信息驗證 def backstage_interface(self): account = self.input_account.get().ljust(10," ") password = self.input_password.get().ljust(10," ") #對賬戶信息進行驗證,普通用戶返回user,管理員返回master,賬戶錯誤返回noAccount,密碼錯誤返回noPassword verifyResult = verifyAccount.verifyAccountData(account,password) if verifyResult=='master': self.root.destroy() tkinter.messagebox.showinfo(title='影視資源管理系統(tǒng)', message='進入管理界面') elif verifyResult=='user': self.root.destroy() tkinter.messagebox.showinfo(title='影視資源管理系統(tǒng)', message='進入用戶界面') elif verifyResult=='noAccount': tkinter.messagebox.showinfo(title='影視資源管理系統(tǒng)', message='該賬號不存在請重新輸入!') elif verifyResult=='noPassword': tkinter.messagebox.showinfo(title='影視資源管理系統(tǒng)', message='賬號/密碼錯誤請重新輸入!') def main(): # 初始化對象 L = Login() # 進行布局 L.gui_arrang() # 主程序執(zhí)行 tkinter.mainloop() if __name__ == '__main__': main()
效果篇
語法介紹
環(huán)境配置:
Python3.6.5,前往官網(wǎng)下載
tkinker包:Python2.5之后,tkinker包是自帶的,我們直接導入就好了
基本語法:
self.root = tkinter.Tk()
創(chuàng)建一個窗口對象root,root前面的self.是面向?qū)ο罄锩娴膬?nèi)容,不明白的童鞋可以去Google一下面向?qū)ο?/p>
self.root.title("影視資源管理系統(tǒng)(離線版)") self.root.geometry('450x300')
給窗口root設置標題,并設置窗口
self.canvas = tkinter.Canvas(self.root, height=200, width=500)#創(chuàng)建畫布 self.image_file = tkinter.PhotoImage(file='welcome_1.gif')#加載圖片文件 self.image = self.canvas.create_image(0,0, anchor='nw', image=self.image_file)#將圖片置于畫布上 self.canvas.pack(side='top')#放置畫布(為上端)
如果我們需要讓自己的界面在美觀上加分,大可以試試創(chuàng)建一個畫布,也就是下面這個東西
我這里是先對圖片背景進行了透明化處理,需要的小伙伴可以去這里 對圖片進行處理,個人覺得這個網(wǎng)站還是不錯的
#創(chuàng)建一個`label`名為`Account: ` self.label_account = tkinter.Label(self.root, text='Account: ') #創(chuàng)建一個`label`名為`Password: ` self.label_password = tkinter.Label(self.root, text='Password: ')
這里創(chuàng)建的是一個label,label是什么不明白可以參考上面貼圖的“Account:”與“Password:”
.Label(A, B):參數(shù)A代表Lable依賴窗口,參數(shù)B即用戶可見的Lable的名字了(text="LableName")
.Button(A, B, text='', [width='', height='']):參數(shù)A是按鈕依賴的窗口主體,參數(shù)B是按鈕的相應事件(command = self.siginUp_interface)這里的響應事件的進行注冊/登錄進入后臺,command后接響應函數(shù)。
.Entry(A):輸入框,參照前面的.Label(),有疑問的可以在下方留言
.place(x="", y=""):這個是設置窗口部件的函數(shù)
看完上述內(nèi)容,你們掌握使用Python怎么實現(xiàn)登錄系統(tǒng)界面的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。