【问题标题】:Making "class " work in tkinter让“课堂”在 tkinter 中工作
【发布时间】:2015-04-23 15:00:56
【问题描述】:

我一直在尝试在 pythons Tkinter 中进行类工作。 但是,当用“if name == “main””调用它时,如果你有任何解决这个问题的方法,我会非常感激。如果你有任何解决方案不涉及我使用的“IF”语句请告诉我。

class Calculo:
     def _init_(self,parent):

        def calcular_moduloV1(self):
                if Vel_in_y.get()>0 and Angulo_desp.get():
                    self.modulo = Vel_in_y.get()* math.sin(math.radians(Angulo_desp.get()))  
                    label = tkinter.Label(frame,text=self.modulo)
                    label.pack()
        def calcular_moduloV2(self):
            if Vel_in_x.get()>0 and Vel_in_y.get()>0 and self.modulo == 0:
                self.modulos = math.sqrt(Vel_in_x.get()**2+ Vel_in_y.get()**2)
                label = tkinter.Label(frame,text=self.modulos)
                label.pack()
        def calcular_anguloD(self):
            if Vel_in_y.get()>0 and Vel_in_x.get()>0:
                self.angulo = math.degrees(math.tanh(Vel_in_y.get()/Vel_in_x.get()))
                label = tkinter.Label(frame,text=self.angulo)
                label.pack()
        def calcular_Vel_in_y(self):
            if Angulo_desp.get()>0 and Velocidad_in.get()>0 and Vel_in_y.get()==0:
                self.Vel_y = Velocidad_in.get()*math.sin(math.radians(Angulo_desp.get()))
                label = tkinter.Label(frame,text=self.Vel_y)
                label.pack()
        def calcular_Vel_in_x(self):
            if Angulo_desp.get()>0 and Velocidad_in.get()>0 and Vel_in_x.get()==0:
                self.Vel_x= Velocidad_in.get()*math.cos(math.radians(Angulo_desp.get()))
                label = tkinter.Label(frame,text=self.Vel_x)
                label.pack()

        button = tkinter.Button(frame,text='respuesta' ,command = 
                                (self.calcular_moduloV1,self.calcular_moduloV2,self.calcular_anguloD,self.calcular_Vel_in_y,self.calcular_Vel_in_x))
        button.pack()
if _name_ == "_main_":
    window =tkinter.Tk()
    myapp = Calculo(window)
    window.mainloop()

(这只是代码的一部分……如果您需要整个代码,请给我发消息谢谢!!!)

【问题讨论】:

  • if __name__ == "__main__"。每边双下划线
  • 运行该脚本时遇到什么错误?
  • 问题是这个类永远不会运行(即使我放了双下划线)
  • 你确定你把双下划线放在运算符两边吗?
  • 如果您只从该文件运行它,则不需要 if 子句。你知道你为什么用这个词吗?

标签: python class object tkinter


【解决方案1】:

几厘米。

第一个

def _init_(self,parent):

应该是

def __init__(self,parent):

第二,正如其他人指出的那样: _name_ == "_main_"

应该是

__name__ == "__main__"

第三。这个按钮没有意义(尤其是命令):

    button = tkinter.Button(frame,text='respuesta' ,command = 
                     (self.calcular_moduloV1, self.calcular_moduloV2,self.calcular_anguloD, self.calcular_Vel_in_y,self.calcular_Vel_in_x))
button.pack()

你想在这里实现什么?框架也没有定义。

第四,def calcular_moduloV1(self): 正文中的缩进错误。

【讨论】:

    猜你喜欢
    • 2017-01-09
    • 1970-01-01
    • 2014-01-13
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多