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
1
  • Refer my solution below for image issue and also infinite times of opening your tkinter window @PorkBurrito Commented Aug 29, 2019 at 11:23

2 Answers 2

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 !

Sign up to request clarification or add additional context in comments.

Comments

-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

5 Comments

that doesnt make it open the image again tho
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.
@PorkBurrito for images, you refer this - stackoverflow.com/questions/35024118/…
This will simply cause the program to freeze after the first window is destroyed.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.