Python前端 Tkinter開發登錄界面

Python GUI界面開發的入門,是從tkinter開始的。(小程序,附帶嗎)
首先先介紹一下Tkinter吧
Tkinter模塊(“Tk 接口”)是Python的標準Tk GUI工具包的接口.
Tk和Tkinter可以在大多數的Unix平臺下使用,同樣可以應用在Windows和Macintosh系統裏.Tk8.0的後續版本可以實現本地窗口風格,並良好地運行在絕大多數平臺中

(親測)但是在windows系統下,目前Python2和Python3中,tkinter的編寫略有不同,我個人愚見,py3調用基本要大寫,py2調用基本要小寫。

下面是本人實驗的一個登錄小界面,主要由登錄界面和系統頁面構成。
先導入必要的包

import os
import Tkinter as tk
from Tkinter import *
import tkFileDialog as fdg
import tkMessageBox as msg
import pickle
from PIL import Image,ImageTk

登錄界面(主界面)的搭建

def get_image(filename,width,height):
    im=Image.open(filename).resize((width,height))
    return ImageTk.PhotoImage(im)

window=tk.Tk()
window.geometry('540x250')
window.title("人工智能助手")

canvs_window=tk.Canvas(window,width=540,height=250)
im_window=get_image('C:/Users/.../lg.jpg',540,250)
canvs_window.create_image(270,125,image=im_window)
canvs_window.pack()

界面的初始化

#登錄界面,用戶、密碼
label1=tk.Label(window,text='VIP賬號:')
label1.place(x=250,y=25)
label2=tk.Label(window,text='賬號密碼:')
label2.place(x=250,y=90)
    
var_usr_name=tk.StringVar()
entry_usr_name=tk.Entry(window,textvariable=var_usr_name)
entry_usr_name.place(x=330,y=25)
 
var_usr_pwd=tk.StringVar()
entry_usr_pwd=tk.Entry(window,textvariable=var_usr_pwd,show='*')
entry_usr_pwd.place(x=330,y=90)

用戶數據庫的建立

def usr_log_in():
    usr_name=var_usr_name.get() 
    user_pwd=var_usr_pwd.get()
    #從本地字典獲取用戶信息,如果沒有則新建本地數據庫
    try:
        with open('C:/Users/john/Desktop/usr_info.pickle','rb') as usr_file:
            usrs_info=pickle.load(usr_file)
    except IOError:
        with open('C:/Users/john/Desktop/usr_info.pickle','wb') as usr_file:
            usrs_info={'admin':'admin'}
            pickle.dump(usrs_info,usr_file)
    if usr_name in usrs_info:
        if user_pwd in usrs_info[usr_name]:
            msg.showinfo(title='welcome',message=usr_name+'歡迎登錄') 
            window.quit
            created()      
        else:
            msg.showerror(title='tip',message='密碼錯誤,請重新輸入!')
    elif usr_name=='' or user_pwd=='':
        msg.showerror(title='tip',message='用戶名或密碼爲空!')
    else:
        go_signup=msg.askyesno('歡迎','你還沒註冊,是否現在去註冊?')
        if go_signup:
            usr_sign_up()  

用戶登錄

