使用tkinter框架进行UI编程
首先是两个要导入的类
import tkinter
import tkinter.messagebox
class Login(object):
def __init__(self, root):
self.reg = ''
self.root = root
self.varName = tkinter.StringVar(value='')
self.varPwd = tkinter.StringVar(value='')
self.labelName = tkinter.Label(self.root, text='用户名:', justify=tkinter.RIGHT, width=80)
self.labelPwd = tkinter.Label(self.root, text='密码:', justify=tkinter.RIGHT, width=80)
self.entryName = tkinter.Entry(self.root, width=80, textvariable=self.varName)
self.entryPwd = tkinter.Entry(self.root, width=80, show='*', textvariable=self.varPwd)
self.buttonOk = tkinter.Button(self.root, text='登录', command=self.login)
self.buttonCancel = tkinter.Button(self.root, text='取消', command=self.cancel)
self.buttonExchanage = tkinter.Button(self.root, text='注册', command=self.exchange)
def arrange(self): #用place函数给各个元素定位
self.labelName.place(x=10, y=5, width=80, height=20)
self.labelPwd.place(x=10, y=30, width=80, height=20)
self.entryName.place(x=100, y=5, width=80, height=20)
self.entryPwd.place(x=100, y=30, width=80, height=20)
self.buttonOk.place(x=30, y=70, width=50, height=20)
self.buttonCancel.place(x=90, y=70, width=50, height=20)
self.buttonExchanage.place(x=150, y=70, width=50, height=20)
def login(self):
name = self.entryName.get()
pwd = self.entryPwd.get()
if name == '我' and pwd == '123456':
tkinter.messagebox.showinfo(title='Python tkinter', message='Ok')
else:
tkinter.messagebox.showerror('Python tkinter', message='Error')
def cancel(self):
self.varName.set('')
self.varPwd.set('')
def disappear(self): #删除页面上的所有架构 但保留root
self.labelName.destroy()
self.entryName.destroy()
self.labelPwd.destroy()
self.entryPwd.destroy()
self.buttonOk.destroy()
self.buttonCancel.destroy()
self.buttonExchanage.destroy()
def exchange(self): #为实现登录注册页面转化而准备的
self.disappear()
self.reg = Register(self.root)
self.reg.arrange()
tkinter.mainloop()
class Register(object):
def __init__(self, root):
self.log = ''
self.root = root
self.varName = tkinter.StringVar(value='')
self.varPwd1 = tkinter.StringVar(value='')
self.varPwd2 = tkinter.StringVar(value='')
self.labelName = tkinter.Label(self.root, text='用户名:', justify=tkinter.RIGHT, width=80)
self.labelPwd1 = tkinter.Label(self.root, text='密码:', justify=tkinter.RIGHT, width=80)
self.labelPwd2 = tkinter.Label(self.root, text='重复密码:', justify=tkinter.RIGHT, width=80)
self.entryName = tkinter.Entry(self.root, width=80, textvariable=self.varName)
self.entryPwd1 = tkinter.Entry(self.root, width=80, show='*', textvariable=self.varPwd1)
self.entryPwd2 = tkinter.Entry(self.root, width=80, show='*', textvariable=self.varPwd2)
self.bottonsubmit = tkinter.Button(self.root, text='提交', command=self.register)
self.bottonlogin = tkinter.Button(self.root, text='返回登录', command=self.exchange)
def register(self):
name = self.entryName.get()
pwd1 = self.entryPwd1.get()
pwd2 = self.entryPwd2.get()
if pwd2 is '' or pwd1 is '' or name is '':
tkinter.messagebox.showinfo(title='提示', message='请输入完整信息!')
elif pwd2 == pwd1:
tkinter.messagebox.showinfo(title='提示', message='注册成功!')
else:
tkinter.messagebox.showinfo(title='提示', message='密码不一致,请重新输入。')
self.cancel()
def cancel(self):
self.varPwd1.set('')
self.varPwd2.set('')
def arrange(self):
self.labelName.place(x=10, y=5, width=80, height=20)
self.entryName.place(x=100, y=5, width=80, height=20)
self.labelPwd1.place(x=10, y=30, width=80, height=20)
self.entryPwd1.place(x=100, y=30, width=80, height=20)
self.labelPwd2.place(x=10, y=55, width=80, height=20)
self.entryPwd2.place(x=100, y=55, width=80, height=20)
self.bottonsubmit.place(x=30, y=90, width=50, height=20)
self.bottonlogin.place(x=90, y=90, width=50, height=20)
def disappear(self):
self.labelName.destroy()
self.entryName.destroy()
self.labelPwd1.destroy()
self.entryPwd1.destroy()
self.labelPwd2.destroy()
self.entryPwd2.destroy()
self.bottonsubmit.destroy()
self.bottonlogin.destroy()
def exchange(self):
self.disappear()
self.log = Login(self.root)
self.log.arrange()
tkinter.mainloop()
def main():
root = tkinter.Tk()
root.title('Page')
root.geometry('200x200+20+20')
root.resizable(1,0)
root_login = Login(root)
root_login.arrange()
root.mainloop()
if __name__ == '__main__':
main()
本人还是小白入门,如果代码有问题,欢迎指正。
谢谢各位!