0

I want to open a new window which includes an image. I don't know how to achieve this. Currently, my code opens up a plain window with a button saying show image.

from tkinter import * 
from PIL import ImageTk, Image




root = Tk()
root.configure(background = "black")
obrazek = ImageTk.PhotoImage(Image.open("dn.jpg"))
def click():
    
    MyLabel = Label(root, image = obrazek)
    Tk()
    MyLabel.pack()


myButton = Button(root, text = "Wyjście", command = click, fg = "white", bg = "#000000")
myButton.pack()


mainloop()
Paramonow
  • 1
  • 1
  • dont use `Tk()` twice, use `Toplevel` instead – Thingamabobs Jan 02 '21 at 11:06
  • also [read this](https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter) – Thingamabobs Jan 02 '21 at 11:07
  • how to show a picture in a new button pop-up ? – Paramonow Jan 02 '21 at 11:27
  • @Paramonow do you want the button to have an image or the window? – JacksonPro Jan 02 '21 at 11:33
  • I want to show the picture after pressing the button, sorry for the inaccuracy – Paramonow Jan 02 '21 at 11:43
  • Say `top = Toplevel()` and make your label to `Label(top,....)` and then make sure to remove `Tk()`. – Delrius Euphoria Jan 02 '21 at 11:49
  • I followed your instructions but after pressing the button the program opens an empty window with no image – Paramonow Jan 02 '21 at 12:25
  • `MyLabel` is packed in the *root* window, not the newly open window. – acw1668 Jan 02 '21 at 13:10
  • I put minimal working example in answer. If you need more then you can search on my [blog](https://blog.furas.pl/category/pythontkinter.html) or in examples in my repo [python-examples](https://github.com/furas/python-examples/). I speak Polish. – furas Jan 02 '21 at 13:16
  • @furas i have a question, is it possible to loop pop-up images, e.g. after clicking a button, 10 images pop up, etc ? – Paramonow Jan 02 '21 at 13:43
  • I dont know if I understand. On blog I have [Tkinter: Update image on Canvas with Button click](https://blog.furas.pl/python-tkinter-update-image-on-canvas-with-button-click-gb.html) (PL: [Tkinter: Zmiana obrazka na Canvas po wciśnięciu Button'a](https://blog.furas.pl/python-tkinter-zmiana-ograzka-na-canvas-po-wcisnieciu-button.html)) and there is `Change many times - and cicle images` which shows how to use list with three images (red,yellow, green) to display different image on every button click. – furas Jan 02 '21 at 13:51
  • if you want to open 10 windows at once when you click 1 button then you can do it. In `click()` you can use loop to create 10 Toplevel windows and display different image from some list. – furas Jan 02 '21 at 13:58
  • where i must place while true: ? – Paramonow Jan 02 '21 at 14:14
  • what `while True`? To create 10 windows you need `for x in range(10)`. But if you thing to repeate some work all time - ie replace image every 30 seconds - then don't use `while True` but `after(30, other_function)`. Using `while True` you will block `mainloop` and code will freeze. – furas Jan 03 '21 at 02:38

1 Answers1

1

This is minimal working example.

You have to use Tk() only to create main window. For other windows you have to use Toplevel. And when you have it in variable ie. top then you have to use it as first argument in Label (or other widget) to display widget in this window.

BTW: PEP 8 -- Style Guide for Python Code

#from tkinter import *  # PEP8: `import *` is not preferred 

import tkinter as tk
from PIL import ImageTk, Image

# --- functions ---

def click():
    top = tk.Toplevel()
    my_label = tk.Label(top, image=my_image)
    my_label.pack()

# --- main ---

root = tk.Tk()
#root.configure(background="black")  # PEP8: without spaces around `=`

my_image = ImageTk.PhotoImage(Image.open("lenna.png"))  # PEP8: english names

my_button = tk.Button(root, text="Wyjście", command=click)  # PEP8: lower_case_names for variables
my_button.pack()

root.mainloop()

enter image description here

Image from Wikipedia: Lenna


EDIT:

I hope you didn't create PhotoImage inside function because there is bug in PhotoImage which removes image from memory when it is assigned to local variable in function. And then you can see empty image. It has to be assigned to global variable or to some object - ie. my_label.img = image

furas
  • 134,197
  • 12
  • 106
  • 148