如何将Tkinter应用程序放入类中

时间:2014-09-04 10:53:48

标签: python class tkinter pillow

我一直在尝试研究如何使用类,但每个人似乎都使用它们的方式不同而且让我感到困惑和困惑。

我正在尝试将两个图像插入一个类但不知道如何这样做。 我正在努力的另一件事是如何把我想破坏的东西放到一个列表中去发送给函数去除图像而不是单独进行它?

如果有人可以帮我解释如何做这些事情或告诉我如何做这些事情(我通过例子学习得最好,但对不使用我的情况的例子感到困惑。)

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) )

1 个答案:

答案 0 :(得分:1)

我假设你想把你的Tkinter应用程序放在课堂上?该应用程序必须显示两个图像,然后删除它们?

如果这是正确的,这应该适合你。我已经解释了评论中的内容。

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()