我在 python 使用 tkinter 时出错,需要帮助(学校项目)
I have an error in python with tkinter and need help(school project)
我必须为 python 的学校创建一个小的文字冒险。
为了检测键盘输入,我决定使用 tkinter 并禁用 window。
一切正常,但如果我在按下某个键后尝试使用变量进行计算,则会出现以下错误...This is the error message
这是我正在使用的脚本(我对python没有太多经验...)
import os
import sys
import tkinter
menueeintraege = ["Start", "Steuerung", "Credits", "Beenden"]
index = 0
def menueaufbauen():
os.system("cls")
print("Menue")
print("")
print("")
for i in range(4):
if i == index:
print(menueeintraege[i] + "<")
else:
print(menueeintraege[i])
menueaufbauen()
def startgame():
os.system("game.py");
def steuerung():
os.system("cls")
print("Steuerung")
print("")
print("Norden = Pfeiltaste Hoch")
print("Sueden = Pfeiltaste Runter")
print("Osten = Pfeiltaste Rechts")
print("Westen = Pfeiltaste Links")
print("Bestaetigen = Enter")
def credits():
os.system("cls")
print("Credits")
print("")
print("Jannik Nickel")
print("Thomas Kraus")
print("")
def exitgame():
sys.exit()
def menueauswahl(taste):
print(taste)
if taste == "Up":
if index > 0:
index -= 1
print(index)
elif taste == "Down":
if index < 3:
index += 1
menueaufbau()
def tasteneingabe(event):
tastenname = event.keysym
menueauswahl(tastenname)
fenster = tkinter.Tk()
fenster.bind_all('<Key>', tasteneingabe)
fenster.withdraw()
fenster.mainloop()
我认为错误必须在脚本的最后一部分,我希望这里有人知道解决方案,因为这对学校来说非常重要。
感谢您的帮助
(我使用的是 Visual Studio 2015)
好的,我发现了一些错误。首先是您在函数内部引用 global variable (索引)。为此,您需要告诉 python 您正在使用全局变量。
def menueauswahl(taste):
global index
print(taste)
并且你还需要将第61行的函数名称更改为menuaufbauen()。
我必须为 python 的学校创建一个小的文字冒险。 为了检测键盘输入,我决定使用 tkinter 并禁用 window。 一切正常,但如果我在按下某个键后尝试使用变量进行计算,则会出现以下错误...This is the error message
这是我正在使用的脚本(我对python没有太多经验...)
import os
import sys
import tkinter
menueeintraege = ["Start", "Steuerung", "Credits", "Beenden"]
index = 0
def menueaufbauen():
os.system("cls")
print("Menue")
print("")
print("")
for i in range(4):
if i == index:
print(menueeintraege[i] + "<")
else:
print(menueeintraege[i])
menueaufbauen()
def startgame():
os.system("game.py");
def steuerung():
os.system("cls")
print("Steuerung")
print("")
print("Norden = Pfeiltaste Hoch")
print("Sueden = Pfeiltaste Runter")
print("Osten = Pfeiltaste Rechts")
print("Westen = Pfeiltaste Links")
print("Bestaetigen = Enter")
def credits():
os.system("cls")
print("Credits")
print("")
print("Jannik Nickel")
print("Thomas Kraus")
print("")
def exitgame():
sys.exit()
def menueauswahl(taste):
print(taste)
if taste == "Up":
if index > 0:
index -= 1
print(index)
elif taste == "Down":
if index < 3:
index += 1
menueaufbau()
def tasteneingabe(event):
tastenname = event.keysym
menueauswahl(tastenname)
fenster = tkinter.Tk()
fenster.bind_all('<Key>', tasteneingabe)
fenster.withdraw()
fenster.mainloop()
我认为错误必须在脚本的最后一部分,我希望这里有人知道解决方案,因为这对学校来说非常重要。
感谢您的帮助 (我使用的是 Visual Studio 2015)
好的,我发现了一些错误。首先是您在函数内部引用 global variable (索引)。为此,您需要告诉 python 您正在使用全局变量。
def menueauswahl(taste):
global index
print(taste)
并且你还需要将第61行的函数名称更改为menuaufbauen()。