Python Stringvar实例没有__trunc__method

时间:2013-09-13 17:27:13

标签: python instances

为了好玩(并学习......),我正在尝试编写一个带有3个输入a,b和c的程序,并将解决方案返回到二次公式。 现在,我收到一个错误,说StringVar实例没有属性' trunc ' 我最初将我的入口变量设置为IntVar并且得到了相同类型的错误,而IntVar实例没有 trunc 属性。

import Tkinter
from math import *


def quadprogram(a,b,c):

    x1=((-1*int(b)+(sqrt((int(b)**2)-(4*int(a)*int(c))))/2*int(a)))
    x2=((-1*int(b)-(sqrt((int(b)**2)-(4*int(a)*int(c))))/2*int(a)))

    my_answer="(x+"+str(-1*x1)+")(x+"+str(-1*x2)+")"
    xinputs= "x1= "+ str(x1) + " and x2= "+str(x2)
    answers= my_answer+", "+ xinputs
    return answers

class quadratic(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent=parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariableA = Tkinter.StringVar()
        self.entry= Tkinter.Entry(self, textvariable= self.entryVariableA)
        self.entry.grid(column=0,row=2,sticky="W")
        self.entry.bind("<Return>",self.OnPressEnter)
        self.entryVariableA.set(u"a")

        self.entryVariableB = Tkinter.StringVar()
        self.entry= Tkinter.Entry(self, textvariable= self.entryVariableB)
        self.entry.grid(column=0,row=3,sticky="W")
        self.entry.bind("<Return>",self.OnPressEnter)
        self.entryVariableB.set(u"b")

        self.entryVariableC = Tkinter.StringVar()
        self.entry= Tkinter.Entry(self, textvariable= self.entryVariableC)
        self.entry.grid(column=0,row=4,sticky="W")
        self.entry.bind("<Return>",self.OnPressEnter)
        self.entryVariableC.set(u"c")




        button = Tkinter.Button(self, text= u"Solve!", command=self.OnButtonClick)
        button.grid(column=1,row=5)

        self.labelVariable= Tkinter.StringVar()
        self.Eq_labelVariable=Tkinter.StringVar()
        self.Ans_labelVariable=Tkinter.StringVar()

        label= Tkinter.Label(self, textvariable=self.labelVariable,anchor= 'w', fg='black',bg='blue')
        label.grid(column=0,row=0, columnspan=2, sticky='EW')
        self.labelVariable.set(u"Enter Equation Here...")

        Eq_label=Tkinter.Label(self, textvariable=self.Eq_labelVariable,anchor='w', fg='white', bg='blue')
        Eq_label.grid(column=0, row=1, columnspan=2, sticky='EW')
        self.Eq_labelVariable.set(u"For A(x^2)+B(x)+C")

        Ans_label=Tkinter.Label(self, textvariable=self.Ans_labelVariable,anchor='w', fg='black', bg='green')
        Ans_label.grid(column=0, row=1, columnspan=2, sticky='EW')
        self.Ans_labelVariable.set(u"Answer will show here")


        self.grid_columnconfigure(0,weight=1)
        self.resizable(True, False)
        self.update()
        self.geometry(self.geometry())
        self.entry.focus_set()
        self.entry.selection_range(0,Tkinter.END)



    def OnButtonClick(self):
        self.Ans_labelVariable.set(quadprogram(self.entryVariableA,self.entryVariableB,self.entryVariableC)+"(this program works?!)")
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        self.labelVariable.set(self.entryVariableA.get())


if __name__=='__main__':
    app=quadratic(None)
    app.title("Quadratic Solver")
    app.geometry("300x300")
    app.mainloop()

任何人都有任何避免这种想法的想法? 谢谢你的帮助...

1 个答案:

答案 0 :(得分:0)

创建类时,所有内容都会被初始化,包括条目小部件中的任何值。创建方法以检索条目小部件中的值,并使这些方法不在初始化之中。我会在几分钟内用代码更新答案。