Python 3.x Python:如何在标签小部件(tkinter)中动态显示图像
我一直在尝试基于上一页上单击的按钮在tk页面上动态显示特定图像。PageOne有5个图像,每个图像下面有5个按钮。单击特定按钮应将用户带到第二页,如果单击第三个按钮,则显示图像3 我已经知道如何根据单击的按钮传递1到5的值,并且图像保存为pic1.gif,…pic5.gif,因此要返回正确的图像,我只需要将值附加到文件位置 我正在努力弄清楚如何在访问第二页时刷新它 任何帮助都将不胜感激,谢谢Python 3.x Python:如何在标签小部件(tkinter)中动态显示图像,python-3.x,tkinter,Python 3.x,Tkinter,我一直在尝试基于上一页上单击的按钮在tk页面上动态显示特定图像。PageOne有5个图像,每个图像下面有5个按钮。单击特定按钮应将用户带到第二页,如果单击第三个按钮,则显示图像3 我已经知道如何根据单击的按钮传递1到5的值,并且图像保存为pic1.gif,…pic5.gif,因此要返回正确的图像,我只需要将值附加到文件位置 我正在努力弄清楚如何在访问第二页时刷新它 任何帮助都将不胜感激,谢谢 TITLE_FONT = ("Helvetica", 18, "bold") class SampleA
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)
从
PageOne
中选择的图像不会绘制在PageTwo
上,因为绘图函数是在\uuuu init\uuuu()
函数中本地化的,当调用函数tkraise()
重新绘制PageTwo
时,不会引发任何事件
问题1-调用第二页的tkraise()
时生成事件
在这里,Python的OOP将通过重写函数来解决问题
tkraise()
在class PageTwo
中
问题2-使用类第二页的功能定位图标的绘图
要考虑新选定的图标,请创建一个函数
class PageTwo
中的refresh_icon()
,并从这两个页面调用它
\uuuu init\uuuuu()
和tkraise()
函数
在\uuuu init\uuuu()函数的末尾添加
class PageTwo(tk.Frame):
...
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
...
self.refresh_icon()
奖金1-为防止图像丢失时出现异常,请在之前添加检查
创建一个文件\u exists()
函数,然后在加载之前进行检查
PhotoImage()
def文件_存在(文件路径):
尝试:
fp_文件=打开(文件路径)
返回(真)
除IOError外:
返回(假)
在类第二页的函数刷新图标()
中:
self.location = 'C:/Users/XXXXXXX/Documents/pic'+str(self.get_id())+'.gif'
if (file_exists(self.location)):
icon = tk.PhotoImage(file=self.location)
...
Bonus 2-如果图像未加载,请清除当前的IDlabel
在PageTwo
中创建新的标签时,变量
self.IDlabel
将存储新图像而不删除旧图像。
在创建新文件之前,请调用destroy()
函数
class PageTwo(tk.Frame):
...
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
...
self.refresh_icon()
添加变量self.IDlabel
的声明,并将其分配给\uuuu init\uuu()
函数中的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)):
...
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)):
...