導言篇
我的python環(huán)境是:python3.6.5
這里我選擇的GUI編程包是:tkinter
tkinker在python2.5以后就是自帶包了,所以我們不需要另外安裝
tkinker相對與其他python GUI編程的包而已,是相對容易入手的
代碼篇
這是系統(tǒng)的登錄界面??
import tkinter from tkinter import messagebox class Login(object): def __init__(self): # 創(chuàng)建主窗口,用于容納其它組件 self.root = tkinter.Tk() # 給主窗口設(shè)置標題內(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)建一個賬號輸入框,并設(shè)置尺寸 self.input_account = tkinter.Entry(self.root, width=30) # 創(chuàng)建一個密碼輸入框,并設(shè)置尺寸 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ū)ο?
self.root.title("影視資源管理系統(tǒng)(離線版)") self.root.geometry('450x300')
給窗口root設(shè)置標題,并設(shè)置窗口
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是按鈕的相應(yīng)事件(command = self.siginUp_interface)這里的響應(yīng)事件的進行注冊/登錄進入后臺,command后接響應(yīng)函數(shù)。
.Entry(A): 輸入框,參照前面的.Label(),有疑問的可以在下方留言
.place(x="", y=""): 這個是設(shè)置窗口部件的函數(shù)
額。。。。登錄界面就介紹到這里了,后面我會繼續(xù)更新登錄界面的響應(yīng)機制,有不明的地方可以在下方留言,我看到會回復的
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
