0

I'm currently working on a safe virus that just spams windows with an image on it, but I ran into a problem of not able to have the image on multiple windows

This is my current code:

from tkinter import Tk, mainloop, Label
from random import randint
from PIL import ImageTk, Image

def Spawn(self):
    self.win = Tk()
    self.win.geometry(str(randint(10,1000))+'x'+str(randint(10,1000))+'+'+str(randint(-20,800))+'+'+str(randint(-20,500)))
    self.win.label = Label(self.win, image= ImageTk.PhotoImage(Image.open(r"bean.jpg"))).pack()
    self.win.protocol("WM_DELETE_WINDOW", self.MULTIPLY)
    
class ULTRASUS():
    def __init__(self):
        Spawn(self)
        
    def MULTIPLY(self):
        self.win.destroy()
        Spawn(self)

for I in range (25): ULTRASUS()
    
mainloop()
Haise
  • 1
  • Don't create more than one instance of `Tk`. Instead, after the root window, create instances of `Toplevel`. – Bryan Oakley May 13 '23 at 18:21
  • the class does not appear to be correctly constructed.... did you mean to put `def Spawn(self):` as a function, class or method of the `ULTRASUS` class ? – D.L May 13 '23 at 19:02
  • ...remember that python is very forgiving in the sense that it will let the user write poorly formed code without throwing errors like strongly typed languages ! – D.L May 13 '23 at 19:04
  • 1
    You also need to keep a reference to the PhotoImage object, or it will be garbage-collected. https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function is the general solution, but since you're using only one image, it might as well be a global variable (loaded once, rather than once per window), in which case the problem doesn't arise. – jasonharper May 13 '23 at 19:05
  • I'm relatively news to tkinter so I have no idea what you guys meant (sorry), it would be great if someone could help me with a basic structure for it. I did research but I doesn't understand any – Haise May 14 '23 at 10:32
  • All you need are already given by the above comments. I'll recap. Change the ```Tk()``` to the ```Toplevel()``` and store the return value of the ```ImageTk.PhotoImage()``` as an instance(member) variable. Of course, you should create a single Tk() root window at the beginning. – relent95 May 15 '23 at 09:27
  • YOOOO! Thx you guys a lot! I couldn't do it without help from you guys! :smile: – Haise May 18 '23 at 19:20

0 Answers0