为什么我点击“注册账户”后,弹出的新窗口没有显示任何文本内容?
主要问题代码如下:
#登录界面
def loadIt(self):
self.label1=tk.Label(text='用户登录')
self.label1.place(x=220,y=20)
self.label2=tk.Label(text='账户:')
self.label2.place(x=130,y=80)
self.label3=tk.Label(text='密码:')
self.label3.place(x=130,y=120)
self.button1=tk.Button(text='管理员登录?点这试试',font=('宋体',8),fg='#A4A4A4',bd=0,command=None)
self.button1.place(x=200,y=280)
self.button2 = tk.Button(text='注册账户', font=('黑体', 8), fg='#A4A4A4', bd=0, command=self.load_register)
self.button2.place(x=180, y=150)
self.button3 = tk.Button(text='忘记密码', font=('黑体', 8), fg='#A4A4A4', bd=0, command=None)
self.button3.place(x=280, y=150)
self.button4 = tk.Button(text='登录', width=8,height=1,command=None)
self.button4.place(x=220, y=200)
self.txt1 = tk.StringVar()
self.stxt1 = tk.Entry(self, textvariable=self.txt1)
self.stxt1.place(x=180, y=80, width=170, height=20)
self.txt2 = tk.StringVar()
self.stxt2 = tk.Entry(self, textvariable=self.txt2,show='●')
self.stxt2.place(x=180, y=120, width=170, height=20)
#注册页面
def load_register(self):
re = register()
re.mainloop()
完整代码如下:
import pymysql
import tkinter as tk
#连接数据库
def sql_link():
conn=pymysql.connect(host='localhost',user='root',port=3306,db='testdb',charset='utf8',passwd='021018zxh')
cur=conn.cursor()#生成游标对象
sql="select* from employees"#sql语句
cur.execute(sql)#执行sql语句
data=cur.fetchall()#通过fetchall方法获得数据
for i in data:
print(i)
cur.close()
conn.close()
class MainWindow(tk.Tk):
def __init__(self):
super(MainWindow, self).__init__()
self.title('我来上厕所')
self.resizable(False,False)
self.geometry('500x300+400+200')
self.loadIt()
#登录界面
def loadIt(self):
self.label1=tk.Label(text='用户登录')
self.label1.place(x=220,y=20)
self.label2=tk.Label(text='账户:')
self.label2.place(x=130,y=80)
self.label3=tk.Label(text='密码:')
self.label3.place(x=130,y=120)
self.button1=tk.Button(text='管理员登录?点这试试',font=('宋体',8),fg='#A4A4A4',bd=0,command=None)
self.button1.place(x=200,y=280)
self.button2 = tk.Button(text='注册账户', font=('黑体', 8), fg='#A4A4A4', bd=0, command=self.load_register)
self.button2.place(x=180, y=150)
self.button3 = tk.Button(text='忘记密码', font=('黑体', 8), fg='#A4A4A4', bd=0, command=None)
self.button3.place(x=280, y=150)
self.button4 = tk.Button(text='登录', width=8,height=1,command=None)
self.button4.place(x=220, y=200)
self.txt1 = tk.StringVar()
self.stxt1 = tk.Entry(self, textvariable=self.txt1)
self.stxt1.place(x=180, y=80, width=170, height=20)
self.txt2 = tk.StringVar()
self.stxt2 = tk.Entry(self, textvariable=self.txt2,show='●')
self.stxt2.place(x=180, y=120, width=170, height=20)
#注册页面
def load_register(self):
re = register()
re.mainloop()
class register(tk.Tk):
def __init__(self):
super(register, self).__init__()
self.title('欢迎')
self.resizable(False, False)
self.geometry('500x300+400+200')
self.interface()
def interface(self):
self.Rlabel1=tk.Label(text='欢迎注册《我来上厕所》',font=('黑体',12))
self.Rlabel1.place(x=160,y=20)
self.Rlabel2=tk.Label(text='给您一流的如厕体验~',font=('宋体',8))
self.Rlabel2.place(x=220,y=50)
self.Rlabel3=tk.Label(text='设置昵称:')
self.Rlabel3.place(x=110,y=80)
self.Rlabel4=tk.Label(text='设置密码:')
self.Rlabel4.place(x=110,y=120)
self.Rlabel5=tk.Label(text='验证密码:')
self.Rlabel5.place(x=110,y=160)
self.Rtxt1 = tk.StringVar()
self.Rstxt1 = tk.Entry(self, textvariable=self.Rtxt1)#昵称输入
self.Rstxt1.place(x=180, y=80, width=170, height=20)
self.Rtxt2 = tk.StringVar()
self.Rstxt2 = tk.Entry(self, textvariable=self.Rtxt2, show='●')#密码输入
self.Rstxt2.place(x=180, y=120, width=170, height=20)
self.Rtxt3 = tk.StringVar()
self.Rstxt3 = tk.Entry(self, textvariable=self.Rtxt3,show='●') #二次确认密码
self.Rstxt3.place(x=180, y=160, width=170, height=20)
self.Rbutton = tk.Button(text='立即注册', width=8, height=1, command=None)
self.Rbutton.place(x=220, y=200)
if __name__=='__main__':
mainwin=MainWindow()
mainwin.mainloop()
看完你的代码之后我是真觉得心累呀!代码给你,饶了我这一条小命吧!
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 5 10:34:10 2018
@author: Administrator
"""
import tkinter as tk
import tkinter.messagebox
import pickle
# 窗口
window = tk.Tk()
window.title('欢迎进入学习系统')
window.geometry('450x300')
# 画布放置图片
canvas = tk.Canvas(window, height=300, width=500)
imagefile = tk.PhotoImage(file='1.png')
image = canvas.create_image(0, 0, anchor='nw', image=imagefile)
canvas.pack(side='top')
# 标签 用户名密码
tk.Label(window, text='用户名:').place(x=100, y=150)
tk.Label(window, text='密码:').place(x=100, y=190)
# 用户名输入框
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
# 密码输入框
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
# 登录函数
def usr_log_in():
# 输入框获取用户名密码
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
# 从本地字典获取用户信息,如果没有则新建本地数据库
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
# 判断用户名和密码是否匹配
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='welcome',message='欢迎您:' + usr_name)
#window.destroy()
info_o = tk.Toplevel(window)
info_o.geometry('350x200')
info_o.title('注册')
else:
tk.messagebox.showerror(message='密码错误')
# 用户名密码不能为空
elif usr_name == '' or usr_pwd == '':
tk.messagebox.showerror(message='用户名或密码为空')
# 不在数据库中弹出是否注册的框
else:
is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
if is_signup:
usr_sign_up()
# 注册函数
def usr_sign_up():
# 确认注册时的相应函数
def signtowcg():
# 获取输入框内的内容
nn = new_name.get()
np = new_pwd.get()
npf = new_pwd_confirm.get()
# 本地加载已有用户信息,如果没有则已有用户信息为空
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
# 检查用户名存在、密码为空、密码前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('错误', '用户名已存在')
elif np == '' or nn == '':
tk.messagebox.showerror('错误', '用户名或密码为空')
elif np != npf:
tk.messagebox.showerror('错误', '密码前后不一致')
# 注册信息没有问题则将用户名密码写入数据库
else:
exist_usr_info[nn] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
# 注册成功关闭注册框
window_sign_up.destroy()
# 新建注册界面
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册')
# 用户名变量及标签、输入框
new_name = tk.StringVar()
tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
# 密码变量及标签、输入框
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
# 重复密码变量及标签、输入框
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
# 确认注册按钮及位置
bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
command=signtowcg)
bt_confirm_sign_up.place(x=150, y=130)
# 退出的函数
def usr_sign_quit():
window.destroy()
# 登录 注册按钮
bt_login = tk.Button(window, text='登录', command=usr_log_in)
bt_login.place(x=140, y=230)
bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
bt_logup.place(x=210, y=230)
bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
bt_logquit.place(x=280, y=230)
# 主循环
window.mainloop()
程序中的1.png: