[英]Confusing Python Error
I am making a basic device using Python and I ran into this issue: 我正在使用Python制造基本设备,但遇到了这个问题:
File "/home/pi/Adafruit_Python_SSD1306/PyOS_V0.py", line 178, in <module>
gameMenuApp()
File "/home/pi/Adafruit_Python_SSD1306/PyOS_V0.py", line 92, in gameMenuApp
draw.text((0, 32), gameMenuText, font=StartupFinishFont,
UnboundLocalError: local variable 'gameMenuText' referenced before assignment
This doesn't make sense because I assigned the variable at the beginning the file. 这没有意义,因为我在文件的开头分配了变量。 I also put the code into a python linter online but I did not receive an error.
我也将代码在线放入了python linter中,但没有收到错误。 Also, I removed most of the code to try to make it easier to read so if something doesn't make sense, don't worry about it.
另外,我删除了大部分代码,以使其更易于阅读,因此,如果没有任何意义,请不要担心。 I posted the code on Github as well.
我也在Github上发布了代码。 https://github.com/raspiuser6969/PyOS
https://github.com/raspiuser6969/PyOS
gameMenuText = " "
gameMenuSelect = int(1)
gameMenu = [' Pong ',' RPS ']
gameMenuSelect = int(1)
def pong():
def rps():
def gameMenuApp():
while GPIO.input(A_pin):
draw.rectangle((0,0,128,64), outline=0, fill=0)
draw.text((0,32), gameMenuText, font=StartupFinishFont, fill=255)
disp.image(image)
disp.display()
if not(GPIO.input(D_pin)):
gameMenuSelect = gameMenuSelect + 1
draw.rectangle((0,0,128,64), outline=0, fill=0)
disp.image(image)
disp.display()
print(gameMenuSelect)
if not(GPIO.input(U_pin)):
gameMenuSelect = gameMenuSelect - 1
draw.rectangle((0,0,128,64), outline=0, fill=0)
disp.image(image)
disp.display()
print(gameMenuSelect)
if gameMenuSelect == 1:
gameMenuText = gameMenu[gameMenuSelect - 1]
shutdownApp()
time.sleep(0.1)
if gameMenuSelect == 2:
gameMenuText = gameMenu[gameMenuSelect - 1]
rps()
time.sleep(0.1)
if 2 < gameMenuSelect:
gameMenuSelect = gameMenuSelect - 1
if 1 > gameMenuSelect:
gameMenuSelect = gameMenuSelect + 1
gameMenuText
is defined in your file, but not in your function. gameMenuText
是在文件中定义的,但未在函数中定义。 You should either pass it as argument: 您应该将其作为参数传递:
def gameMenuApp(gameMenuText, gameMenuSelect, gameMenu, gameMenuSelect):
or use it as global: 或将其用作全局变量:
def gameMenuApp():
global gameMenuText, gameMenuSelect, gameMenu, gameMenuSelect
Same for your other globals, that's why I already added them. 您的其他全局变量也是如此,这就是为什么我已经添加了它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.