Why is this not working, no errors? photo exists in correct position... gui doesnt alwyas show up, image doesnt. Using python 3

from tkinter import *
from random import *
import time

mainGUI = Tk()

width2= 319
height2 = 240
widthOfScreen = mainGUI.winfo_screenwidth() #Get the width of the screen
heightOfScreen = mainGUI.winfo_screenheight() #Get the height of the screen

mainGUI.geometry("%dx%d+0+0" % (widthOfScreen+width2, heightOfScreen+height2)) #Set the size of the window to full screen
mainGUI.configure(background = "white")
mainGUI.title("spam Example")

address = r"S:\picture.gif" #Set address of image
print(widthOfScreen, heightOfScreen, address, mainGUI.title)

count = 0
for count in range(0,10):
    XValue = randint(0,widthOfScreen-width2)
    YValue = randint(0,heightOfScreen-height2)
    photo1 = PhotoImage(file=address) #Assign image to PhotoImage
    print(widthOfScreen, heightOfScreen, address, mainGUI.title, count, photo1, "\n", XValue, YValue)
    imageTopRightCorner = Label(mainGUI,image = photo1, borderwidth = 0).place(x=XValue, y = YValue)

mainGUI.mainloop() #start the event loop

Recommended Answers

I believe you will need to use Canvas to do this, I have not used any other way in Tkinter.


Jump to Post

All 2 Replies

With time.sleep() you need to tell Tkinter to update the root/label ...

display a GIF image on a Tkinter label at random positions

import time
import random as rn
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

mainGUI = tk.Tk()

width2= 319
height2 = 240
#Get the width of the screen
widthOfScreen = mainGUI.winfo_screenwidth()
#Get the height of the screen
heightOfScreen = mainGUI.winfo_screenheight()
#Set the size of the window to full screen
mainGUI.geometry("%dx%d+0+0" % (widthOfScreen+width2, heightOfScreen+height2))

mainGUI.configure(background = "white")
mainGUI.title("spam Example")

#Set address of image
address = "Slide_Python.gif"  # my test image
#address = "S:/picture.gif"
#Assign image to PhotoImage
photo1 = tk.PhotoImage(file=address)

for count in range(0,10):
    XValue = rn.randint(0,widthOfScreen-width2)
    YValue = rn.randint(0,heightOfScreen-height2)
    tk.Label(mainGUI,image=photo1, borderwidth=0).place(x=XValue, y=YValue)
    mainGUI.update()  # important, use with time.sleep()

#start the event loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.