Python - 图像不是在画布上绘制的 [英] Python - Image not being drawn on canvas

查看:165
本文介绍了Python - 图像不是在画布上绘制的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在while语句中有一个声明,它应该在画布上绘制图像,但它不会发生,我不知道为什么它没有显示任何错误。

I have a statement inside a while statement that is supposed to draw an image onto the canvas but it doesn't happen and I don't know why as it doesn't show any errors.

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)应该适用于我在另一个案例中工作。

The line images = canvas.create_image(30, 65, image = game, anchor = NW) should work as it worked in another case for me.

这是一个指向其余代码的链接,因为我不想让这个问题太长而且混乱,这表明画布的绘制位置。 http://pastebin.com/RxmPDUAD
根据我的理解,我现在必须创建一个上课并从那里调用函数为此工作?
编辑:因为我尝试使用全局变量和类而没有运气,但仍然遇到麻烦。

Here's a link to the rest of the code since I don't want to make this question too long and messy, which shows where the canvas is drawn. http://pastebin.com/RxmPDUAD From my understanding right now I'll have to create a class and call the functions from there for this to work? Having trouble still as I've tried using global variables and classes with no luck.

随着我的打印,这不是随机数的问题就在该行之前,该图像应该只是因为它不起作用而打印出来但是确实如此。我做错了什么?

It's not a problem with the random number as I printed it just before the line the image is supposed to print from just incase it didn't work but it does. What am I doing wrong?

推荐答案

如果没有实际测试过代码,我很确定问题是你的<$退出方法时,c $ c> PhotoImage 对象被垃圾收集。出于某种奇怪的原因,只需将它们传递给 canvas.create_image 就不会阻止这种情况。尝试让它们全球

Without having actually tested your code, I'm pretty sure the problem is that your PhotoImage objects are being garbage-collected when you exit the method. For some weird reason, just passing them to canvas.create_image won't prevent this. Try to make them global:

global game
game = PhotoImage(file=imagelist[randomimage])
images = canvas.create_image(30, 65, image = game, anchor = NW)

另见,< a href =https://stackoverflow.com/questions/15433771/python-tkinter-why-is-this-jpeg-not-showing/15435134#15435134>这个和相关问题/答案。

Also see this, this and this related questions/answers.

更多指示:


  • 条件如 event.x> 853和event.x< 957 可以写成 853< event.x< 957

  • 您可以将 imagelist 定义为 [%d.gif %(i + 1)for i in range(50)]

  • 方法后的一个以毫秒为单位的时间,所以我想这应该是之后(1000,...)

  • 在它的当前状态,按下时== 8:循环似乎没有多大意义,因为按下设置为

  • 最后,我建议定义一个自定义的类GameFrame(Frame)并将所有这些内容放在该课程中,而不是将所有内容全局;在这种情况下,您可以使用 self 关键字将 PhotoImage 绑定到 Frame 以防止垃圾收集

  • conditions like event.x >853 and event.x <957 can be written as 853 < event.x < 957
  • you can define your imagelist as ["%d.gif" % (i+1) for i in range(50)]
  • the after method takes a time in milliseconds, so I guess this should be after(1000, ...)
  • in it's current state, the while pressed == 8: loop seems not to make much sense, since pressed is set to '' after one iteration anyway
  • finally, I'd recommend defining a custom class GameFrame(Frame) and putting all this stuff in that class, instead of making everything global; in this case, you can use the self keyword to bind your PhotoImage to the Frame to prevent it from being garbage-collected

这篇关于Python - 图像不是在画布上绘制的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