新建一個data.txt檔案,存用戶名和密碼

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Benjamin
# @Time : 2021/10/11 20:33
# from tkinter import *
from tkinter import messagebox
from tkinter import Tk
import tkinter as tk
#創建表單
my_window=Tk()
my_window.title("登陸")
#設定視窗大小并居中顯示,
#螢屏的寬度和亮度
screen_width,screen_height=my_window.maxsize()#獲取當前螢屏的高度和寬度
#表單的寬度高度
width=250
height=200
#設定表單再螢屏中央顯示
align_str="%dx%d+%d+%d"%(width,height,(screen_width-width)/2,(screen_height-height)/2)
my_window.geometry(align_str)
#設定寬高不可縮放
my_window.resizable(width=False,height=False)
#添加標簽,賬戶,密碼
user_name_lable=tk.Label(my_window,text="賬號",font=('FangSong',14))
user_name_lable.place(x=30,y=30)
user_pwd_lable=tk.Label(my_window,text="密碼",font=('FangSong',14))
user_pwd_lable.place(x=30,y=70)
#賬號輸入框,輸入框文本設定
user_name_text=tk.StringVar()
user_name_text.set("輸入賬號")
user_name_entry=tk.Entry(my_window,textvariable=user_name_text,font=('FangSong',14),width=15)
user_name_entry.place(x=80,y=30)
#密碼輸入框,輸入框文本設定
user_pwd_text=tk.StringVar()#定義文本框
user_pwd_text.set("輸入密碼")#文本框提示語設定
user_pwd_entry=tk.Entry(my_window,textvariable=user_pwd_text,font=('FangSong',14),width=15)
user_pwd_entry.place(x=80,y=70)
#資料讀取,讀取data檔案
def read():
with open('data.txt','r') as file:
rows=file.readlines()
user_info_dict={}
#字典資料封裝
for row in rows:
dict_list=row.strip().split(':')#去掉前后空格,然后切割生成陣列
# print(dict_list)
user_info_dict[dict_list[0]]=dict_list[1]
return user_info_dict
#資料寫入,打開data檔案,寫入資料
def write(name,pwd):
with open('data.txt','a+') as file:#a+是追加寫入
file.write(name+":"+pwd+'\n')
#登陸按鈕事件處理
def user_login():
#獲取用戶輸入的賬號和密碼
name=user_name_text.get()
pwd=user_pwd_text.get()
print(name,pwd)
user_dict=read()
if name !='' and pwd !='':
if name in user_dict and user_dict[name]==pwd:
print("ok")
messagebox.showinfo(title="成功",message="歡迎"+name+"登陸到這個頁面")
else:
messagebox.showerror(title="錯誤",message="密碼或用戶名錯誤")
# print("密碼或用戶名錯誤")
else:
messagebox.showerror(title="錯誤",message="請輸入完整內容,用戶名和密碼不能為空!!!")
# print("請輸入完整內容,用戶名和密碼不能為空!!!")
#注冊事件的處理
def user_reg():
#獲取用戶名和密碼
name=user_name_text.get()
pwd=user_pwd_text.get()
print(name,pwd)
user_dict = read()
if name!=""and pwd!="":
if name not in user_dict:
write(name,pwd)
messagebox.showinfo(title="ok",message="注冊成功")
else:
messagebox.showerror(title="錯誤",message="用戶名已存在!!!")
else:
messagebox.showerror(title="錯誤",message="請輸入完整內容,用戶名和密碼不能為空!!!")
#按鈕,登陸按鈕,事件處理
user_login_button=tk.Button(my_window,text="登陸",font=('FangSong',14),command=user_login)
user_login_button.place(x=30,y=120)
#注冊,注冊按鈕,事件處理
user_reg_button=tk.Button(my_window,text="注冊",font=('FangSong',14),command=user_reg)
user_reg_button.place(x=150,y=120)
my_window.mainloop()




此專案分為兩步:
GUI撰寫程式
pyinstaller打包軟體
所用到的第三方庫:
具體實作
1. GUI界面
本次主要講解如何打包,所以在此只是設計一個十分簡單GUI界面本次主要講解如何打包,所以在此只是設計一個十分簡單GUI界面
如上文的代碼
2. 程式打包
首先一個程式都有該程式的圖示,所以要想讓自己的程式擁有更加亮眼的圖示的話,可以自己通過PS或者作圖軟體設計自己的圖示,然后通過網站http://www.bitbug.net/將自己設計的圖示改換成ico格式,該網站是可以將jpg、png、gif等影像檔案轉換為ico檔案,當然,本人在這里還給大家推薦一個更省時省力的辦法,那就是直接使用別人已經設計好的ico圖示,具體怎么操作呢,可以去http://www.iconpng.com下載,
圖示解決了,程式代碼撰寫好了,接下來就是最最重要的軟體打包環節了,
在此,我們用pyinstaller打包,這是一個用于pythonGUI打包軟體的第三方庫,
具體操作命令如下:
1,安裝pyinstaller
pip install pyinstaller (如果安裝失敗,用管理員權限安裝)
2,打包程式


(4)如果程式里面有圖片,需要將圖片復制到exe檔案同一級檔案夾,否則程式無法正常運行,默認生成的exe在dist檔案夾里面,必須拖出來放在和代碼同一目錄下,才能生效

打包原文鏈接:https://blog.csdn.net/weifuliu/article/details/82826643
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310583.html
標籤:python
