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'] = '用戶名只能包含字母、數(shù)字、下劃線'
return False
#--------------------------獲取數(shù)據(jù)庫信息-----------------------------------
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)
#--------------------------登陸數(shù)據(jù)庫驗證用戶-----------------------------------
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", # 數(shù)據(jù)庫主機地址
user="root", # 數(shù)據(jù)庫用戶名
passwd="123456", # 數(shù)據(jù)庫密碼
port="66",
database="py_test"
)
'''
db_user_name = entry1.get()
mydb = mysql.connector.connect(
host=str(db_host), # 數(shù)據(jù)庫主機地址a
user=str(db_user), # 數(shù)據(jù)庫用戶名
passwd=str(db_pass), # 數(shù)據(jù)庫密碼
port=str(db_port),
database=str(db_data)
)
mycursor = mydb.cursor() #創(chuàng)建游標
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(),關(guān)閉窗口
import test
test
else:
label3['text'] = '用戶名或密碼錯誤,請重新輸入!'
def registered():
top.destroy()
import zc
zc
def down_zc():
label3['text'] = '注冊功能已關(guān)閉'
#---------------------------------窗口主體---------------------------------------------
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)
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比崇仁網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式崇仁網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務覆蓋崇仁地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
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='信息提示區(qū)', font=('華文新魏', '16'), relief = 'ridge', width = 30)
label3.grid(row=3, column=0, padx=10, pady=10, columnspan=2, sticky='s')
top.mainloop()
grid:網(wǎng)格布局
row=3, 行數(shù)值,從0開始,默認為0
column=0, 列數(shù)值,從0開始,默認為0
padx=10, x方向填充
pady=10, y方向填充
columnspan=2,
sticky='s' 方向 s,n,e,w
分享名稱:使用tkinter編寫登錄窗口
網(wǎng)頁路徑:http://jinyejixie.com/article2/ghopoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站排名、網(wǎng)站營銷、面包屑導航、外貿(mào)網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)