My examples must have been overly simplistic, since I don't even know what TkReactor 
is.

Truthfully, I don't know what the issues are as to what should be problematic about it 
so
maybe I haven't really done it after all :-) However, I do have code that uses fnorb to
make CORBA calls to a server and display that data using pygtk.

I've attached an example of client code that I wrote. This code is connecting to
an Orbix Server that is written in C++. Maybe there's something that I should be
doing that I'm not. Note that this isn't production quality code - only proof of 
concept
so I'm sure there are bugs. The app is getting and displaying time series data for
fixed income securities. All the fnorb stuff is in Hist.py. The main gtk code is in 
T1b.py.

I'd be interested in better ways to accomplish what I'm doing here.

--
Bill Allen


#!/usr/bin/env python

# Fnorb modules.
from Fnorb.orb import CORBA, BOA

# Stubs generated by 'fnidl'.
import PA

class Hist:

    def __init__(self, orb):

        self.orb = orb
    
        stringified_ior = open('SecurityHistServer.ior').read()
        self.server = self.orb.string_to_object(stringified_ior)
        
        if self.server is None:
            raise 'Nil object reference!'

    def getAttributes(self):
        return self.server.availableAttributes()

    def getData(self, fid, attrib, start, end):
        dvpl = self.server.getHistData(fid, attrib, start, end, PA.Daily)
        return dvpl
#!/usr/bin/env python

import sys
from string import atol

from gtk import *
import GtkExtra

from PADate import *

from Hist import *

