[Tutor] More OOP
Tim Ronning
tim.ronning at start.no
Mon Nov 10 13:47:28 EST 2003
På Fri, 7 Nov 2003 19:42:43 -0000, skrev Alan Gauld
<alan.gauld at blueyonder.co.uk>:
> Its not necessary but there is a natural synergy between GUI widgets
> and objects. Also many GUI toolkits are implemented as object
> frameworks
> so you have to use objects anyway, you might as well make your code
> fit in.
>
The attached code is a new little learning project of mine. And it somewhat
ilustrates what I meant with "learning GUI" straight away. I think the code
(albeit small) is fairly OO (correct me if I'm wrong). And it felt natural
somehow to devide things into functions and a class and work with
attributes of an object(instance). I know that if I had done this without a
GUI I would have gone with batch style (the old autoexec.bat is haunting
me!) So to the question, is this more difficult to learn? Or is it just
moore to learn? I think, if you have good tools like
QTDesigner+Eric/Blackadder or wxPython+wxGlade or pyfltk+flconvert, one can
go GUI quite soon in ones learningprocess without overloading oneself with
new stuff to learn. It took me 5 minutes to generate the FLTK GUI code in
this script, and going through it, line by line, it's fairly easy to
understand what each line do, even for a novice like myself. The concepts
behind the core Python lines are a bit more difficult to learn, but for me
personally I think the GUI portion helps me understand the Python objects
because it introduces the concepts of object attributes(methodes). And
somehow these attributes makes more sense when I can fixate them to visible
widgets. Maybe it's just me?
Anyway, here's the Python/FLTK code for a simple product/VAT calculator.
Best regards
Tim R.
-------------------------------------------------------
#!/usr/bin/env python
# -*-coding: iso-8859-1 -*-
from fltk import *
import sys
# global object names
window_main = None # type 'Fl_Window' from 'make_window()'
gross_pris = None # type 'Fl_Value_Input' from 'make_window()'
outp_umoms = None # type 'Fl_Value_Output' from 'make_window()'
outp_mmoms = None # type 'Fl_Value_Output' from 'make_window()'
outp_inntj = None # type 'Fl_Value_Output' from 'make_window()'
outp_skyldig = None # type 'Fl_Value_Output' from 'make_window()'
kalk_knapp = None # type 'Fl_Button' from 'make_window()'
logo = None # type 'Fl_Box' from 'make_window()'
pris_u_moms = 0
pris_m_moms = 0
inntjening = 0
skyldig_moms = 0
verdi = 0
def createobj(ptr):
global pris_u_moms, pris_m_moms, inntjening, skyldig_moms
i = Productcalc(verdi)
pris_u_moms = i.umoms()
pris_m_moms = i.mmoms()
inntjening = i.inntj()
skyldig_moms = i.skyldig()
outp_umoms.value(pris_u_moms)
outp_mmoms.value(pris_m_moms)
outp_inntj.value(inntjening)
outp_skyldig.value(skyldig_moms)
def getValue(ptr):
global verdi
valuator = castWidget2Valuator(ptr)
verdi = valuator.value()
return verdi
def make_window():
global window_main
global gross_pris
global outp_umoms
global outp_mmoms
global outp_inntj
global outp_skyldig
global kalk_knapp
global logo
window_main = Fl_Double_Window(404, 471, 308, 235,
"""Productcalculations""")
gross_pris = Fl_Value_Input(100, 40, 90, 25, """Gross. price""")
gross_pris.callback(getValue)
gross_pris.label('''Gross. price''')
outp_umoms = Fl_Value_Output(100, 75, 90, 25, """Price ex. vat""")
outp_umoms.label('''Price ex. vat''')
outp_umoms.value(pris_u_moms)
outp_mmoms = Fl_Value_Output(100, 110, 90, 25, """Customer""")
outp_mmoms.label('''Customer''')
outp_mmoms.value(pris_m_moms)
outp_inntj = Fl_Value_Output(100, 145, 90, 25, """Profit""")
outp_inntj.label('''Profit''')
outp_inntj.value(inntjening)
outp_skyldig = Fl_Value_Output(100, 180, 90, 25, """VAT to pay""")
outp_skyldig.label('''VAT to pay''')
outp_skyldig.value(skyldig_moms)
kalk_knapp = Fl_Button(205, 40, 90, 25, """Calculate""")
kalk_knapp.labelsize(12)
kalk_knapp.labelfont(1)
kalk_knapp.label('''Calculate''')
kalk_knapp.callback(createobj)
logo = Fl_Box(50, 5, 215, 25, """Productcalculations v. 0.1.0""")
logo.box(FL_SHADOW_FRAME)
logo.labelfont(9)
logo.label('''Productcalculations v. 0.1.0''')
window_main.box(FL_PLASTIC_DOWN_BOX)
window_main.color(53)
window_main.labeltype(FL_NORMAL_LABEL)
window_main.label('''Productcalculations''')
window_main.end()
return window_main
class Productcalc:
def __init__(self, g_pris):
self.g_pris = g_pris
def umoms(self):
return self.g_pris+(self.g_pris*0.35)
def mmoms(self):
return (self.g_pris+(self.g_pris*0.35))*1.24
def inntj(self):
return (self.g_pris*0.24)+((self.g_pris+(self.g_pris*0.35))-
(self.g_pris+(self.g_pris*0.24)))
def skyldig(self):
return (((self.g_pris+(self.g_pris*0.35))*1.24)-
(self.g_pris+(self.g_pris*0.35)))-(self.g_pris*0.24)
if __name__=='__main__':
window = make_window()
window.show(len(sys.argv), sys.argv)
Fl.run()
-- _______________________________________
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
More information about the Tutor
mailing list