您好,登錄后才能下訂單哦!
import tkinter
import sys
import re
import mysql.connector
top = tkinter.Tk()
top.geometry('400x170+350+150')
top.wmtitle('綜合實例')
top.resizable(width=False, height=False) 禁止修改窗口大小
def validateText():
val = entry1.get()
pwd = entry2.get()
if re.findall('^[0-9a-zA-Z]{1,}$', str(val)):
return True
else:
label3['text'] = '用戶名只能包含字母、數字、下劃線'
return False
#--------------------------獲取數據庫信息-----------------------------------
file = open('mysql.conf', 'r')
f = file.readlines()
l = " ".join(f)
ip = re.findall(r"host=(.\S+)", l)
us = re.findall(r"user=(.\S+)", l)
pd = re.findall(r"passwd=(.\S+)", l)
pt = re.findall(r"port=(.\S+)", l)
db = re.findall(r"database=(.\S+)", l)
#--------------------------登陸數據庫驗證用戶-----------------------------------
def anw_button(db_host=ip[0], db_user=us[0], db_pass=pd[0], db_port=pt[0], db_data=db[0]):
'''
mydb = mysql.connector.connect(
host="xx.xx.xx.xx", # 數據庫主機地址
user="root", # 數據庫用戶名
passwd="123456", # 數據庫密碼
port="66",
database="py_test"
)
'''
db_user_name = entry1.get()
mydb = mysql.connector.connect(
host=str(db_host), # 數據庫主機地址a
user=str(db_user), # 數據庫用戶名
passwd=str(db_pass), # 數據庫密碼
port=str(db_port),
database=str(db_data)
)
mycursor = mydb.cursor() #創建游標
mycursor.execute("select * FROM py_table where name = " + db_user_name)
results = mycursor.fetchall()
if results[0][0] != "":
db_user_pwd = results[0][1]
if str.upper(entry1.get()) == db_user_name and str.upper(entry2.get()) == db_user_pwd:
label3['text'] = '登陸成功'
top.destroy() #停止運行top.mainloop(),關閉窗口
import test
test
else:
label3['text'] = '用戶名或密碼錯誤,請重新輸入!'
def registered():
top.destroy()
import zc
zc
def down_zc():
label3['text'] = '注冊功能已關閉'
#---------------------------------窗口主體---------------------------------------------
label1 = tkinter.Label(top, text='用戶名:', font=('宋體', '18'))
label1.grid(row=0, column=0)
label2 = tkinter.Label(top, text='密碼:', font=('宋體', '18'))
label2 .grid(row=1, column=0)
v = tkinter.StringVar()
entry1 = tkinter.Entry(top, font=('宋體', '18'), textvariable = v, validate = 'focusout', validatecommand = validateText)
entry1.grid(row=0, column=1)
entry1.focus_force()
entry2 = tkinter.Entry(top, font=('宋體', '18'), show = '*')
entry2.grid(row=1, column=1)
button1 = tkinter.Button(top, text='登陸', font=('宋體', '18'), command = anw_button)
button1.grid(row=2, column=0, padx=50, pady=10)
button2 = tkinter.Button(top, text='注冊', font=('宋體', '18'), command = registered)
button2.grid(row=2, column=1, padx=80, pady=10)
label3 = tkinter.Label(top, text='信息提示區', font=('華文新魏', '16'), relief = 'ridge', width = 30)
label3.grid(row=3, column=0, padx=10, pady=10, columnspan=2, sticky='s')
top.mainloop()
grid:網格布局
row=3, 行數值,從0開始,默認為0
column=0, 列數值,從0開始,默認為0
padx=10, x方向填充
pady=10, y方向填充
columnspan=2,
sticky='s' 方向 s,n,e,w
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。