The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
My first 'real' program
Discuss My first 'real' program in the Python Programming forum on Dev Shed. My first 'real' program Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2004, 07:24 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 44
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
My first 'real' program
Sorry for posting this... this is the first time i make a program that really looks like a program(well.. not really) so I need a little advice or comment.. there's may be a mistake in the program..please say something so I can make an improvement... this program just convert a number to either CM or INCH( I'm sure about accuracy)... if you put the number on CM box it'll conver to inch and vice versa... comment please.. thanx
the code
Code:
from wxPython.wx import *
class testdialog(wxDialog):
def __init__(self,parent,ID,title,pos,size):
id = wxNewId()
wxDialog.__init__(self,parent,id,title,pos,size)
panel = self
#Label
wxStaticBox(panel,-1,'Cm Value',(3,3),(120,43))
wxStaticBox(panel,-1,'Inch Value',(3,48),(120,43))
#Text Entry
self.cm = wxTextCtrl(panel,-1,'',(14,18))
self.inch = wxTextCtrl(panel,-1,'',(14,63))
#Buttons
wxButton(panel,10,'Count',(3,100),(120,25))
wxButton(panel,11,'Clear',(3,125),(120,25))
EVT_BUTTON(self,10,self.Count)
EVT_BUTTON(self,11,self.Clear)
def Count(self,event):
cm = self.cm.GetValue()
inch = self.inch.GetValue()
try :
if cm=='' and inch=='':
warning = wxMessageDialog(self,'Cannot process empty string','Error',
wxOK|wxICON_INFORMATION)
warning.ShowModal()
warning.Destroy()
elif cm !='' and inch!='':
warning3 = wxMessageDialog(self,'Enter singe value only','Error',
wxOK|wxICON_INFORMATION)
warning3.ShowModal()
warning3.Destroy()
self.cm.Clear()
self.inch.Clear()
elif inch == '':
inch_1 = float(cm)/2.54
inch_a = round(inch_1,2)
self.inch.WriteText(str(inch_a))
elif cm == '':
cm_1 = float(inch)*2.54
cm_a = round(cm_1,2)
self.cm.WriteText(str(cm_a))
except ValueError:
warning2 = wxMessageDialog(self,'Please enter numerical value(s) only','Error',
wxOK|wxICON_INFORMATION)
warning2.ShowModal()
warning2.Destroy()
self.cm.Clear()
self.inch.Clear()
def Clear(self,event):
self.cm.Clear()
self.inch.Clear()
class okrun(wxApp):
def OnInit(self):
return True
app = okrun(0)
dlg = testdialog(NULL,-1,'MIGHTconv v1(b)',(200,200),(132,180))
dlg.ShowModal()
dlg.Destroy()
|

February 23rd, 2004, 10:07 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Canada
|
|
Note: In order to use this program, you need Python and wxPython.
I can't really offer any advice, because I haven't really used wxPython yet.
Well, lets go through a list of things:
1. The box was successfully made.
2. The labels are there.
3. When I click on 'Count' without any data, I get the correct error message: "Cannot process empty string"
4. When I click on 'Count' with data in both fields, I get the correct error message: "Enter single value only"
5. When I enter letters into one of the fields, I get the correct error message: "Please enter numerical value(s) only"
6. When I click on 'Clear', it deletes all data in both fields.
7. The correct title is in the title bar.
So far, everything is working. The only thing that wasn't quite right, was the math. The math was close, though. Maybe your math is right. Hopefully a mathematician can help us out with this one. If your math is wrong, maybe try using the math module.
Anyways, nice program! 
|

February 24th, 2004, 03:35 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 44
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
My math sucks!!!
|

February 24th, 2004, 10:31 AM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
Pretty good, considering I never achieved anything using wxPython  If its docs were as good as Python's... *sighs*
But, more on topic, your math is OK. 1 inch == 2.54 cm.
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|