On Friday 18 January 2002 20:33, Steve McClure wrote:
> This isn't necessarily Python related but:
>
> I wan't to use a GtkSpinButton to display the port of a service but I
> want to display the text for know services.  e.g. if 79 is displayed
> and they increment, display 'http' instead of 80.  Does anyone know
> if this is possible?  

Not easily. I think this will mean twisting the widget (at least for
gtk 1.2.x). You will need to intercept the value change event and then
try to put your own value without triggering/or ignoring the new
value change :( ...

> ...There is an attribute to allow non-numeric
> input but when i call get_text(), I get back the minimum value. 
> There must be some non-obvious (to me) use for this attribute?

get_text works fine for me. Don't forget that GtkSpinButton is
a subclass of GtkEntry, from which you can use methods/callbacks.


But I don't know what should your widget look like :
  - more than 30000 possibles values 
  - several protocols (tcp, udp).
I would have thought using a combo if you only want to peek valid
values, or have a quick of what exists, but there are more than 400 
available for the tcp protocol.

Skip idea to define a new widget is a good idea (target gtk 1.99 ;).
But I think you will need to tailor it for your needs : should the
user be allowed to type text, what about aliases, should he be able
to have a look at the known protocols ?

You may try the following code to get an idea.
Hoping this help...

---------------------------------------------------------------------
my_try.py
---------------------------------------------------------------------
import gtk
import re


RE_BLANK = re.compile('^(\s*)' '(#.*)?$')
RE_SERVICES = re.compile(
                '^(\s)*' '(?P<name>[0-9a-zA-Z_\-\.\+]+)'
                    '(\s+)' '(?P<port>[0-9]+)' 
                    '(\s*)' '/' 
                    '(\s*)' '(?P<prot>(tcp|udp|ddp|ucp))' 
                    '(?P<alias>((\s)+[0-9a-zA-Z_\-\.\+]+)*)' 
                    '(\s*)' '(#.*)?$'
                    )

f = open('/etc/services', 'r')

servbyport = {}
for l in f.readlines():
    l = l[:-1]
    if RE_BLANK.match(l):
        continue
    m = RE_SERVICES.match(l)
    if m is not None:
        d = m.groupdict()
        prot = d["prot"]
        port = int(d["port"])
        servbyport[(port,prot)] = d["name"]
    else:
        print "UNKNOWN : %s" % l

f.close()


def getservbyport(i, prot):
    return servbyport.get( (i,prot), "Unknown")

def quit(*args):
    gtk.mainquit()

def changed(wdgt, *args):
    i = wdgt.get_value_as_int()
    txt = getservbyport(i, "tcp")
    l.set_text(txt)

w = gtk.GtkWindow()
b = gtk.GtkHBox()
l = gtk.GtkLabel()
s = gtk.GtkSpinButton()

adj = gtk.GtkAdjustment(1, 1, 32000, 1, 10, 10)
s.set_digits(0)
s.set_adjustment(adj)

b.pack_end(s,1,1)
b.pack_end(l,1,1)
w.add(b)

s.connect("changed", changed)

w.connect("destroy_event", quit)
w.connect("delete_event", quit)

w.show_all()

gtk.mainloop()
---------------------------------------------------------------------


-- 
Pedro
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk

Reply via email to