当前位置:网站首页>关于tkinter.Canvas 不显示图片的问题
关于tkinter.Canvas 不显示图片的问题
2022-06-13 01:30:00 【falwat】
问题描述
本想使用tkinter开发一个"蜘蛛纸牌"游戏, 但是在测试tkinter.Canvas上创建图像时却不显示图像.
测试代码如下:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
class SpiderSolitaire(ttk.Frame):
def __init__(self, master: Tk, **kw):
super().__init__(master, **kw)
self.mainframe = ttk.Frame(master)
self.mainframe.grid(column=0, row=0, sticky=NSEW)
self.canvas = Canvas(self.mainframe, width=400, height=300, background='gray55')
self.canvas.grid(row=0, column=0, sticky=NS)
im = ImageTk.PhotoImage(Image.open('./images/Spades_1.png'))
self.canvas.create_image(30, 10, image=im, anchor='nw')
# 这两条线只是为了标明图片要显示的位置.
self.canvas.create_line(30, 10, 98, 113, fill='red', width=3)
self.canvas.create_line(30, 113, 98, 10, fill='red', width=3)
# set window's title.
self.master.title('Spider Solitaire')
# set window's geometry size
self.master.geometry('400x300')
if __name__ == '__main__':
root = Tk()
SpiderSolitaire(root)
root.mainloop()
运行结果如下, 图片未显出出来.
在stackoverflow上找到了相同的问题描述, 里面给出了一个可行的解决方法. 但没有给出此问题的解释. 在其他文章中有说是内存回收的问题, 这种说法还是相对靠谱的.
解决方法
将导入的图片先赋给一个"持久变量", 比如创建一个self.image用来持有导入的PhotoImage, 这样图片就不会被当垃圾回收掉. 如果有多个图片可以创建一个列表来持有.
修改上述测试代码如下:
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
class SpiderSolitaire(ttk.Frame):
def __init__(self, master: Tk, **kw):
super().__init__(master, **kw)
self.mainframe = ttk.Frame(master)
self.mainframe.grid(column=0, row=0, sticky=NSEW)
self.canvas = Canvas(self.mainframe, width=400, height=300, background='gray55')
self.canvas.grid(row=0, column=0, sticky=NS)
# bug fixed: 修改以下两行即可.
self.image = ImageTk.PhotoImage(Image.open('./images/Spades_1.png'))
self.canvas.create_image(30, 10, image=self.image, anchor='nw')
# 这两条线只是为了标明图片要显示的位置.
self.canvas.create_line(30, 10, 98, 113, fill='red', width=3)
self.canvas.create_line(30, 113, 98, 10, fill='red', width=3)
# set window's title.
self.master.title('Spider Solitaire')
# set window's geometry size
self.master.geometry('400x300')
if __name__ == '__main__':
root = Tk()
SpiderSolitaire(root)
root.mainloop()
运行结果如下, 如片确实被显示出来了!o( ̄▽ ̄)ブ
如果以上内容帮到了你, 烦请点赞收藏, !!在此谢过!!
边栏推荐
- This of phaser3 add. sprite
- Memory learning book reference
- Happy string
- Database query user mailbox
- Use koa to mock data and set cross domain issues
- ES6 deconstruction assignment
- Leetcode-19- delete the penultimate node of the linked list (medium)
- Pytorch's leafnode understanding
- Facial expression recognition dataset
- Several categories of software testing are clear at a glance
猜你喜欢
随机推荐
Jenkins continuous integration operation
Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic
leetcode. 541. reverse string II
在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样
Network communication tcp/ip
V-inline-date, similar to Ctrip, flying pig, time selection with price
pycharm add configutions
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM
My crawler learning notes
Temporary objects and compilation optimization
Study notes on the introduction paper of face recognition deep facial expression recognition: a survey
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
Copy (copy) constructors and assignment overloaded operators=
About inquirerjs
[latex] insert picture
Web Application & applet application deployment
Anims of phaser3
Five classic articles worth reading
Five classic articles worth reading (2)
spiral matrix visit Search a 2D Matrix







![[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx](/img/e9/4e08e07c2de2f99c2938e79f7f1c44.png)

