有没有一种方法可以在__init__中分配变量而不让它们立即运行?

时间:2020-04-28 20:43:37

标签: python tkinter

因此,我试图编写一个程序,将各种度量单位转换为其他单位。 (即Cm到Mm)我的教授坚决不使用global,而将所有代码都放在函数或类中。使用如下所示的代码,它给了我一个

ValueError  (*line 18, in __init__ self.intNtry = int(self.ntryAnswer) ValueError: invalid literal for int() with base 10: ''*)

有什么方法可以使程序启动后不立即激活变量?我是编程新手,请不要欺负我

class Converter(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.txt = tk.Text(self, height=1, width=45)
        self.txt1 = tk.Text(self, height=1, width=45)
        self.txt2 = tk.Text(self, height=1, width=45)
        self.txt3 = tk.Text(self, height=3, width=45)

        self.unit1 = tk.Entry(self)
        self.unit2 = tk.Entry(self)
        self.num1 = tk.Entry(self)

        self.btn = tk.Button(self, text="Calculate", padx=15, pady=15, command=self.buttonClick)

        **self.ntryAnswer = self.num1.get()
        self.intNtry = int(self.ntryAnswer)**

        self.initWindow()

    def buttonClick(self):
        if self.unit1 == "cm" and self.unit2 == "m":
            ans1 = float(self.intNtry) / 100
            print(ans1)

3 个答案:

答案 0 :(得分:2)

您的课程不需要intNTry属性。在您准备好self.num1.get()返回的值之前,即您实际单击该按钮时,该值才变得有趣。

class Converter(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.txt = tk.Text(self, height=1, width=45)
        self.txt1 = tk.Text(self, height=1, width=45)
        self.txt2 = tk.Text(self, height=1, width=45)
        self.txt3 = tk.Text(self, height=3, width=45)

        self.unit1 = tk.Entry(self)
        self.unit2 = tk.Entry(self)
        self.num1 = tk.Entry(self)

        self.btn = tk.Button(self, text="Calculate", padx=15, pady=15, command=self.buttonClick)

        self.initWindow()

    def buttonClick(self):
        if self.unit1 == "cm" and self.unit2 == "m":
            answer = self.num1.get()
            entry = int(answer)
            ans1 = entry / 100
            print(ans1)

如果有多个地方值得检索该值(或者如果您只是想使用更多封装),则适合使用suggested by @modesitt这样的属性。

class Converter(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.txt = tk.Text(self, height=1, width=45)
        self.txt1 = tk.Text(self, height=1, width=45)
        self.txt2 = tk.Text(self, height=1, width=45)
        self.txt3 = tk.Text(self, height=3, width=45)

        self.unit1 = tk.Entry(self)
        self.unit2 = tk.Entry(self)
        self.num1 = tk.Entry(self)

        self.btn = tk.Button(self, text="Calculate", padx=15, pady=15, command=self.buttonClick)

        self.initWindow()

    @property
    def field1(self):
        return int(self.num.get()) / 100

    def buttonClick(self):
        if self.unit1 == "cm" and self.unit2 == "m":
            print(self.field1)

答案 1 :(得分:1)

使用property

class Converter(Frame):
    ...
    @property
    def intNtry(self):
        ntryAnswer = self.num1.get()
        return int(self.ntryAnswer)

,它将动态评估何时调用.intNtry。尽管没有理由存储此变量-在.self.num.get()期间需要此值时可以调用buttonClick。不过,一般而言,计算属性应使用@property

答案 2 :(得分:0)

您始终可以将该变量分配为None,然后创建一个设置它的附加方法:

def __init__(self, parent):
   self.intNtry = None

def foo(self):
   self.intNtry = int(self.ntryAnswer)