2

I am trying to make it so that when you close the tinker GUI another one opens and this process repeats

I have tried this while command but it says invalid syntax.

from tkinter import *

root = Tk()

photo = PhotoImage(file="scary.png")
label = Label(root, image=photo)
label.pack()

root.mainloop()

while 2 > 1
  • Refer my solution below for image issue and also infinite times of opening your tkinter window @PorkBurrito – amrs-tech Aug 29 '19 at 11:23

2 Answers2

1

You can try this for infinite times opening the tkinter window:

from tkinter import *
from PIL import Image, ImageTk

image = Image.open("scary.png")
photo = ImageTk.PhotoImage(image)

while True:
    root = Tk()
    label = Label(root, image=photo)
    label.pack()

    root.mainloop()

EDIT I've added code for opening image of any format in Tkinter, for that you have to install the required package with pip install pillow (PIL package)

I've tested it without image, it is working. Hope it helps !

amrs-tech
  • 485
  • 2
  • 14
-1

You were missing the loop body. Now it has but since 2 > 1 is always true it's an infinite loop.

from tkinter import *

root = Tk()

photo = PhotoImage(file="scary.png")
label = Label(root, image=photo)
label.pack()

root.mainloop()

while 2 > 1:
  pass
matcheek
  • 4,887
  • 9
  • 42
  • 73
  • that doesnt make it open the image again tho – PorkBurrito Aug 29 '19 at 11:14
  • You have invalid syntax in your snippet and this answer addresses it. Opening an image is another issue that you can raise in another question. – matcheek Aug 29 '19 at 11:17
  • @PorkBurrito for images, you refer this - https://stackoverflow.com/questions/35024118/how-to-load-an-image-into-a-python-3-4-tkinter-window – amrs-tech Aug 29 '19 at 11:19
  • This will simply cause the program to freeze after the first window is destroyed. – Bryan Oakley Aug 29 '19 at 13:51
  • @BryanOakley What else would you expect for a 2 > 1 condition in this while loop? Plus did explicitly say that in the answer that it only addresses the syntax error which is exactly what OP asked for. – matcheek Aug 29 '19 at 14:25