2017-03-01 181 views
0

我被困在一個基於上一頁單擊的按鈕在tk頁面上動態顯示特定圖像。 PageOne有5個圖像,每個圖像下面有5個按鈕。如果單擊第三個按鈕,單擊特定按鈕應將用戶帶到第二頁並顯示圖像3。Python:如何在標籤窗口小部件(tkinter)中動態顯示圖像

我已經想出瞭如何傳遞1到5的值,取決於哪個按鈕被點擊,並且圖像被保存了pic1.gif,... pic5.gif所以要返回正確的圖像我只需要追加該文件位置的值。

我正在努力弄清楚如何刷新PageTwo訪問時。

任何幫助將不勝感激,謝謝。

TITLE_FONT = ("Helvetica", 18, "bold") 
class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 
     self.attributes("-fullscreen", False) 
     self.geometry('{}x{}'.format(1000, 1000)) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 


     self.frames = {} 
     for F in (PageOne, PageTwo): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(PageOne) 

    def show_frame(self, c): 
     frame = self.frames[c] 
     frame.tkraise() 

class PageOne(tk.Frame): 
    praiseid = 0 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 

     def PraiseClick(button_id): 

      PageOne.praiseid = button_id 
      controller.show_frame(PageTwo) 

     users= [1,2,3,4,5] 

     for i in users: 
      location = str('C:/Users/XXXXXXX/Documents/pic'+str(i)+'.gif') 
      icon = tk.PhotoImage(file=location) 
      IDlabel = tk.Label(self,image=icon) 
      IDlabel.image = icon 
      IDlabel.place(x=i*100,y=200,width=100,height=100) 

     for j in users: 
      praisebutton = tk.Button(self,text="Click",width=10,command=lambda x=j: PraiseClick(int(x))) 
      praisebutton.place(x=j*100,y=300,width=100,height=44) 

     backbutton = tk.Button(self, text="Go to Start Page", 
         command=lambda: controller.show_frame(StartPage)) 
     backbutton.place(x=100,y=50,width=200,height=44) 

class PageTwo(tk.Frame): 

    def get_id(self): 
     return(PageOne.praiseid) 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 


     self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif' 
     icon = tk.PhotoImage(file=self.location) 
     self.IDlabel = tk.Label(self,image=icon) 
     self.IDlabel.image = icon 
     self.IDlabel.place(x=0,y=200,width=100,height=100) 

回答

0

PageOne所選擇的圖像,因爲繪圖函數是在__init__()功能定位和任何情況下,是提高當PageTwo是通過調用函數tkraise()重繪未在PageTwo繪製。

問題1 - 在撥打的tkraise()時生成一個事件。

Here, the OOP of Python will be the answer by overriding the function tkraise() in the class PageTwo .

class PageTwo(tk.Frame): 
... 
    def tkraise(self): 
     print('PageTwo.tkraise()') 
     tk.Frame.tkraise(self) 
     # then call the drawing icon 
     self.refresh_icon() # see Problem 2 

問題2 - 定位所述圖標的圖中的class PageTwo的函數。

To take into account of the new selected icon, create a function refresh_icon() in the class PageTwo and call it from both __init__() and tkraise() functions.

class PageTwo(tk.Frame): 
... 
    def refresh_icon(self): 
     self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif' 
     icon = tk.PhotoImage(file=self.location) 
     self.IDlabel = tk.Label(self,image=icon) 
     self.IDlabel.image = icon 
     self.IDlabel.place(x=0,y=200,width=100,height=100) 

添加在__init__()函數的末尾。

class PageTwo(tk.Frame): 
... 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     ... 
     self.refresh_icon() 

加成1 - 防止了異常缺失圖像的情況下,前添加復。

Create a file_exists() function, then check before loading in PhotoImage() .

高清file_exists(文件路徑): 嘗試: fp_file =打開(文件路徑) 回報(真) 除了IO錯誤: 回報(假)

而且在功能class PageTworefresh_icon()

self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif' 
    if (file_exists(self.location)): 
     icon = tk.PhotoImage(file=self.location) 
     ... 

獎金2 - 清除當前IDlabel如果圖像未加載。

when creating a new Label in the PageTwo , the variable self.IDlabel will store the new Image without deleting the old one. Before creating a new one, call the destroy() function.

添加變量self.IDlabel的聲明,並在__init__()功能分配給None。然後撥打destroy()中的

class PageTwo(tk.Frame): 
... 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     ... 
     self.refresh_icon() 
     self.IDlabel = None 
... 
    def refresh_icon(self): 
     self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif' 
     if (self.IDlabel): # check label and destroy it 
      self.IDlabel.destroy() 
     if (file_exists(self.location)): 
      ... 
相關問題