使用输入框设置变量的值,并使用Tkinter将其显示在Label中

时间:2017-10-19 11:11:09

标签: python python-3.x tkinter tkinter-entry

我正在尝试让用户输入一个数字(在这个例子中,他们需要在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()

1 个答案:

答案 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}}