[WxPython] Advice on Temperature Application Method & Structure

J

Jimbo

Hello

I have made a simple application using WxPython that is a temperature
converter. This is my first application made in WxPython, the only
other apps I have made were in Win32 c++ so I am new to widgets in
general.


I am looking for advice & criticism on when you should use classes in
GUI widget apps (& where) & other general advice:
- Structure of a wxPython script/code
- Should every window be in its own class?
- whats the best way to refer to a control (by window name or ID)
- Should a group of controls that are related be in a class
For example should I make a Celsius class that handles the
celsius editbox, static & button & the button action
function(convert_to_fahrenheit())?
- Errors, bad things I have done in my code
- When & why use a Validator??


Temperature Converter Application:
"""
Temperature Application

Description: This application uses WxPython to create a simple
windows app to convert temperature from celsius
to fahrenheit & visa versa.

Looking for advice on everything to do with wxPython:
For example:
- Structure of a wxPython script/code
- Should every window be in its own class?
- whats the best way to refer to a control (by window name or ID)
- Should a ground of controls that are related be in a class
For example should I make a Celsius class that handles the
celsius editbox, static & button?
- Errors, bad things I have done in my code
- When & why use a Validator??
"""


import wx


## Constants ##
ID_FAHREN = 1
ID_CELSIUS = 2


class Controller(wx.Frame):


def __init__(self, parent, id):

wx.Frame.__init__(self,parent,id,"Temperature Converter",
size=(247,186))

# Create Controls
panel = wx.Panel(self)
self.create_controls( panel, wx.Validator() )

self.fah_edit.SetValue(str(32))
self.cel_edit.SetValue(str(0))

# Define actions
self.Bind_Events()


def create_controls(self, panel, val):
""" Create all the controls we will need for the temperature
conversion """

self.bmp_static = wx.StaticText(panel, -1, "Bitmap pic will be here",
(10,5), (150,40))

self.fah_static = wx.StaticText(panel, -1, "Fahrenheit: ",
(10,70), (-1,-1))

self.cel_static = wx.StaticText(panel, -1,"Celsius: ",
(140,70), (-1,-1))

self.fah_edit = wx.TextCtrl(panel,ID_FAHREN, pos=(70,65),
size=(40,25), style=0, validator=val,
name='a')

self.cel_edit = wx.TextCtrl(panel,ID_CELSIUS, pos=(180,65),
size=(40,25), style=0, validator=val,
name='b')

self.to_fah_button = wx.Button(panel,label="To Fahrenheit",
pos=(145,95),size=(80,25))

self.to_cel_button = wx.Button(panel,label="To Celsius",
pos=(35,95),size=(80,25))

self.exit_button = wx.Button(panel,label="Exit",pos=(157,123),
size=(70,25))



def Bind_Events(self):
""" Bind application events to class functions """

self.Bind(wx.EVT_BUTTON,self.close_button,
self.exit_button)

self.Bind(wx.EVT_CLOSE,self.close_window)

self.Bind(wx.EVT_BUTTON,self.convert_to_celsius,
self.to_fah_button)

self.Bind(wx.EVT_BUTTON,self.convert_to_fahrenheit,
self.to_cel_button)


def close_button(self, event):

self.Close(True)


def close_window(self, event):

self.Destroy()


def error_check(self, string):
""" Check for valid input in editboxes. Valid input
includes float & integer values only """

if string.isalpha() or len(string) <= 0:

return False

return True


def convert_to_fahrenheit(self, event):
""" Convert the value in fahrenheit window to celsius &
display in our window/edit box """

# Algorithm
# - Get number from fahrenheit editbox
# - convert fahrenheit value to celsius
# - change value in Celsius Window to

# Step 1:
fahren_value = self.fah_edit.GetValue()

if not self.error_check( fahren_value ):

self.fah_edit.SetValue( "NA" )
return

# Step 2:
value = (float(fahren_value) - 32) * 5/9

# Step 3:
self.cel_edit.SetValue( str(value) )


def convert_to_celsius(self, event):
""" Convert the value in celcius window/editbox to
fahrenheit & display in our window/editbox """

# Algorithm
# - Get number from celsius editbox
# - convert celsius value to fahrenheit
# - change value in fahrenheit Window

# Step 1:
celsius_value = self.cel_edit.GetValue()

if not self.error_check( celsius_value ):

self.cel_edit.SetValue( "NA" )
return

# Step 2:
value = (float( celsius_value ) * 9/5) + 32

# Step 3:
self.fah_edit.SetValue( str(value) )



if __name__ == "__main__":

app = wx.PySimpleApp()

frame = Controller(parent=None,id=-1)
frame.Show()
app.MainLoop()
 
P

Philip Semanchuk

Hello

I have made a simple application using WxPython that is a temperature
converter. This is my first application made in WxPython, the only
other apps I have made were in Win32 c++ so I am new to widgets in
general.


I am looking for advice & criticism on when you should use classes in
GUI widget apps (& where) & other general advice:

Hi Jimbo,
The first piece of advice I have is to post on the wxPython mailing
list where you'll reach a more focused audience.
- Should every window be in its own class?

That works for me.
- whats the best way to refer to a control (by window name or ID)

ID. Uniqueness is not enforced among window names.
- Should a group of controls that are related be in a class
For example should I make a Celsius class that handles the
celsius editbox, static & button & the button action
function(convert_to_fahrenheit())?

When and where to use a class is not wxPython-specific, and is partly
a matter of taste. For instance, I would not put a
convert_to_fahrenheit() function inside a class, because it doesn't
need any class-related info. Make it a module-level function and it
will be easier to re-use. I wouldn't do what you suggested (grouping
controls into a class) because it doesn't seem to serve any purpose
other than documentation, which can be accomplished just as well with
a comment. Ask yourself this -- what problem would grouping them into
a class solve?

- When & why use a Validator??

I'm ambivalent about Validators. Before I used them I had all of my
init code in one place, and in the on_ok_clicked() event I had all of
my teardown code and it was easy to see what control got assigned to
which variable. Now that information is scattered through a bunch of
classes which are fairly verbose.

Also, Validators are great for validating individual controls, but not
so great with groups. E.g. when control A can only be blank if B is
too. A & B have to know about one another and the Validator starts to
get messy.

OTOH it's nice to group the code that knows everything that needs to
be know about the relationship between a control and the variable to
which is assigned. We call the validator when the control loses focus
(to let users know if they've entered something invalid) and when OK
is clicked. It's nice to be able to simply call the validators in that
case.

Good luck with your app
Philip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top