带有图像的简单 GUI [英] Simple GUI with images

查看:31
本文介绍了带有图像的简单 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的简单 GUI 上显示一些卡片,但它没有显示出来.

I'm trying to display some cards on my simple GUI, but it's not showing up.

我已经上传了一个带有我想要上传的 .gif 的工作文件,代码很接近但不正确.http://www.filedropper.com/cardgameproblem 大小:0Kb,类型:.zip

I've uploaded a working file with the .gif I want to upload, and the code is close but not right. http://www.filedropper.com/cardgameproblem Size: 0Kb, Type:.zip

  • 包含 1 个 .py 和 1 个 .gif

要查看的行从 257-266 开始.我知道这只是一个小错误,但我看不到.

The lines to look at start at 257-266. I know it's just a minor error, but I can't see it.

请帮忙

推荐答案

经过一番修修补补,我发现图像存储在 GUI 的变量中时可以正确显示,即使用 self

After some tinkering, I found out that the image is properly displayed when stored in a variable of the GUI, i.e. using self

self.gif1 = PhotoImage(file='1.gif')
self.canvas.create_image(0, 0, image=self.gif1, anchor=NW)

我不知道为什么,但是这是有效的,同时只命名变量gif1(或任何其他名称),没有self,才不是.在您的代码和最小示例中进行了测试.

I have no idea why, but This works, while naming the variable just gif1 (or any other name), without self, does not. Tested both in your code and in a minimum example.

更新: 正如@Bryan 所指出的,垃圾收集器在 __init__ 完成时处理 PhotoImage 实例.您必须在构造函数的范围之外保留对实例的引用,例如使用 selfglobal.鉴于手头的问题,最好创建一个 dict,使用卡片的名称作为密钥存储图像.

Update: As @Bryan pointed out, the garbage collector disposes the PhotoImage instance when __init__ finishes. You have to keep a reference to the instance beyond the scope of the constructor, e.g. using self, or global. Given the problem at hand, it might be best to create a dict, storing the images using the names of the cards as keys.

这篇关于带有图像的简单 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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