代码如下:
from tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.creatWidget()
def creatWidget(self):
self.Label01 = Label(self, text='用户名')
self.Label01.pack()
v1 = StringVar()
self.entry01 = Entry(self, textvariable=v1)
self.entry01.pack()
self.Label02 = Label(self, text='密码')
self.Label02.pack()
v2 = StringVar()
self.entry02 = Entry(self, textvariable=v2, show="*")
self.entry02.pack()
Button(self, text="登录", command=self.login).pack()
def login(self):
username = self.entry01.get()
pwd = self.entry02.get()
print("去数据库比对用户名和密码!")
print("用户名:"+username)
print("密码:"+pwd)
#abcd和123321分别是用户名、密码,可以随意修改
if username=='abcd' and pwd=="123321":
messagebox.showinfo("xx登录系统", "登录成功!")
else:
messagebox.showinfo("xx登录系统","用户名或密码错误!")
if __name__=='__main__':
root = Tk()
root.geometry("400x130+300+200") #四个数字分别代表窗口宽度,高度,距屏幕左边,上边距离
app = Application(master=root)
root.mainloop()