def usr_sign_up():
    def signtowcg():
        #獲取輸入框內的內容
        nn=new_name.get()
        np=new_pwd.get()
        npf=new_pwd_confirm.get()
        #本地加載已有用戶信息,如果沒有則已有用戶信息爲空
        try:
            with open('C:/Users/.../usr_info.pickle','rb') as usr_file:
                exist_usr_info=pickle.load(usr_file)
        except IOError:
            exist_usr_info={}           
            
        #檢查用戶名存在、密碼爲空、密碼前後不一致
        if nn in exist_usr_info:
            msg.showerror('錯誤','用戶名已存在')
        elif np =='' or nn=='':
            msg.showerror('錯誤','用戶名或密碼爲空')
        elif np !=npf:
            msg.showerror('錯誤','密碼前後不一致')
 
        #註冊信息沒有問題則將用戶名密碼寫入數據庫
        else:
            exist_usr_info[nn]=np
            with open('C:/Users/john/Desktop/usr_info.pickle','wb') as usr_file:
                pickle.dump(exist_usr_info,usr_file)
            msg.showinfo('歡迎','註冊成功')
            #註冊成功關閉註冊框
            window_sign_up.destroy()
 
    #新建註冊界面
    window_sign_up=tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('註冊')
    #用戶名變量及標籤、輸入框
    new_name=tk.StringVar()
    tk.Label(window_sign_up,text='用戶名:').place(x=10,y=10)
    tk.Entry(window_sign_up,textvariable=new_name).place(x=150,y=10)

    #密碼變量及標籤、輸入框
    new_pwd=tk.StringVar()
    tk.Label(window_sign_up,text='請輸入密碼:').place(x=10,y=50)
    tk.Entry(window_sign_up,textvariable=new_pwd,show='*').place(x=150,y=50) 
 
    #重複密碼變量及標籤、輸入框
    new_pwd_confirm=tk.StringVar()
    tk.Label(window_sign_up,text='請再次輸入密碼:').place(x=10,y=90)
    tk.Entry(window_sign_up,textvariable=new_pwd_confirm,show='*').place(x=150,y=90)
 
    #確認註冊按鈕及位置
    bt_confirm_sign_up=tk.Button(window_sign_up,text='確認註冊',
                                 command=signtowcg)
    bt_confirm_sign_up.place(x=150,y=130)

用戶退出

def usr_sign_quit():
    window.destroy() 

子窗口搭建

def created():
    
    window_sign_up=tk.Toplevel(window)
    window_sign_up.geometry('500x300')
    window_sign_up.title('AI管家') 

    def Open_image():
        global Input_image, File
        File = fdg.askopenfilename(parent=window_sign_up,
                                      initialdir='C:/',
                                      title='選擇圖片')
        img = Image.open(File)
        img_resized = img.resize((28 * 4, 28 * 4), Image.ANTIALIAS)
        filename = ImageTk.PhotoImage(img_resized)
        Input_image = tk.Label(window_sign_up,image=filename)
        Input_image.image = filename
        Input_image.grid(row=0, column=0)
        return img_resized

    mainmenu = tk.Menu(window_sign_up)
    menuFile = tk.Menu(mainmenu)  # 菜單分組
    mainmenu.add_cascade(label="文件",menu=menuFile)
    menuFile.add_command(label="導入",command=Open_image)
    menuFile.add_separator()  # 分割線
    menuFile.add_command(label="退出",command=window.destroy)
    menuEdit = tk.Menu(mainmenu)  # 菜單分組
    mainmenu.add_cascade(label="編輯",menu=menuEdit)
    menuEdit.add_command(label="剪切")
    window_sign_up.config(menu=mainmenu)


    Input_image = tk.Label(window_sign_up,
                           width=200,
                           height=200,
                           bitmap='warning',
                           bg='white').grid(row=0, column=0, padx=20)
    
    testLabel = tk.Label(window_sign_up,
                         text="預測",  # 文本
                         font=('黑體', 10),  # 字體和大小
                         width=12,
                         height=2,  # 字體所佔的寬度和高度
                         ).grid(row=1, column=0, sticky='w', pady=5, padx=36)
    
    start = tk.Button(window_sign_up,
                      text='開始',
                      width=6, height=1,
                      ).grid(row=3, column=0, padx=200)

    testLabel.pack()
    start.pack()    

按鈕初始化

#按鈕,登錄、註冊、退出
bt_login=tk.Button(window,text="登錄",command=usr_log_in)
bt_login.place(x=250,y=180)
bt_sign_up=tk.Button(window,text="註冊",command=usr_sign_up)
bt_sign_up.place(x=350,y=180) 
bt_sign_quit=tk.Button(window,text="退出",command=usr_sign_quit)
bt_sign_quit.place(x=450,y=180) 

在加上窗口調試的命令語句,大功告成!

window.mainloop() 

還不快試試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章