2017-03-01 63 views
0

前のページでクリックしたボタンに基づいて特定の画像を動的に表示しようとしました。 PageOneには5つの画像があり、下に5つのボタンがあります。特定のボタンをクリックすると、2番目のページに移動し、3番目のボタンをクリックすると画像3が表示されます。Python:ラベルウィジェット(tkinter)に画像を動的に表示する方法

どのボタンがクリックされたかに応じて1から5の値を渡す方法を知り、画像を保存しました。pic1.gif、... pic5.gif正しい画像を返すために、値をファイルの場所に設定します。

PageTwoがアクセスされたときにページを更新する方法を理解するのが苦労しています。

ご協力いただきありがとうございます。描画機能が__init__()機能に局在し、何のイベントがPageTwoが機能tkraise()を呼び出して再描画されたときに上げているされていないため

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から選択した画像がPageTwo上に描画されていません。

問題1 - PageTwotkraise()を呼び出すときにイベントを生成します。

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 =オープン(ファイルパス)はIOErrorを除く リターン(真) : リターン(偽)

そしてclass PageTwoの機能refresh_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 - 現在のをクリア画像がない場合はが表示されます。

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に割り当てます。次に、

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)): 
      ... 
のと電話してください
関連する問題