我正在创建一个多帧tkinter应用程序,并且希望用户在一个帧子类上输入内容以更新在另一个帧子类小部件上显示的图像。
唯一能够更新图像的方法是通过.config()。我知道松散或紧密的耦合是答案,我在执行时遇到了麻烦。
from tkinter import *
class GUI(Tk): # Main Tk SubClass
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
# Photo Instances
self.PI1= PhotoImage(file='Image1.png')
self.PI2= PhotoImage(file='Image2.png')
# Frame Control
container = Frame(self)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
container.pack(side="top",fill="both", expand=1)
self.frames = {}
for F in (Page_1, Page_2):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.showFrame("Page_1")
def showFrame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class Page_1(Frame): # I/P SubClass
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
def Input():
# Page_2.tbut.config(image='pyimage2') ?????
pass
Button(self, text='ChangeImage', command =lambda: Input()).pack()
#Access Page 2
Button(self, text='Go to Page 2', command=lambda: self.controller.showFrame("Page_2")).pack()
class Page_2(Frame): # O/P SubClass
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
self.tbut=Button(self, image='pyimage1', height=45, width=45)
self.tbut.pack()
#Access Page 1
Button(self, text='Go to Page 1', command=lambda: self.controller.showFrame("Page_1")).pack()
app = GUI()
app.mainloop()
非常感谢您的帮助!