以下是一些相关代码:
class root_win(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.renderRows()
self.title("Scheduler")
self.geometry('150x110')
def renderRows(self):
self.row_1 = row_1(self)
self.row_2 = row_2(self)
self.row_2_button = row_2_button(self.row_2)
class row_2(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.pack(side="top", anchor='w',padx="6")
class row_2_button(row_2):
def __init__(self, parent):
row_2.__init__(self, parent)
self.parent = parent
self.widgets()
self.pack(side="left", anchor='w',padx="6")
def widgets(self):
self.run = Button(self, text="Run", padx="3", command=self.action)
self.run.pack(side="left")
def action(self):
print(row_1.month.get())
class row_1(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.month = StringVar()
self.pack(side="top", anchor='w',padx="6")
self.widgets()
def widgets(self):
self.month_label = Label(self, text="Month:", anchor="w", padx="3", pady="3", width=12)
self.month_label.pack(side="left")
self.month_entry = Entry(self, bd=2, width=4, textvariable = self.month)
self.month_entry.pack(side="right")
def give(self):
pass
if __name__ == "__main__":
app = root_win(None)
app.mainloop()
如果您查看 row_2_button ,该按钮将运行action
功能。如果我使用虚拟打印语句,则执行效果很好。但是,当我要从 row_1 中获取month
变量时,会出现错误。
我尝试过的第一件事; print(row_1.month)
给了我row_1 has no attribute month
。然后我尝试使用row_1.month.get()
,这给了我同样的错误。在将row1.give()
填充到return self.month
函数之后,我应该使用它吗?虽然不确定如何正确调用它。提示表示感谢,谢谢!