question

Johan Beke johanbeke at hotmail.com
Wed Nov 15 04:46:32 PST 2006


 
Hello, 
 
A little question, 've writen this piece of code to solve quadratic equations
 
 
# Created with FarPy GUIE v0.5.2
 
import wx
 
# returns a solution string
def solve_equ(a,b,c):
    if a==0:
        if b==0:
            if c==0:
                sol = "infinity solutions"
            else:
                sol = "no solutions"
        else:
            sol = "x = " + str(float(-1*c)/float(b))
    else:
        d=b**2-4*a*c
        D="D = "+str(d)+"\n"
        if d==0:
            sol = D+"x = " + str(float(-1*b)/float(2*a))
        elif d>0:
            sol = D+"x1 = " + str((-1*b+m.sqrt(d))/(2*a)) + "\n" + "x2 = " + str((-1*b-m.sqrt(d))/(2*a))
        else:
            sol = D+"x1 = " + str(float(-1*b)/float(2*a))+"+j"+str(m.sqrt(-1*d)/(2*a))+"\nx2 = " + str(float(-1*b)/float(2*a))+"-j"+str(m.sqrt(-1*d)/(2*a))
    return sol      
 
 
class MyFrame(wx.Frame):	
	def __init__(self, parent, title):
		wx.Frame.__init__(self, parent, -1, 'quadratic equation', wx.DefaultPosition, (546, 184), style=wx.CLOSE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.RESIZE_BORDER | wx.FRAME_NO_TASKBAR | 0 | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX)
		self.panel = wx.Panel(self, -1)
 
		self.a = wx.TextCtrl(self.panel, -1, '', (6,8), size=(100, 20))
		self.a.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.a.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.a.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.label4 = wx.StaticText(self.panel, -1, 'x^2 + ', (111,8), (47, 17))
		self.label4.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label4.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.b = wx.TextCtrl(self.panel, -1, '', (159,8), size=(100, 20))
		self.b.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.b.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.b.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.label5 = wx.StaticText(self.panel, -1, 'x + ', (263,8), (38, 25))
		self.label5.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label5.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.c = wx.TextCtrl(self.panel, -1, '', (302,8), size=(100, 20))
		self.c.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.c.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.c.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.label6 = wx.StaticText(self.panel, -1, ' = 0', (407,7), (40, 23))
		self.label6.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label6.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.bsolve = wx.Button(self.panel, -1, 'solve', (454,5), (78, 26))
		self.bsolve.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.bsolve.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.solutions = wx.TextCtrl(self.panel, -1, '', (87,62), size=(353, 62), style=wx.TE_MULTILINE)
		self.solutions.SetBackgroundColour(wx.Colour(255, 255, 255))
		self.solutions.SetFont(wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.solutions.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		self.label7 = wx.StaticText(self.panel, -1, 'solutions :', (7,62), (80, 23))
		self.label7.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, 'Microsoft Sans Serif'))
		self.label7.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
		wx.EVT_BUTTON(self,ID_bsolve,self.Solve)
 
		
#---------------------------------------------------------------------------
class MyApp(wx.App):
	def OnInit(self):
		frame = MyFrame(None, 'App')
		frame.Show(True)
		self.SetTopWindow(frame)
		return True
        def Solve(self,event):
            a=self.a.GetValue()
            b=self.b.GetValue()
            c=self.c.GetValue()
            solution = solve_equ(a,b,c)
            self.solutions.SetValue(solution)
        
app = MyApp(True)
app.MainLoop()
 
 
But  how can I createn an event handler that, when I click the button (bsolve), the function Solve is called
 
 
thx


Johan

_________________________________________________________________
Probeer Live.com: je eigen snelle startpagina met alles wat jij belangrijk vindt op één plek.
http://www.live.com/getstarted



More information about the wxpython-users mailing list