python tkinter数据库通讯录_TKINTER--GUI连接数据库登陆

#-*- coding:UTF-8 -*-

import tkinter.messagebox

import tkinter as tk

import pymysql

# 构造窗口

window = tk.Tk()

window.title('登陆窗口')

window.geometry('450x300')

# 构造画布

canvas = tk.Canvas(window,height=200,width=500)

image_file = tk.PhotoImage(file='welcome.gif')

image=canvas.create_image(0,0,anchor='nw',image=image_file)

canvas.pack(side='top')

tk.Label(window,text='Username:').place(x=50,y=150)

tk.Label(window,text='Password:').place(x=50,y=190)

var_user_name = tk.StringVar()

var_user_name.set('example@python.com')

entry_user_name = tk.Entry(window,textvariable=var_user_name)

entry_user_name.place(x=160,y=150)

var_user_password=tk.StringVar()

entry_user_password = tk.Entry(window,textvariable=var_user_password,show='*')

entry_user_password.place(x=160,y=190)

def user_login():

# 获取label中的输入

user_name = var_user_name.get()

user_password = var_user_password.get()

db = pymysql.connect(host="localhost", user="root",

password="123456", db="test", port=3306)

try:

cursor = db.cursor()

sql='select * from users_name where username = "'"%s"'";'%user_name #动态sql拼接

cursor.execute(sql)

result = cursor.fetchall()

if user_name == result[0][1] and user_password == result[0][2]:

tk.messagebox.showinfo(title='welcome',message='How are you? ' + str(user_name))

else:

tk.messagebox.showerror(message='Erro,your password is wrong,try again!')

except Exception as e:

tk.messagebox.showerror(e)

finally:

db.close()

def user_registe():

pass

# 构造登陆/注册按钮

btn_login = tk.Button(window,text='Login',command=user_login)

btn_login.place(x=170,y=230)

btn_sign_up = tk.Button(window,text='Regist',command=user_registe)

btn_sign_up.place(x=270,y=230)

window.mainloop()

注册的还没写完,今天就先到这里!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值