当前位置:网站首页>About tkinter Canvas does not display pictures
About tkinter Canvas does not display pictures
2022-06-13 01:35:00 【falwat】
Problem description
Originally intended to use tkinter To develop a " Spider card " game , But testing tkinter.Canvas Create an image on without displaying the image .
The test code is as follows :
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')
# These two lines are only used to indicate where the picture is to be displayed .
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()
The operation results are as follows , The picture doesn't show up .
stay stackoverflow Found the same problem description on , There is a feasible solution . But there is no explanation for this problem . In other articles, there is a problem of memory recycling , This statement is relatively reliable .
resolvent
Assign the imported image to a " Persistent variables ", For example, create a self.image Used to hold imported PhotoImage, In this way, the pictures will not be recycled as garbage . If there are multiple pictures, you can create a list to hold .
Modify the above test code as follows :
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: Modify the following two lines .
self.image = ImageTk.PhotoImage(Image.open('./images/Spades_1.png'))
self.canvas.create_image(30, 10, image=self.image, anchor='nw')
# These two lines are only used to indicate where the picture is to be displayed .
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()
The operation results are as follows , If the film is indeed shown !o( ̄▽ ̄)ブ
If the above helps you , Please like the collection , !! In this thanked !!
边栏推荐
- [Stanford Jiwang cs144 project] lab1: streamreassembler
- Database query user mailbox
- leetcode. 541. reverse string II
- Memory learning book reference
- Network communication tcp/ip
- ES6 deconstruction assignment
- Rasa dialogue robot helpdesk (III)
- Tweets movement description and chart display
- Phaser3 load
- [WSL2]限制WSL2可访问的硬件资源(CPU/内存)
猜你喜欢

MySQL related summary

Page optimization - Notes

How to print infinite symbol in WPS

【斯坦福计网CS144项目】Lab1: StreamReassembler

项目实训(十七)---个人工作总结
![[leetcode] valid phone number Bash](/img/f8/cecb74f9d8f7c589e62e3a9a874f82.jpg)
[leetcode] valid phone number Bash

受众群体应该选择观察模式还是定位模式?

Ecological convergence NFT attacks, metaverse ape leads the new paradigm revolution of Web 3.0 meta universe

Leetcode question brushing 04 string

Realization of flip animation
随机推荐
About the proposed signature file migration to industry standard format pkcs12
Introduction to convolutional neural network
Set and array conversion, list, array
[从零开始学习FPGA编程-21]:进阶篇 - 架构 - VerilogHDL编码规范
Mysql database listening -canal
[learn FPGA programming from scratch -22]: Advanced chapter - Architecture - Design and modeling of FPGA internal hardware circuit
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
Docker install MySQL
H5 open the app. If the app is not downloaded, jump to the download page. If the app has been downloaded, wake up the app
Create a simple game interface using pyGame
[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析
项目实训(十七)---个人工作总结
Facial expression recognition dataset
Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic
FLIP动画实现思路
spiral matrix visit Search a 2D Matrix
ES6解构赋值
[soft test] software designer knowledge points sorting (to be updated)
leetcode 206. Reverse linked list
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM