我有一个while语句里面的声明应该在画布上画一个图像,但它不会发生,我不知道为什么,因为它不显示任何错误。Python - 图像没有画在画布上
def buttonclick_gamescreen(event):
global scorecounter
global pressed
global randomimage
global images
pressed = ""
if event.x >853 and event.x <957 and event.y > 8 and event.y < 56 : pressed = 7
if event.x >666 and event.x <947 and event.y > 491 and event.y < 534 : pressed = 8
while pressed == 8 :
entryx = e1.get()
entryy = e2.get()
answerx = answerlistx[randomimage]
answery = answerlisty[randomimage]
print("The answer to X is", answerx, "You entered", entryx,"The answer to Y is", answery, "You entered ", entryy)
if entryx == answerx and entryy == answery:
print("correct")
canvas.delete(images)
randomimage = random.randrange(0,49+1)
scorecounter = scorecounter + 1
print("You score is now", scorecounter, "New random number is", randomimage)
game = PhotoImage(file=imagelist[randomimage])
images = canvas.create_image(30, 65, image = game, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
else:
print("incorrect")
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
线,因为它在其他情况下,工作对我来说images = canvas.create_image(30, 65, image = game, anchor = NW)
应该工作。
下面是代码的其余部分的链接,因为我不想让这个问题太长而且杂乱,它显示了画布的绘制位置。 http://pastebin.com/RxmPDUAD 从我现在的理解,我将不得不创建一个类,并从那里调用函数来工作? 编辑:仍然有麻烦,因为我试过使用全局变量和类没有运气。
这是不是一个随机数的问题,因为我打印它的图像应该打印的行之前,只是因为它没有工作,但它确实如此。我究竟做错了什么?
你进入主循环?除非你这样做,否则任何事情都不会发生。所有任务基本上排队,主循环将运行任务队列并执行任何操作,处理事件等。 – GP89
我不完全确定这意味着什么,但我会猜测它不是,所以我会不得不去看看,谢谢你的评论。 – ThatsNotMyName
我的意思是你在哪里调用'canvas.mainloop()'? (您没有提供您提供的代码,但我认为这不是所有的代码,因为那里有未定义的符号) – GP89