python+tkinter 简单的登录窗口demo
一个简单的登录窗口布局,可以用于日常快速搭建一个简单的窗口类。
from tkinter import * import tkinter.messagebox class LoginUi: def __init__(self,window): self.window = window #初始化窗口 def initWindow(self): self.width = 200 self.height = 100 #设置窗口宽高和位子 self.window.geometry("%dx%d+%d+%d" % (self.width, self.height, (self.window.winfo_screenwidth() - self.width)/2, (self.window.winfo_screenheight()-self.height)/2)) #标题 self.window.title("登录窗口") #标签 self.userNameLabel = Label(self.window, text="帐号:") self.userNameLabel.grid(row=0, column=1, columnspan=1) #输入框 self.userNameEntry = Entry(self.window, bd=5) self.userNameEntry.grid(row=0, column=2, columnspan=1) self.passwordLabel = Label(self.window, text="密码:") self.passwordLabel.grid(row=1, column=1, columnspan=1) self.passwordEntry = Entry(self.window, bd=5) self.passwordEntry.grid(row=1, column=2, columnspan=1) #按钮 self.checkButton = Button(self.window, text="登录", command=self.checkCallBack) self.checkButton.grid(row=2, column=2, columnspan=1) def checkCallBack(self): if self.userNameEntry.get() == "" or self.passwordEntry.get() == "": tkinter.messagebox.showinfo('提示','请输入帐号或者密码!') if self.userNameEntry.get() == "123" and self.passwordEntry.get() == "123": tkinter.messagebox.showinfo('提示','密码正确!') else: tkinter.messagebox.showinfo('提示', '密码错误!') def login(): loginWindow = Tk() #创建窗口 window = LoginUi(loginWindow) #类初始化 window.initWindow() #窗口添加组件 loginWindow.mainloop() #载入窗口循环 login()
效果如下: