#python-3.x #tkinter
Вопрос:
Предупреждение: Это будет долго (извините)
Я использую Tkinter в python, с PyCharm IDE. Я создаю текстовую rpg, в которой вам нужно создать учетную запись для доступа к настройкам, что все сделано и работает. Однако, когда вы ранее создали учетную запись при входе в систему, если вы затем вернетесь в главное меню и попытаетесь снова войти в систему, сразу же появится сообщение об ошибке:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersVandkbAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1885, in __call__
return self.func(*args)
File "C:UsersVandkbPycharmProjectsVideoGamemain.py", line 8, in combined_func
f(*args, **kwargs)
File "C:UsersVandkbPycharmProjectsVideoGamemain.py", line 161, in Login
Settings()
TypeError: 'Tk' object is not callable
Насколько мне удалось выяснить, нет никаких причин, по которым это не должно сработать. Мой код выглядит следующим образом (он длинный)
from tkinter import *
import json
import os
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
#Title Screen
def TitleScreen():
global TitleScreen
TitleScreen = Tk()
TitleScreen.geometry("980x470")
TitleScreen.resizable(False, False)
TitleScreen.title("Breaking the System - Main Menu")
Title1 = Label(TitleScreen, text="Breaking", font=("Arial", 50, "underline"))
Space1 = Label(TitleScreen, text=" ", font=("Arial", 50))
Title2 = Label(TitleScreen, text="the", font=("Arial", 50, "underline"))
Space2 = Label(TitleScreen, text=" ", font=("Arial", 50))
Title3 = Label(TitleScreen, text="System", font=("Arial", 50, "underline"))
Start = Button(TitleScreen, text="Start", font=("Arial", 20))
Settings = Button(TitleScreen, text="Settings", font=("Arial", 20),
command=combine_funcs(SettingsLogin, TitleScreen.withdraw))
Exit = Button(TitleScreen, text="Exit", font=("Arial", 20), command=Close)
Developer =Label(TitleScreen, text="Developer: Kailan Van der Putten", font=("Arial", 10))
Title1.grid(row=0, column=0)
Space1.grid(row=1, column=1)
Title2.grid(row=2, column=2)
Space2.grid(row=3, column=3)
Title3.grid(row=4, column=4)
Start.grid(row=5, column=0)
Settings.grid(row=5, column=2)
Exit.grid(row=5, column=4)
Developer.grid(row=0, column=4, sticky=NE)
#Account Creation
def AccountCreation():
global AccountCreation
AccountCreation = Tk()
AccountCreation.geometry("980x470")
AccountCreation.resizable(False, False)
AccountCreation.title("Breaking the System - Create an account")
global created_username
global created_password
AccountCreationRequest = Label(AccountCreation, text="Please create an account to access!",
font=("Arial", 20))
Space1 = Label(AccountCreation, text=" ", font=("Arial", 20))
Username = Label(AccountCreation, text="Username:", font=("Arial", 15))
created_username = Entry(AccountCreation, font=("Arial", 20))
Password = Label(AccountCreation, text="Password:", font=("Arial", 15))
created_password = Entry(AccountCreation, font=("Arial", 20))
Create = Button(AccountCreation, text="Create", font=("Arial", 15),
command=combine_funcs(Acceptable, AccountCreation.withdraw))
Back = Button(AccountCreation, text="Back", font=("Arial", 15),
comman=combine_funcs(TitleScreen.deiconify, AccountCreation.withdraw))
Sword1 = Label(AccountCreation, text=" /", font=("Arial", 20))
Sword2 = Label(AccountCreation, text="O===[====================-", font=("Arial", 20))
Sword3 = Label(AccountCreation, text=" " '\', font=("Arial", 20))
AccountCreationRequest.grid(row=0, column=0)
Space1.grid(row=1, column=0)
Username.grid(row=2, column=0, sticky=W)
created_username.grid(row=3, column=0, sticky=W)
Password.grid(row=4, column=0, sticky=W)
created_password.grid(row=5, column=0, sticky=W)
Create.grid(row=6, column=0, sticky=W)
Back.grid(row=6, column=0)
Sword1.grid(row=3, column=1, sticky=SW)
Sword2.grid(row=4, column=1, sticky=W)
Sword3.grid(row=5, column=1, sticky=NW)
#Settings (Login)
def SettingsLogin():
global SettingsLogin
SettingsLogin = Tk()
SettingsLogin.geometry("980x470")
SettingsLogin.resizable(False, False)
SettingsLogin.title("Breaking the System - Login")
global entered_username
global entered_password
Login1 = Label(SettingsLogin, text="Login", font=("Arial", 20))
Username = Label(SettingsLogin, text="Username:", font=("Arial", 15))
entered_username = Entry(SettingsLogin, font=("Arial", 20))
Password = Label(SettingsLogin, text="Password:", font=("Arial", 15))
entered_password = Entry(SettingsLogin, font=("Arial", 20))
LoginButton = Button(SettingsLogin, text="Login", font=("Arial", 15),
command=combine_funcs(Login, SettingsLogin.withdraw))
Back = Button(SettingsLogin, text="Back", font=("Arial", 15),
command=combine_funcs(TitleScreen.deiconify, SettingsLogin.withdraw))
DeleteAccount = Button(SettingsLogin, text="Delete Account", font=("Arial", 15),
command=combine_funcs(Delete))
CreateAccount = Button(SettingsLogin, text="Create Account", font=("Arial", 15),
command=combine_funcs(AccountCreation, SettingsLogin.withdraw))
Space1 = Label(SettingsLogin, text=" ", font=("Arial", 20))
Space2 = Label(SettingsLogin, text=" ", font=("Arial", 20))
HP1 = Label(SettingsLogin, text=" _________", font=("Arial", 20))
HP2 = Label(SettingsLogin, text=" {_________}", font=("Arial", 20))
HP3 = Label(SettingsLogin, text=" )=======(", font=("Arial", 20))
HP4 = Label(SettingsLogin, text=" / \", font=("Arial", 20))
HP5 = Label(SettingsLogin, text="| _________ |", font=("Arial", 20))
HP6 = Label(SettingsLogin, text="|| ||", font=("Arial", 20))
HP7 = Label(SettingsLogin, text="|| ||", font=("Arial", 20))
HP8 = Label(SettingsLogin, text="|| ||", font=("Arial", 20))
HP9 = Label(SettingsLogin, text="|| ||", font=("Arial", 20))
HP10 = Label(SettingsLogin, text="|'----------------'|", font=("Arial", 20))
HP11 = Label(SettingsLogin, text="`-.................-'", font=("Arial", 20))
Space1.grid(row=0, column=0)
HP1.grid(row=0, column=1)
HP2.grid(row=1, column=1)
HP3.grid(row=2, column=1)
HP4.grid(row=3, column=1)
HP5.grid(row=4, column=1)
HP6.grid(row=5, column=1)
HP7.grid(row=6, column=1)
HP8.grid(row=7, column=1)
HP9.grid(row=8, column=1)
HP10.grid(row=9, column=1)
HP11.grid(row=10, column=1)
Space2.grid(row=0, column=2)
Login1.grid(row=1, column=3)
Username.grid(row=2, column=3)
entered_username.grid(row=3, column=3)
Password.grid(row=4, column=3)
entered_password.grid(row=5, column=3)
CreateAccount.grid(row=6, column=3)
DeleteAccount.grid(row=7, column=3)
LoginButton.grid(row=8, column=3)
Back.grid(row=9, column=3)
#Login
def Login():
global username2
global password2
username2 = str(entered_username.get())
password2 = str(entered_password.get())
with open("UserInfo.json", "r") as read_file:
userword = json.load(read_file)
if userword["Username"] == username2 and userword["Password"] == password2:
Settings()
else:
Unmatched()
read_file.close()
#Unmatched
def Unmatched():
global Unmatched
Unmatched = Tk()
Unmatched.geometry("388x160")
Unmatched.resizable(False, False)
Unmatched.title("Breaking the System")
Incorrect1 = Label(Unmatched, text="Username and Password are not incorrect.", font=("Arial", 15))
Space = Label(Unmatched, text=" ", font=("Arial", 15))
Done = Button(Unmatched, text="Done", font=("Arial", 15),
command=combine_funcs(SettingsLogin.deiconify, Unmatched.destroy))
Incorrect1.grid(row=0, column=0)
Space.grid(row=2, column=0)
Done.grid(row=3, column=0)
#Delete
def Delete():
global username2
global password2
username2 = str(entered_username.get())
password2 = str(entered_password.get())
with open("UserInfo.json", "r") as read_file:
userword = json.load(read_file)
if userword["Username"] == username2 and userword["Password"] == password2:
os.remove("UserInfo.json")
TitleScreen.deiconify()
SettingsLogin.withdraw()
else:
Unmatched()
read_file.close()
#Settings
def Settings():
global Settings
Settings = Tk()
Settings.geometry("980x470")
Settings.resizable(False, False)
Settings.title("Breaking the System - Settings")
Violence = Label(Settings, text="Level of Violence", font=("Arial", 20))
Back = Button(Settings, text="Back", font=("Arial", 20), command=combine_funcs(TitleScreen.deiconify, Settings.withdraw))
Violence.grid(row=1, column=0)
Back.grid(row=2, column=0)
#Pause Screen
#Save Screen
#Inventory
#Info about person speaking
#The Game
#User Information
def UserInfo():
List = {"Username": created_username.get(), "Password": created_password.get()}
with open("UserInfo.json", "w") as write_file:
json.dump(List, write_file)
write_file.close()
#Acceptable
def Acceptable():
global username1
global password1
username1 = str(created_username.get())
password1 = str(created_password.get())
if username1 == "" and password1 == "":
Blank()
elif username1.isspace() and password1.isspace():
BothFalseWhite()
elif not username1.isalnum() and not password1.isalnum() and not username1.isspace() and not password1.isspace():
BothFalseSpChar()
elif not username1.isalnum() or username1.isspace() or not password1.isalnum() or password1.isspace() or username1 == "" or password1 == "":
BothFalse()
else:
SettingsLogin.deiconify()
UserInfo()
#Blank
def Blank():
global Blank
Blank = Tk()
Blank.geometry("388x160")
Blank.resizable(False, False)
Blank.title("Breaking the System")
Incorrect1 = Label(Blank, text="Username and Password are not accepted,", font=("Arial", 15))
Incorrect2 = Label(Blank, text="make sure to input letters or numbers.", font=("Arial", 15))
Space = Label(Blank, text=" ", font=("Arial", 15))
Done = Button(Blank, text="Done", font=("Arial", 15),
command=combine_funcs(AccountCreation.deiconify, Blank.destroy))
Incorrect1.grid(row=0, column=0)
Incorrect2.grid(row=1, column=0)
Space.grid(row=2, column=0)
Done.grid(row=3, column=0)
#Both False - Special Characters
def BothFalseSpChar():
global BothFalseSpChar
BothFalseSpChar = Tk()
BothFalseSpChar.geometry("388x160")
BothFalseSpChar.resizable(False, False)
BothFalseSpChar.title("Breaking the System")
Incorrect1 = Label(BothFalseSpChar, text="Username and Password are not accepted,", font=("Arial", 15))
Incorrect2 = Label(BothFalseSpChar, text="make sure to input letters or numbers.", font=("Arial", 15))
Space = Label(BothFalseSpChar, text=" ", font=("Arial", 15))
Done = Button(BothFalseSpChar, text="Done", font=("Arial", 15),
command=combine_funcs(AccountCreation.deiconify, BothFalseSpChar.destroy))
Incorrect1.grid(row=0, column=0)
Incorrect2.grid(row=1, column=0)
Space.grid(row=2, column=0)
Done.grid(row=3, column=0)
#Both False - Only Whitespaces
def BothFalseWhite():
global BothFalseWhite
BothFalseWhite = Tk()
BothFalseWhite.geometry("388x160")
BothFalseWhite.resizable(False, False)
BothFalseWhite.title("Breaking the System")
Incorrect1 = Label(BothFalseWhite, text="Username and Password are not accepted,", font=("Arial", 15))
Incorrect2 = Label(BothFalseWhite, text="make sure to input letters or numbers.", font=("Arial", 15))
Space = Label(BothFalseWhite, text=" ", font=("Arial", 15))
Done = Button(BothFalseWhite, text="Done", font=("Arial", 15),
command=combine_funcs(AccountCreation.deiconify, BothFalseWhite.destroy))
Incorrect1.grid(row=0, column=0)
Incorrect2.grid(row=1, column=0)
Space.grid(row=2, column=0)
Done.grid(row=3, column=0)
#Both False - One or both reasons
def BothFalse():
global BothFalse
BothFalse = Tk()
BothFalse.geometry("388x160")
BothFalse.resizable(False, False)
BothFalse.title("Breaking the System")
Incorrect1 = Label(BothFalse, text="Username and Password are not accepted,", font=("Arial", 15))
Incorrect2 = Label(BothFalse, text="make sure to input letters or numbers.", font=("Arial", 15))
Space = Label(BothFalse, text=" ", font=("Arial", 15))
Done = Button(BothFalse, text="Done", font=("Arial", 15),
command=combine_funcs(AccountCreation.deiconify, BothFalse.destroy))
Incorrect1.grid(row=0, column=0)
Incorrect2.grid(row=1, column=0)
Space.grid(row=2, column=0)
Done.grid(row=3, column=0)
#Exit Function
def Close():
TitleScreen.quit()
#Other Stuff
TitleScreen()
TitleScreen.mainloop()
Еще раз мне очень жаль, что код такой длинный, надеюсь, вы сможете помочь.
Спасибо.
Комментарии:
1. как добавить ошибку в блок цитат?
2. Вы использовали имя переменной
Settings
по крайней мере для трех разных вещей в этом коде. Python не может читать ваши мысли, чтобы определить, на что из этих вещей вы намеревались ссылаться, он просто использует самое последнее задание.3. Я не могу найти ничего, что имеет отношение к чему
Settings
-либо, кроме как внутри функцииSettings():
, и когда в командах для каких-либо кнопок, вы об этом говорите? Проблема в том, что я использовал его слишком много раз для команд в кнопках? или внутри функции?4. После того, как вы присвоили этому имени что-то другое, оно больше не ссылается на функцию, поэтому любая попытка вызвать эту функцию завершится неудачей.
5. У вас есть функция «Настройки» и
Tk
окно «Настройки».