图形用户界面(Graphical User Interface)是指采用图形界面显示的计算机操作用户界面。 Python具有强大的GUI应用程序开发功能,tkinter库是python的标准GUI库,使用tkinter库可以快速的创建GUI应用程序。
import pymysql as pl
import tkinter as tk
from tkinter import messagebox
#创建窗体对象
mywin = tk.Tk()
#窗体标题
mywin.title("window登录界面")
#窗体大小
#mywin.geometry("400x300")
def myclick1():
#数据库的连接
conn = pl.connect(host="localhost", user="root", password="此处填写自己的密码", db="student")
cur = conn.cursor()
cur.execute("select *from user")
#获取表中的所有数据
data = cur.fetchall()
myn = myen1.get()
myp = myen2.get()
if myn == "" or myp == "":
messagebox.showinfo("提示对话框", "对不起,姓名和密码不能为空!")
else:
x = 0
for row in data:
username = row[0]
password = row[1]
x = (myn == username and myp == password)
if x==1:
break
if x==1:
messagebox.showinfo("提示对话框", "用户名和密码都正确,可以成功登录。")
else:
messagebox.showinfo("用户名或密码错误,无法成功登录!请重新输入。")
def myclick2():
kk.set("")
tt.set("")
#label控件及布局
mylab1= tk.Label(mywin, text="用户登录系统", font=("Arial 18 bold"))
mylab1.grid(row=0,column=0, columnspan=2)
mylab2 = tk.Label(mywin,text="姓名:" )
mylab2.grid(row=2,column=0,sticky="w")
kk = tk.StringVar()
myen1 = tk.Entry(mywin, text="", textvariable=kk)
myen1.grid(row=2, column=1,sticky= "w")
mylab3 = tk.Label(mywin, text="密码:")
mylab3.grid(row=3, column=0,sticky='w')
tt =tk.StringVar()
myen2 = tk.Entry(mywin, text='',show="*", textvariable=tt)
myen2.grid(row=3, column=1, sticky='w')
#按钮控件的布局
mybut1 = tk.Button(mywin, text="登录", command= myclick1)
mybut1.grid(row=4, column=0, ipadx=15)
mybut1 = tk.Button(mywin, text="清空",command= myclick2)
mybut1.grid(row=4, column=1, ipadx=15)
tk.mainloop()
博主目前本科在读,所写内容多为学习总结与记录,新人报道,难免存在不足之处,请各位大佬多多指教哈!