I am making a program that displays a calendar picture in tkinter.
import tkinter as tk from tkinter import PhotoImage w0 = tk.Tk () img0 = tk.PhotoImage (file ='calendar.png', master = w0) img0 = img0.subsample (5) w1 = tk.Toplevel () def button_push (): img1 = tk.PhotoImage (file ='calendar.png', master = w1) img1 = img1.subsample (5) b1 = tk.Button (w1, image = img1) b1.place (x = 5, y = 5) b0 = tk.Button (w0, image = img0, command = button_push) b0.place (x = 5, y = 5)
So, even if I do it, the picture is not displayed when the button is pressed. .. ..
I don't get any error message. .. ..
(Only the frame is displayed)
Answer # 1
Cause: The variable containing the PhotoImage object
Because it is a local variable in the button_push function
When the function finishes executing, the PhotoImage image is discarded.
Details: Tkinter's non-Widget objects (Variable and PhotoImage)
Since the lifetime is different from widgets etc., you need to manage it independently.
For a normal Python object, use tk.Button (w1, image = img1)
I'm hoping that the Button will have a reference to img1
Inside the Button, you're just passing the ID string (usually the filename) based on img1
The widget does not hold the object itself passed to the image.
Make it a global variable.
In the case of this code, if you want to display the same image, you don't need to generate a new img1.
image = img0But it's okay.
Make PhotoImage an instance variable using a class.
Save to the attribute of a long-lived object (usually a global variable) somewhere
Since we are using Toplevel, if you want to create a dialog,
It's a good idea to create a class of dialogs and have a PhotoImage object in its instance variable.
- python - image is not displayed in tkinter module
- image cannot be displayed with python tkinter
- how to save the image ocr result file in python
- python 3x - unintended behavior of python3 tkinter binding
- python 3x - change font size for python3 tkinter entry
- python image display error beginner, thank you
- python - image recognition with keras
- python - reopening the file dialog in tkinter results in an error
- python - image is not saved even though there is no opencv error
- python - about image deletion in django
- [python] [tkinter] i want to change the acquisition contents of the second and subsequent menu lists by selecting the previous p
- python - [tkinter] [py installer] unable to execute exe file
- i have a question about runtime errors and exception handling in python
- python - "atcoder abc 015d takahashi-kun's anguish" question about how to make a dp table for dynamic programming
- python3 tkinter i want to be able to input only character strings in a fixed format
- python - image recognition using cnn keras multiple inputs
- python - i want to read an image and display it
- python 3x - python3 tkinter toplevel behaves differently than intended
- python - i want to divide the read image into pixels and label them
- Python Tkinter not seeing 'TimePL' instance of Label widget
- python : How can a scrollbar be attached to a label in tkinter
- python : Why root.attributes('-topmost', False) fires every other time?
- python : How to write functions for buttons to work?
- Working buttons in TKinter in Python in OOP
- python : How to create a large text input field?
- python : How to keep the scale of buttons, text, etc in tkinter?
- python : how to bind keyboard input in Tkinter
- python : how to find out the size of a Tkinter window?
- python : How to make txt to html text converter