[Tutor] Help me make it look pretty!
Kent Johnson
kent37 at tds.net
Fri Jul 21 12:35:14 CEST 2006
John CORRY wrote:
>
> Good evening all.
>
>
>
> I am writing a program using python 2.4, glade 2 and pygtk. It takes
> input from the user using textentry boxes. The input should be a
> number. When the user keys the data in, it automatically left
> justifies. The function below, takes the input for four boxes and
> right justifies it by using an ugly, string format. The boxes are 45
> characters big, so I use the len function to fit them into the box.
> The code is below.
>
I'm not familiar with GTK but I would look for a way to set the
alignment of the text field so it always right aligns.
gtk-entry-set-alignment() looks promising:
http://www.gtk.org/api/2.6/gtk/GtkEntry.html#gtk-entry-set-alignment
More notes below
>
>
>
> I am looking for a way to set the text entry up, so that there is a
> decimal point in the box regardless of what the user does and I am
> also looking for a way to keep the numbers right justified.
>
>
>
> Any suggestions or comments as always are greatly appreciated.
>
>
>
> Regards,
>
>
>
> John.
>
>
>
>
>
> def callback36(self,data,text37,text38,text39,text40,text41,text42,label):
>
> a = text37.get_text()
>
>
>
> b = text38.get_text()
>
> c = text39.get_text()
>
> d = text40.get_text()
>
> a= float(a)
>
> b= float(b)
>
> c= float(c)
>
> d= float(d)
>
>
>
> try:
>
>
>
> e = float(a + b + c + d)
>
> g = e/a
>
> e = "%0.2f" % e
>
>
>
> g = "%0.2f" % g
>
> g = str(g)
>
> label.hide()
>
> e = " %s" % e
>
> a = " %s" % a
>
> b = " %s" % b
>
> c = " %s" % c
>
> d = " %s" % d
>
> g = "%s%%" % g
>
String formatting can create the strings you want directly. str.rjust()
is also useful here. For example
In [1]: e=1./3
In [3]: '% 20.2f' % e
Out[3]: ' 0.33'
In [4]: '0.33'.rjust(20)
Out[4]: ' 0.33'
> text42.set_text(str(g))
>
> if len(e)>45:
>
> x = len(e) - 45
>
> x = x + 4
>
> y = e[x:]
>
> text41.set_text(str(y))
>
> return
>
I think this is taking the last 41 chars of e if len(e) > 45. You can
get the last 41 chars of e directly with
e[-41:]
Kent
>
> else:
>
> text41.set_text(str(e))
>
> return
>
>
>
> except:
>
> label.show()
>
> return
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list