我需要从另一个类buttonReturn
访问对象Return
,以便可以在其上调用方法。
我想通过将buttonReturn
隐藏在视图(x=-100
)的外面来使其隐藏在第一页上,然后使用下一页按钮将返回按钮放置在视图中。
我尝试使用Return.buttonReturn.place(x=0, y=0)
,但它给了我AttributeError: type object 'Return' has no attribute 'buttonReturn'
下面是我可以减少的程序
import tkinter as tk
PreviousPage = None
class Controller(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
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 (FirstPage, SecondPage, Return):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.geometry("200x100")
self.show_frame(FirstPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Return(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
buttonReturn = tk.Button(text="return", command=lambda: controller.show_frame(PreviousPage))
buttonReturn.place(x=-100, y=0)
class FirstPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="first page")
label.place(x=0, y=50)
buttonA = tk.Button(self, text="next page", command=lambda: nextPage())
buttonA.place(x=70, y=0)
def nextPage():
global PreviousPage
PreviousPage = FirstPage
Return.buttonReturn.place(x=0, y=0) #problematic code
controller.show_frame(SecondPage)
class SecondPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="second page")
label.place(x=0, y=50)
app = Controller()
app.mainloop()
答案 0 :(得分:1)
您没有创建要调用的Return对象,因此您只是在调用该类。 要获得想要发生的变化,请进行以下更改:
Return.buttonReturn.place(x=0, y=0)
到
returnbutton = Return(parent, controller)
returnbutton.buttonReturn.place(x=0, y=0)
然后在Return类中,将自己添加到buttonReturn语句的前面:
self.buttonReturn = tk.Button(text="return", command=lambda: controller.show_frame(PreviousPage))
self.buttonReturn.place(x=-100, y=0)