2017-03-01 246 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)): 
      ... 
相关问题