【发布时间】:2013-07-20 04:38:41
【问题描述】:
我在 while 语句中有一个语句,它应该在画布上绘制图像,但它没有发生,我不知道为什么,因为它没有显示任何错误。
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 根据我现在的理解,我必须创建一个类并从那里调用函数才能使其工作? 编辑:我尝试使用全局变量和类时仍然遇到麻烦。
随机数不是问题,因为我在图像应该打印的行之前打印了它,以防它不起作用但它确实起作用。我做错了什么?
【问题讨论】:
-
你进入主循环了吗?除非您这样做,否则不会发生任何事情。所有任务本质上都是排队的,主循环将通过任务队列运行并执行任何操作,处理事件等。
-
我不完全确定这是什么意思,但我猜不是,所以我得去查一下,谢谢你的评论。
-
我的意思是你在某个地方打电话给
canvas.mainloop()? (您提供的代码中没有,但我认为这不是所有代码,因为其中使用了未定义的符号。) -
@GP89 它在第二个代码块的最后一行,但你是对的:代码不完整,因此很难重现问题。画布是在哪里以及如何创建的?
-
必须将事件的坐标与一些神奇的数字进行比较是非常不寻常的。您是否知道您可以创建仅在有人单击特定按钮或画布项时触发的事件,这样您就不必猜测单击了哪个小部件?