当前位置:网站首页>关于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( ̄▽ ̄)ブ
如果以上内容帮到了你, 烦请点赞收藏, !!在此谢过!!
边栏推荐
- Simple operation of MySQL database
- Create a simple game interface using pyGame
- MySQL performance optimization
- HashSet underlying source code
- Tweets movement description and chart display
- np. Understanding of axis in concatenate
- Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic
- Detailed explanation of Joseph problem
- Realization of flip animation
- Leetcode question brushing 03 stack
猜你喜欢

Sliding window summary of TCP connections

MySQL related summary

Introduction to common activation functions

Tangent and tangent plane

leetcode 142. Circular linked list II

Simple operation of MySQL database
![[leetcode] valid phone number Bash](/img/f8/cecb74f9d8f7c589e62e3a9a874f82.jpg)
[leetcode] valid phone number Bash

V-inline-date, similar to Ctrip, flying pig, time selection with price

Method of cleaning C disk

DFS and BFS notes (II): depth first search (implemented in C language)
随机推荐
Leetcode-78- subset (medium)
Getting started with phaser 3
项目实训(十七)---个人工作总结
The interviewer only asked me five questions and the interview was over
FLIP动画实现思路
[learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
Leetcode question brushing 04 string
Golang learning essay
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
Deadlock problem summary
Reinstall opencv and step on the pit.
MySQL connection query
Introduction to convolutional neural network
ng-tv-focusable
Docker install MySQL
Leetcode-11- container with the most water (medium)
5G工业网关在煤矿行业的应用优势
Go JWT learning summary
MySQL performance optimization
在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样