class HistDisplay(GtkWindow):
    def __init__(self, hist, quitfunc='hide'):
        GtkWindow.__init__(self, WINDOW_TOPLEVEL)
        self.hist = hist
        self.quitfunc = quitfunc
        self.clist = GtkCList(2, ["Date", "Value"])
        def quit(win, event=None, _self=self):
            if (_self.quitfunc == 'quit'):
                mainquit()
            else:
                _self.hide()
                
        self.connect("destroy", quit)
        self.connect("delete_event", quit)
        self.set_title("clist")
        self.set_usize(425, 425)

        box1 = GtkVBox()
        self.add(box1)
        box1.show()

        box2 = GtkHBox(spacing=10)
        box2.set_border_width(10)
        box1.pack_start(box2, expand=FALSE, fill=FALSE)
        box2.show()

        self.clist.set_column_width(0, 100)
        for i in range(1, 2):
            self.clist.set_column_width(i, 80)


        s = [0]
        
        def select_clist(_clist, r, c, event, selected=s):
            selected[0] = r

        button = GtkButton("Go")
        box2.pack_start(button)

        def do_clist(_button, clist=button):
            self = clist.my_self
            fid = self.entry.get_text()
            self.dohist(fid)

        button.connect("clicked", do_clist)
        button.my_self = self
        button.show()

        self.entry = GtkEntry()
        self.entry.set_text("")
        box2.pack_start(self.entry)
        self.entry.show()

        self.attribMenu = GtkMenu()
        group = None
        attributes = hist.getAttributes()
        for item in attributes:
            menuitem = GtkRadioMenuItem(group, item)
            menuitem.set_data("Attribute", item)
            self.attribMenu.append(menuitem)
            menuitem.show()

        optionmenu = GtkOptionMenu()
        optionmenu.set_menu(self.attribMenu)
        box2.pack_start(optionmenu)
        optionmenu.show()
        

        box2 = GtkHBox(spacing=10)
        box2.set_border_width(10)
        box1.pack_start(box2, expand=FALSE, fill=FALSE)
        box2.show()

        self.startdate = GtkEntry()
        self.startdate.set_text("19990101")
        box2.pack_start(self.startdate)
        self.startdate.show()

        button = GtkButton("s")
        box2.pack_start(button)

        def do_s(_button, clist=button):
            self = clist.my_self
            d = get_calendar_date(atol(self.startdate.get_text()))
            self.startdate.set_text('%ld' % d)

        button.connect("clicked", do_s)
        button.my_self = self
        button.show()

        self.enddate = GtkEntry()
        self.enddate.set_text("19990331")
        box2.pack_start(self.enddate)
        self.enddate.show()

        button = GtkButton("e")
        box2.pack_start(button)

        def do_e(_button, clist=button):
            self = clist.my_self
            d = get_calendar_date(atol(self.enddate.get_text()))
            self.enddate.set_text('%ld' % d)

        button.connect("clicked", do_e)
        button.my_self = self
        button.show()


        box2 = GtkVBox(spacing=10)
        box2.set_border_width(10)
        box1.pack_start(box2)
        box2.show()

        self.clist.set_row_height(20)
        self.clist.connect("select_row", select_clist)
        self.clist.set_column_width(0, 100)

        for i in range(1, 2):
            self.clist.set_column_width(0, 80)

        self.clist.set_selection_mode(SELECTION_BROWSE)
        self.clist.set_column_justification(1, JUSTIFY_RIGHT)
        self.clist.set_column_justification(2, JUSTIFY_CENTER)

        self.clist.set_border_width(5)

        self.curve = GtkCurve()
        box2.pack_start(self.curve)
        self.curve.set_sensitive(FALSE)
        self.curve.show()

        swin = GtkScrolledWindow()
        swin.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
        box2.pack_start(swin)
        swin.show()

        swin.add(self.clist)
        self.clist.show()

        separator = GtkHSeparator()
        box1.pack_start(separator, expand=FALSE)
        separator.show()

        box2 = GtkVBox(spacing=10)
        box2.set_border_width(10)
        box1.pack_start(box2, expand=FALSE)
        box2.show()

        button = GtkButton("close")
        button.connect("clicked", quit)
        box2.pack_start(button)
        button.set_flags(CAN_DEFAULT)
        button.grab_default()
        button.show()
        #self.show()

    def quit(self):
        self.hide()

    def dohist(self, fid):
        self.clist.clear()
        self.entry.set_text(fid)
        attrib = self.attribMenu.get_active().get_data("Attribute")
        start = atol(self.startdate.get_text())
        end = atol(self.enddate.get_text())
        dvpl = self.hist.getData(fid, attrib, start, end)
        text = map(lambda i: "Column "+str(i), range(2))
        vector = [];
        miny = 999999
        maxy = 0
        for i in range(len(dvpl)):
            text[0] = str(dvpl[i].date)
            val = dvpl[i].value
            text[1] = '%6.2f' % val
            self.clist.append(text)
            vector.append(val)
            if miny > val: miny = val
            if maxy < val: maxy = val

        self.curve.set_range(0, len(vector), miny, maxy)
        self.curve.set_vector(vector)

        
def main(args):
    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
    hist = Hist(orb)
    histDisplay = HistDisplay(hist)
    histDisplay.dohist("C912810EJ")
    histDisplay.show()
    mainloop()

    return 0

#############################################################################

if __name__ == '__main__':
    # Do it!
    sys.exit(main(sys.argv))

#############################################################################
#!/usr/bin/env python

from gtk import *
from time import sleep

def get_calendar_date(initialdate):
    window = GtkWindow(WINDOW_TOPLEVEL)
    window.connect("destroy", mainquit)
    window.set_border_width(10)

    box2 = GtkVBox(spacing=10)
    box2.set_border_width(10)
    window.add(box2)
    box2.show()

    cal = GtkCalendar()
    y = initialdate / 10000
    m = (initialdate % 10000) / 100 - 1
    d = initialdate % 100
    cal.select_month(m, y)
    cal.select_day(d)
    box2.pack_start(cal)
    cal.show()

    cal.waiting = TRUE

    def done(button, c = cal):
        c.waiting = FALSE
    

    button = GtkButton("OK")
    button.connect("clicked", done)
    box2.pack_start(button)
    button.show()
        
    window.show()

    while cal.waiting:
        mainiteration(FALSE)
            
    d = cal.get_date()
    window.hide()
    return d[0] * 10000 + (d[1] + 1) * 100 + d[2]




Reply via email to