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()
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()