我正在尝试让用户输入一个数字(在这个例子中,他们需要在minEntry
框中输入几分钟)。我想取这个数字并用它来设置多个变量值,方法是将该值乘以其他数字。然后我想获取此值并将其显示在sublabel
函数中的__init__
框中。我收到一条显示'PageSix' object has no attribute 'peak_rate'
的错误消息。我知道这意味着什么,但我不知道如何解决它。
class PageSix(tk.Frame):
def projected_figures(self):
global minEntry
tariff = self.controller.page_get(PageTwo)
minutes=minEntry.get()
self.peak_rate = tk.StringVar()
self.peak_rate.set(0)
self.off_peak = tk.StringVar()
self.off_peak.set(0)
self.line_rental = tk.StringVar()
self.line_rental.set(0)
if tariff.current_tariff == "A":
self.peak_rate.set("Peak Rate: £"+minutes*0.3)
self.off_peak.set("Off-Peak: £"+minutes*0.05)
self.line_rental.set("Line Rental: £15")
elif tariff.current_tariff == "B":
self.peak_rate.set("Peak Rate: £"+minutes*0.1)
self.off_peak.set("Off-Peak: £"+minutes*0.02)
self.line_rental.set("Line Rental: £20")
else:
self.peak_rate.set("Peak Rate: £"+minutes*0.9)
self.off_peak.set("Off-Peak: -")
self.line_rental.set("Line Rental: £30")
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller=controller
label = tk.Label(self, text="Account Balance", font=LARGE_FONT)
label.pack(pady=10,padx=10)
sublabel = tk.Label(self, textvariable=self.peak_rate, font=SMALL_FONT)
sublabel.pack(pady=10,padx=10)
sublabel2 = tk.Label(self, textvariable=self.off_peak, font=SMALL_FONT)
sublabel2.pack(pady=10,padx=10)
sublabel3 = tk.Label(self, textvariable=self.line_rental, font=SMALL_FONT)
sublabel3.pack(pady=10,padx=10)
minLabel = Label(self, text = 'Enter Minutes: ')
minEntry = Entry(self)
minLabel.pack(pady =10, padx = 10, side = TOP, anchor = S)
minEntry.pack(pady =10, padx = 10, side = TOP, anchor = S)
button1 = tk.Button(self, text="View Projected Figures",
command=self.projected_figures)
button1.pack()
button2 = tk.Button(self, text="Back to Menu",
command=lambda: controller.show_frame(StartPage))
button2.pack()
此行tariff = self.controller.get_page(PageTwo)
和if self.current_tariff == "x"
如果有任何用途,也会引用此类。
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller=controller
self.current_tariff = tk.StringVar()
self.current_tariff.set("A")
label = tk.Label(self, text="Current Tariff", font=LARGE_FONT)
label.pack(pady=10,padx=10)
sublabel = tk.Label(self, textvariable=self.current_tariff, font=SMALL_FONT)
sublabel.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Change Tariff",
command=lambda: controller.show_frame(PageSix))
button1.pack()
button2 = tk.Button(self, text="Projected Figures",
command=lambda: controller.show_frame(PageSix))
button2.pack()
button3 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button3.pack()
答案 0 :(得分:0)
__init__
,并在调用时调用projected_figures
。
当您尝试在PageSix
创建行__init__
内部初始化sublabel
时,由于self.peak_rate
没有projected_figures
,因此没有名为__init__
的变量。我打来电话。
在def __init__(self, parent, controller):
...
self.peak_rate = tk.StringVar()
self.peak_rate.set(0)
self.off_peak = tk.StringVar()
self.off_peak.set(0)
self.line_rental = tk.StringVar()
self.line_rental.set(0)
...
内移动变量创建应解决此问题。
{{1}}