溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用tkinter編寫登錄窗口

發(fā)布時(shí)間:2020-06-15 09:07:00 來(lái)源:網(wǎng)絡(luò) 閱讀:316 作者:瘋子的夢(mèng) 欄目:編程語(yǔ)言

import tkinter
import sys
import re
import mysql.connector
top = tkinter.Tk()
top.geometry('400x170+350+150')
top.wmtitle('綜合實(shí)例')
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ù)庫(kù)信息-----------------------------------
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ù)庫(kù)驗(yàn)證用戶-----------------------------------
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ù)庫(kù)主機(jī)地址
user="root", # 數(shù)據(jù)庫(kù)用戶名
passwd="123456", # 數(shù)據(jù)庫(kù)密碼
port="66",
database="py_test"
)
'''
db_user_name = entry1.get()
mydb = mysql.connector.connect(
host=str(db_host), # 數(shù)據(jù)庫(kù)主機(jī)地址a
user=str(db_user), # 數(shù)據(jù)庫(kù)用戶名
passwd=str(db_pass), # 數(shù)據(jù)庫(kù)密碼
port=str(db_port),
database=str(db_data)
)
mycursor = mydb.cursor() #創(chuàng)建游標(biāo)
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() #停止運(yùn)行top.mainloop(),關(guān)閉窗口
import test
test
else:
label3['text'] = '用戶名或密碼錯(cuò)誤,請(qǐng)重新輸入!'
def registered():
top.destroy()
import zc
zc
def down_zc():
label3['text'] = '注冊(cè)功能已關(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)

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='注冊(cè)', 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開(kāi)始,默認(rèn)為0
column=0, 列數(shù)值,從0開(kāi)始,默認(rèn)為0
padx=10, x方向填充
pady=10, y方向填充
columnspan=2,
sticky='s' 方向 s,n,e,w

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI