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

Python 有没有一种方法可以在_init__中分配变量而不立即运行它们?,python,tkinter,Python,Tkinter,所以我试着写一个程序,把各种公制单位转换成其他单位。i、 我的教授坚持不使用globals,我们所有的代码要么在函数中,要么在类中。代码如下所示,它为我提供了一个: ValueError (*line 18, in __init__ self.intNtry = int(self.ntryAnswer) ValueError: invalid literal for int() with base 10: ''*) 有没有办法让变量在程序启动后不立即激活?我是编程新手,请不要欺负我 clas

所以我试着写一个程序,把各种公制单位转换成其他单位。i、 我的教授坚持不使用globals,我们所有的代码要么在函数中,要么在类中。代码如下所示,它为我提供了一个:

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)
使用

调用.intNtry时将动态计算。但是,没有理由存储此变量-.self.num.get可以在单击按钮时需要该值时调用。但一般来说,计算属性应该使用@property

使用


调用.intNtry时将动态计算。但是,没有理由存储此变量-.self.num.get可以在单击按钮时需要该值时调用。但一般来说,计算属性应该使用@property

您可以始终将该变量指定为“无”,然后创建一个附加的方法来设置该变量:

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

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

您可以始终将该变量指定为“无”,然后创建一个附加方法来设置该变量:

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

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

您的类不需要intry属性。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)
如果在多个地方检索值很有意思,或者如果您只是想使用更多的封装,那么可以使用一个适当的属性

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)

您的类不需要intry属性。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)
如果在多个地方检索值很有意思,或者如果您只是想使用更多的封装,那么可以使用一个适当的属性

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)

self.inNtry根本不应该存在。只需调用self.num1.get inside按钮click.self.inNtry根本不应该存在。只需调用self.num1.get inside button单击.OP应该接受这个答案-它解释了你应该做得更好的地方。OP应该接受这个答案-它解释了你应该做得更好的地方。