[英]tkinter: AttributeError: 'str' object has no attribute 'tk'
I'm writing code for a login system using the tkinter module.我正在使用 tkinter 模块为登录系统编写代码。 Python is returning an attribute error, I'm only just learning the tkinter module. Python 返回一个属性错误,我只是在学习 tkinter 模块。 help would be appreciated.帮助将不胜感激。 The code is being run from another file using the exec command.代码正在使用 exec 命令从另一个文件运行。 Here is my code:这是我的代码:
#import modules
from tkinter import *
import os
import pygame
from Mathletics_V2 import *
# Designing window for registration
def register():
global register_screen
register_screen = Toplevel(main_screen)
register_screen.title("Register")
register_screen.geometry("300x250")
global username
global password
global username_entry
global password_entry
username = StringVar()
password = StringVar()
Label(register_screen, text="Please enter details below", bg="blue").pack()
Label(register_screen, text="").pack()
username_lable = Label(register_screen, text="Username * ")
username_lable.pack()
username_entry = Entry(register_screen, textvariable=username)
username_entry.pack()
password_lable = Label(register_screen, text="Password * ")
password_lable.pack()
password_entry = Entry(register_screen, textvariable=password, show='*')
password_entry.pack()
Label(register_screen, text="").pack()
Button(register_screen, text="Register", width=10, height=1, bg="blue", command = register_user).pack()
# Designing window for login
def login():
global login_screen
login_screen = Toplevel(main_screen)
login_screen.title("Login")
login_screen.geometry("300x250")
Label(login_screen, text="Please enter details below to login").pack()
Label(login_screen, text="").pack()
global username_verify
global password_verify
username_verify = StringVar()
password_verify = StringVar()
global username_login_entry
global password_login_entry
Label(login_screen, text="Username * ").pack()
username_login_entry = Entry(login_screen, textvariable=username_verify)
username_login_entry.pack()
Label(login_screen, text="").pack()
Label(login_screen, text="Password * ").pack()
password_login_entry = Entry(login_screen, textvariable=password_verify, show= '*')
password_login_entry.pack()
Label(login_screen, text="").pack()
Button(login_screen, text="Login", width=10, height=1, command =login_verify).pack()
# Implementing event on register button
def register_user():
username_info = username.get()
password_info = password.get()
file = open(username_info, "a+")
file.write(username_info + "\n")
file.write(password_info + "\n")
file.close()
username_entry.delete(0, END)
password_entry.delete(0, END)
Label(register_screen, text="Registration Success", fg="green", font=("calibri", 11)).pack()
# Implementing event on login button
def login_verify():
global username1
username1 = username_verify.get()
password1 = password_verify.get()
username_login_entry.delete(0, END)
password_login_entry.delete(0, END)
list_of_files = os.listdir()
if username1 in list_of_files:
file1 = open(username1, "r")
print(username1)
verify = file1.read().splitlines()
if username1 == 'admin':
tMain_Screen()
elif password1 in verify:
login_sucess()
else:
password_not_recognised()
else:
user_not_found()
# Designing popup for login success
def login_sucess():
global login_success_screen
login_success_screen = Toplevel(login_screen)
login_success_screen.title("Success")
login_success_screen.geometry("150x100")
Label(login_success_screen, text="Login Success").pack()
Button(login_success_screen, text="OK", command=delete_login_success).pack()
game_intro()
# Designing popup for login invalid password
def password_not_recognised():
global password_not_recog_screen
password_not_recog_screen = Toplevel(login_screen)
password_not_recog_screen.title("Success")
password_not_recog_screen.geometry("150x100")
Label(password_not_recog_screen, text="Invalid Password ").pack()
Button(password_not_recog_screen, text="OK", command=delete_password_not_recognised).pack()
# Designing popup for user not found
def user_not_found():
global user_not_found_screen
user_not_found_screen = Toplevel(login_screen)
user_not_found_screen.title("Success")
user_not_found_screen.geometry("150x100")
Label(user_not_found_screen, text="User Not Found").pack()
Button(user_not_found_screen, text="OK", command=delete_user_not_found_screen).pack()
# Deleting popups
def delete_login_success():
login_success_screen.destroy()
def delete_password_not_recognised():
password_not_recog_screen.destroy()
def delete_user_not_found_screen():
user_not_found_screen.destroy()
# Designing Main(first) window
def main_account_screen():
global main_screen
main_screen = Tk()
main_screen.geometry("300x250")
main_screen.title("Account Login")
Label(text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(text="Login", height="2", width="30", command = login).pack()
Button(text="Registration", height="2", width="30", command= register).pack()
Button(text="Quit", height="2", width="30", command= quit).pack()
main_screen.mainloop()
#Teacher Screen
def tStudent_Progress():
global Student_Progress_Screen
Student_Progress_Screen = Toplevel(Main_Screen)
Student_Progress_Screen.title("Student Progress")
Student_Progress_Screen.geometry("500x500")
Label(Student_Progress_Screen,text="Student Progress").pack()
#Use while loop to list buttons for registered students
def tgame():
game_intro()
#def tutorial():
#Function to Change Tutorial Links
def tMain_Screen():
global tmain_Screen
tmain_Screen = Toplevel(login_screen)
tmain_Screen.geometry("300x400")
tmain_Screen.title("Admin Interface")
Label("tmain_screen",text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
## Button(text="Student Progess",height="2", width=" 30", command = tStudent_Progress).pack()
## Button(text="Game", height="2", width="30", command = tgame).pack()
## #Button(text="Tutorial",height="2",width="30", command = ttutorial).pack()
## Label(text="").pack()
## Button(text="Quit",height="2", width="30",command = quit).pack()
main_account_screen()
Here is my error:这是我的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Ethan\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "<string>", line 100, in login_verify
File "<string>", line 187, in tMain_Screen
File "C:\Users\Ethan\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Ethan\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2561, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Users\Ethan\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2530, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
I'm using idle, any help would be greatly appreciated.我正在使用空闲,任何帮助将不胜感激。 This ones got me stumped.这让我难住了。 The error only runs when accessing the admin section of this interface.该错误仅在访问此界面的管理部分时运行。
based on your code根据您的代码
def login_verify():
global username1
username1 = username_verify.get()
password1 = password_verify.get()
username_login_entry.delete(0, END)
password_login_entry.delete(0, END)
list_of_files = os.listdir()
if username1 in list_of_files:
file1 = open(username1, "r")
print(username1)
verify = file1.read().splitlines()
if username1 == 'admin':
tMain_Screen()
elif password1 in verify:
login_sucess()
else:
password_not_recognised()
else:
user_not_found()
the login_success
function where your variable login_success_screen
is declared is only ran it if not username1 == 'admin'
, even though, you are using the variable login_success_screen
in the function tMain_Screen()
.声明变量login_success_screen
的login_success
function 仅在if not username1 == 'admin'
时运行它,即使您在 function tMain_Screen()
中使用变量login_success_screen
。
def tMain_Screen():
global tmain_Screen
tmain_Screen = Toplevel(login_success_screen)
tmain_Screen.geometry("300x400")
tmain_Screen.title("Admin Interface")
Label("tmain_screen",text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
You need to run login_success()
in order to login_success_screen
be declared.您需要运行login_success()
才能声明login_success_screen
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.