2014-09-04 17 views
0

我一直在努力研究如何使用類,但每個人似乎都如此不同地使用它們,它讓我感到困惑和難以理解。如何將Tkinter應用程序放入類中

我試圖將兩個圖像插入一個類,但不知道如何去做。 我正在努力的另一件事是如何把我想要摧毀的東西放入一個列表中發送到函數來刪除圖像,而不是單獨做?

如果有人可以幫助解釋我如何做這些東西,或告訴我怎麼做這些(我最好的學習通過實例但與實例沒有使用我的情況糊塗了。)

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

雖然沒有一個確切的重複,你可能會發現你這裏尋找答案:http://stackoverflow.com/questions/17466561/python-tkinter-program-structure/17470842#17470842 – 2014-09-04 16:16:50

回答

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

作品夢想:D謝謝這麼多:)。雖然我在等待一些幫助,但我確實設法讓一切在課堂上都能夠正常工作,但那只是從教程中撕掉了所有東西,所以我並沒有真正理解它的全部內容,而且我從未解決過我遇到的列表問題,你的確會:)你的評論是超級有用和信息。再次感謝:) – 2014-09-04 14:33:36

+0

嗨,再次,只是想知道,我將如何去從班級內外溝通。例如,在我的原始代碼中,我使用lambda轉到外面的函數。在您改進的代碼中,您將內部函數移動並使用self。如果將來我有更多的類並將函數移到所有類之外,當列表和函數不在類中時,如何將圖像添加到列表中?謝謝:) – 2014-09-04 15:03:34

+0

如果將來你有更多的課程,並且把這個功能移到了所有課程之外,那麼你應該先爲自己嘗試一下,如果你弄不明白,就問一個新問題。 – fhdrsdg 2014-09-16 11:16:02

相關問題