I have used python for awhile, and am quite good with it. But last week I added the wxpython..I wanted to actually make a program and not a script.

So I can do this in normal python all day long. But wxpython is alot different, so I need help with doing a math function. The function is finding the Area of a circle which is really easy (input number)*Radius*Radius) ya.

So I made a program, and here is the code part I just need a "Helper code" or somewhere to start from, tutorial..reference..I dont want to be told HOW to do it, unless you want to explain HOW you did it. I intend to actually learn..

button=wx.Button(panel,label="Radial Entry",pos=(10,10),size=(70,25))
        self.Bind(wx.EVT_BUTTON, self.OnClick)

    def OnClick(self, event):
        TED=wx.TextEntryDialog(None,"Enter Radius","Radial Entry","default text")
        if TED.ShowModal()==wx.ID_OK:
            answer=TED.GetValue()

So here it is..I made a button,simple...I made it to when you click the button, a dialog box pops up..That is where I want the person to input the "Radius" of the circle or math problem. Then, I want it to perform the math problem by doing the formula above^, then put it in the main window panel, or another box, does not matter to me I just need help getting the basics of doing math with wxpython.

Recommended Answers

All 3 Replies

First, you should change the line where you bind the button to: button.Bind(wx.EVT_BUTTON, self.onClick) or else give the button an id (it's the arg passed second in the button's constructor) and then you can use self.Bind(wx.EVT_BUTTON, self.onClick, id=BTN_ID) . The way you're currently doing it, any button's press will call the onClick function, not just a press on your specific button.

Math is the same in wxPython as Python. The one difference is that you'll be getting input via widgets (not raw_input) and you'll be showing the answer differently (through a widget instead of print).

That being said, when you use GetValue() on a wx.TextCtrl, it returns a string (same deal as raw_input). So you'll need to convert it to an int or float first before you can do calculations. I'd also suggest adding error checking in this part so that it re-asks for a number if bad input was given (i.e. letters, punctuation, etc). The str class in Python has a very handy function "isdigit()" that you may try to use for this.

Once you've converted this to an int or float, then you just need to perform the simple PI * radius^2 calculation. The math module contains a PI constant you can use: from math import pi .

As for output, you have tons of options. Here I'll just show a wx.MessageDialog way of alerting the user to the answer:

# parent, message, title, style (Ok btn and 'information' icon)
dlg = wx.MessageDialog(self, "The answer is ___", "Area of circle", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()

(wx.MessageDialog API doc.)

This is just a simple example; if you have a TextCtrl on your frame for output you could just use outTC.SetValue(answer) , but note that SetValue() overwrites any other text in that widget. You could use the AppendText() function which wouldn't overwrite it if you wanted to retain past output. (Here's the TextCtrl API doc if you need it.)

So as you can see, there are a multitude of ways of giving output to the user.

Thanks a ton for this. I am still unclear about some of the working of wx. I tend to make things 10x as hard as they need to actually be.

So that said.. Would you use int? float? or dose this really matter in terms of good code?
what is the proper way to do a float in wxpython?

I want to make sure I do this correctly..

I'd go with using floats because I'm sure you want to allow for using decimal numbers in your program. And as I said before, math in Python = math in wxPython. All that wxPython is, is a wrapper for the wxWidgets GUI toolkit. It doesn't change Python's language engineering or anything...

So you can make something a float the same way you would in Python: float(variable) . So if you had a string like "11.45" and you called float() on it, you'd get back 11.45 as a number (float), rather than the string. Now you can do calculations with it. You'll want error-catching in your code though so that something like "asd2-)_d2" will get rejected by your program and it will ask for input again until it gets a valid number (I mentioned the str classes' isdigit() function).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.