1

I have been trying to work out how to use classes but everyone seems to use them so differently and it has left me confused and boggled.

I'm trying to insert the two images into a class but have no idea how to do so. Another thing I'm struggling with is how to put whatever I want destroying into a list to be sent to the function to remove the images instead of doing it individually?

If anyone can help explain to me how to do these things or show me how to do them (i learn best by example but got confused with examples not using my situation.)

import sys
from tkinter import *
from PIL import Image, ImageTk

SWH = Tk()
SWH.geometry("1024x950+130+0")
SWH.title("ServiceWhiz.")
#_#GlobalFunction#_
#ClearAllWidgets
def removewidgets(A, B):
    A.destroy()
    B.destroy()
    return;
#_#LoadingPage#_

class SWLoad:
    def __init__(self, master):


load = Image.open("Logo.png")
render = ImageTk.PhotoImage(load)
img = Label(SWH,image=render)
img.image = render  
img.place(x=458,y=250)

load = Image.open("PoweredByServiceWhiz.png")
render = ImageTk.PhotoImage(load)
img1 = Label(SWH,image=render)
img1.image = render  
img1.place(x=362,y=612.5)

img.after(3000, lambda: removewidgets(img, img1) )
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
B.Hawkins
  • 353
  • 5
  • 13
  • While not a precise duplicate, you might find the answer you're looking for here: http://stackoverflow.com/questions/17466561/python-tkinter-program-structure/17470842#17470842 – Bryan Oakley Sep 04 '14 at 16:16

1 Answers1

2

I assume you want to put your Tkinter app in a class? And the app has to display two images and later remove both?

If that's correct, this should work for you. I've explained what does what in the comments.

import sys
from tkinter import *
from PIL import Image, ImageTk

# Create the Class. Everything goes in the class,
# and passing variables is very easy because self is passed around
class SWLoad():
    # __init__ runs when the class is called
    def __init__(self):
        # Create window
        SWH = Tk()
        SWH.geometry("1024x950+130+0")
        SWH.title("ServiceWhiz.")

        # Initialize a list for the images
        self.img_list = []

        # Load the first image
        load = Image.open("Logo.png")
        render = ImageTk.PhotoImage(load)
        # Add the label to the list of images.
        # To access this label you can use self.img_list[0]
        self.img_list.append(Label(SWH, image=render))
        self.img_list[0].image = render  
        self.img_list[0].place(x=458,y=250)

        # Load the second image
        load = Image.open("PoweredByServiceWhiz.png")
        render = ImageTk.PhotoImage(load)
        # Add the label to the list of images.
        # To access this label you can use self.img_list[1]
        self.img_list.append(Label(SWH, image=render))
        self.img_list[1].image = render  
        self.img_list[1].place(x=362,y=612.5)

        # Fire the command that removes the images after 3 seconds
        SWH.after(3000, self.removewidgets)
        # Initialize the main loop
        SWH.mainloop()

    # Here, the function that removes the images is made
    # Note that it only needs self, which is passed automatically
    def removewidgets(self):
        # For every widget in the self.img_list, destroy the widget
        for widget in self.img_list:
            widget.destroy()

# Run the app Class
SWLoad()
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • Works a dream :D Thankyou so much :). While i was waiting for some help i did manage to get everything working within a class but that was only from ripping things from tutorials so i didnt really understand all of it and i never addressed the list issue i was having, yours does :) Your comments are super useful and informative. Thanks again :) – B.Hawkins Sep 04 '14 at 14:33
  • Hi again, just wondering, how would i go about communicating from within the class to outside the class. For example, in my original code i used lambda to go to the function outside. In your improved code you move the function inside and use self. If, in the future, i had more classes and moved the function outside all the classes how would i add the images to the list when the list and function is outside of the class? Thanks :) – B.Hawkins Sep 04 '14 at 15:03
  • If, in the future, you have more classes and moved the function outside all the classes you should probably try for yourself first and ask a new question if you can't figure it out. – fhdrsdg Sep 16 '14 at 11:16