0

what i really need help with is the function on line 112. I am trying to create a GUI calculator the only part i am having trouble with is when the person enters a problem like 5+5 it isnt able to store that in a varible. So how would i solve the problem and then output it to the text box. thank you

import math
import wx

class justin(wx.Frame):

    loop=False   

    def __init__(self,parent,id):

        wx.Frame.__init__(self,parent,id,'MAP2', size=(250, 300))
        panel=wx.Panel(self)

        status=self.CreateStatusBar()
        file_menu = wx.MenuBar()
        menu1=wx.Menu()
        menu2=wx.Menu()
        exit1=menu1.Append(wx.NewId(),"exit","exit")
        info1=menu1.Append(wx.NewId(),"info","info")
        instr=menu2.Append(wx.NewId(),"instructions","instructions")
        file_menu.Append(menu1,"file")
        file_menu.Append(menu2,"instructions")
        self.SetMenuBar(file_menu)

        self.panel = panel

        self.Bind(wx.EVT_MENU, self.onexit, exit1)
        self.Bind(wx.EVT_MENU, self.oninfo, info1)
        self.Bind(wx.EVT_MENU, self.oninstr, instr)

        self.box=wx.TextCtrl(panel,-1,"",(0,0),(180,45))        

        self.button1=wx.Button(panel,label='1', size=(45,45))
        self.button2=wx.Button(panel,label='2', size=(45,45))
        self.button3=wx.Button(panel,label='3', size=(45,45))
        self.button4=wx.Button(panel,label='4', size=(45,45))
        self.button5=wx.Button(panel,label='5', size=(45,45))
        self.button6=wx.Button(panel,label='6', size=(45,45))
        self.button7=wx.Button(panel,label='7', size=(45,45))
        self.button8=wx.Button(panel,label='8', size=(45,45))
        self.button9=wx.Button(panel,label='9', size=(45,45))
        self.buttonplus=wx.Button(panel,label='+', size=(45,45))
        self.buttonsub=wx.Button(panel,label='-', size=(45,45))
        self.buttonmul=wx.Button(panel,label='*', size=(45,45))
        self.buttondiv=wx.Button(panel,label='/', size=(45,45))
        self.button0=wx.Button(panel,label='0', size=(45,45))
        self.enter=wx.Button(panel,label='enter',size=(45,45))

        siz=wx.GridBagSizer(5,5)
        siz.Add(self.button1,pos=(2,0))
        siz.Add(self.button2,pos=(2,1))
        siz.Add(self.button3,pos=(2,2))
        siz.Add(self.button4,pos=(3,0))
        siz.Add(self.button5,pos=(3,1))
        siz.Add(self.button6,pos=(3,2))
        siz.Add(self.button7,pos=(4,0))
        siz.Add(self.button8,pos=(4,1))
        siz.Add(self.button9,pos=(4,2))
        siz.Add(self.buttonplus,pos=(2,3))
        siz.Add(self.buttonsub,pos=(3,3))
        siz.Add(self.buttonmul,pos=(4,3))
        siz.Add(self.buttondiv,pos=(5,3))
        siz.Add(self.button0,pos=(5,2))
        siz.Add(self.enter,pos=(5,0))

        panel.SetSizer(siz)

        self.Bind(wx.EVT_BUTTON,self.ebutton1,self.button1)
        self.Bind(wx.EVT_BUTTON,self.ebutton2,self.button2)
        self.Bind(wx.EVT_BUTTON,self.ebutton3,self.button3)
        self.Bind(wx.EVT_BUTTON,self.ebutton4,self.button4)
        self.Bind(wx.EVT_BUTTON,self.ebutton5,self.button5)
        self.Bind(wx.EVT_BUTTON,self.ebutton6,self.button6)
        self.Bind(wx.EVT_BUTTON,self.ebutton7,self.button7)
        self.Bind(wx.EVT_BUTTON,self.ebutton8,self.button8)
        self.Bind(wx.EVT_BUTTON,self.ebutton9,self.button9)
        self.Bind(wx.EVT_BUTTON,self.ebutton0,self.button0)
        self.Bind(wx.EVT_BUTTON,self.ebuttonplus,self.buttonplus)
        self.Bind(wx.EVT_BUTTON,self.ebuttonsub,self.buttonsub)
        self.Bind(wx.EVT_BUTTON,self.ebuttonmul,self.buttonmul)
        self.Bind(wx.EVT_BUTTON,self.ebuttondiv,self.buttondiv)
        self.Bind(wx.EVT_BUTTON,self.eenter,self.enter)


    def ebutton1(self,e):
        self.box.AppendText('1')
    def ebutton2(self,e):
        self.box.AppendText('2')
    def ebutton3(self,e):
        self.box.AppendText('3')
    def ebutton4(self,e):
        self.box.AppendText('4')
    def ebutton5(self,e):
        self.box.AppendText('5')
    def ebutton6(self,e):
        self.box.AppendText('6')
    def ebutton7(self,e):
        self.box.AppendText('7')
    def ebutton8(self,e):
        self.box.AppendText('8')
    def ebutton9(self,e):
        self.box.AppendText('9')
    def ebutton0(self,e):
        self.box.AppendText('0')
    def ebuttonplus(self,e):
        self.box.AppendText('+')
    def ebuttonsub(self,e):
        self.box.AppendText('-')
    def ebuttonmul(self,e):
        self.box.AppendText('*')
    def ebuttondiv(self,e):
        self.box.AppendText('/')
    def eenter(self,e):
        a=self.box.GetValue()
        answer=int(a)
        ans=str(answer)
        self.box.SetValue(ans)


    def onexit(self,e):
        self.Close()

    def oninfo(self,e):
        dlg = wx.MessageDialog(None,"This is a program that computes percent error value. MAP2 is created by Justin Rolf","info",wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def oninstr(self,e):
        dlg1 = wx.MessageDialog(None,"To use this program enter your first number in the top gray box (make sure to have all text deleted before you type), then enter your second number in the bottom box, and hit the enter button","instructions",wx.OK)
        dlg1.ShowModal()
        dlg1.Destroy()


if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=justin(parent=None, id=-1)
    frame.Show()
    app.MainLoop()
1
  • Please try to trim down the code when you're posting a question. I've better things to do than try to read all that, given that the vast majority of it looks to be irrelevant. Working out what is and isn't relevant is a good exercise for finding bugs, too. Commented Jan 3, 2012 at 17:44

1 Answer 1

1

I think your problem is here:

def eenter(self,e):
    a=self.box.GetValue()
    answer=int(a)
    ans=str(answer)
    self.box.SetValue(ans)

It doesn't work because a is a string that can not be converted to an integer directly.
You can see here an example of a wxPython calculator. It takes less than 50 lines of code so you can learn a lot from this code. Another example is here You will discover they use python built-in function eval to process the entered string. For your case this translates into:

answer = eval(a)

eval is often not recommended for general code. As it executes any string it gets, it is considered dangerous, However, for your case, it is a valid alternative (another would be to parse the input string and bind operator chars with python operator methods or custom functions).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but now when I run a equation like 5/6 it will round and to make it work i need to print 5. / 6. how can i make that so that it defines as a float or is there another way
Add from __future__ import division. Please select the answer if it was useful to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.