如何将Tkinter应用程序放入类

2024-10-05 10:40:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在努力研究如何使用类,但是每个人使用它们的方式都不一样,这让我感到困惑和困惑。在

我正在尝试将这两个图像插入到一个类中,但不知道如何执行此操作。 另一件我正在努力解决的问题是,如何将我想要销毁的内容放入一个列表中,然后发送到函数中以删除图像,而不是单独执行这些操作?在

如果有人能帮我解释如何做这些事情,或者告诉我如何去做(我通过例子学到了最好的东西,但是我对没有用我的情况的例子感到困惑。)

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

Tags: from图像imageimportimgdefloadswh
1条回答
网友
1楼 · 发布于 2024-10-05 10:40:31

我想你想把你的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()

相关问题 更多 >

    热门问题