471,330 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

How to use Styles in PyGTK

The GTK includes a class gtk.Style which affects the look of widgets in a nice and efficient way. However, I have found no clues on how to modify the style within the application. I am trying to find a tutorial that explains how to use and modify the gtk.Style objects. There is also a class gtk.RcStyle, and I'm trying to find out how it relates to gtk.Style.

The related problem is as follows:

I am developing an application that will run on a PDA and it requires some special layouts and looks for the widgets. The PDA has a touch sensitive screen and the application is used by pressing on the display with the finger tips. Because of this I need to make some UI elements really big so that they are easy to click on. I also need to make some customization on the default widgets in order to create an optimal UI. In order to save effort I want the widgets to lay out and respond to interaction in the default way, but I want to entirely control the drawing of the widgets in each of their states. I want to write the entire application in Python.

The question is:

I want to make a vertical scrollbar 40 pixels wide, so that it is easy to use with the finger tips. I find an attribute named "slider-width" listed in gtk.Range Style Properties in the GTK class reference. However, in the API reference I find no clue whatsoever on how to manipulate that value, or even in which construct it is stored in or how to read it. I need to know 1) how I can read the "slider-width" property of a gtk.VScrollbar and 2) how can I change the value.
Sep 19 '07 #1
3 8821
6,596 Expert 4TB
If the use of PyGTK.Style is not clear from here, I may be able to help a bit (it's not too different from other frameworks). But that may not be what you are looking for.

In wxPython, we are allowed to subclass most wrapped objects (and that's probably true of PyGTK, as well). In that case, the underlying API function that sets the width (or other attribute) may be overridden. I'm looking into a simple way to set the width of a vertical scrollbar.

It looks like the scrollbar is descended from Range() so that you may call Range.method() (or Adjustment.method()) and attributes directly on the scrollbar object. For example:
Expand|Select|Wrap|Line Numbers
  1. myVScrollBar.adjustment.lower = 0
I hope that's not too confusing. GUI programming is really just advanced OOP and GTK doesn't seem to be the most well planned implementation.
Sep 19 '07 #2
6,596 Expert 4TB
I'm looking into a simple way to set the width of a vertical scrollbar.
All GTK widgets have a standard set of "properties". As in:
Expand|Select|Wrap|Line Numbers
  1. "width-request"    Read-Write    The width request of the widget, or -1 if natural request should be used.
These are probably set in the creation method and might be accessed as normal attributes. It is hard to tell looking at the manual, but I'm sure it is covered is any beginning GTK book or tutorial.

I remember that Tkinter uses
Expand|Select|Wrap|Line Numbers
  1. myWidget.config("BG"=RED)
for example.
That interface was also written with access methods that allowed
Expand|Select|Wrap|Line Numbers
  1. myWidget["BG"] = RED
as I recall.
Sep 19 '07 #3
I had a similar problem with developing for a PDA-style device as well, and scoured the docs for a few days to come up with the answer:

Unfortunately, the answer is not as easy as you would like, but it is doable.
1) how I can read the "slider-width" property of a gtk.VScrollbar
This is pretty easy, just do
Expand|Select|Wrap|Line Numbers
  1. v_scrollbar.style_get_property("slider-width")
2) how can I change the value.
Not so easy. Since slider-width is a read-only style property (look in the docs at pygtk.org, it specifies), you have to specify it through a gtk.RcStyle. The way to do that is, either via a file or string, write out a resource file which changes the default style of the widgets in a particular way. This is rather complicated, and I don't really completely understand it, but the documentation contains an ok, if not very complete, tutorial on how to do this nestled away here:


Reading this little three-part tutorial, with example, should give you a general idea of how rc files are written. I would also recommend reading, after that, both the pygtk and gtk references for the RcStyle class:

http://www.pygtk.org/docs/pygtk/class-gtkrcstyle.html (pygtk ref)
http://library.gnome.org/devel/gtk/u...rce-Files.html (gtk ref)

The gtk version of the documentation has a better explanation of what exactly an rc style does, while the pygtk version really is mostly useful so you know all the function calls.

What will get you the behavior you want is this in your rc file:
Expand|Select|Wrap|Line Numbers
  1. style "wide_range"
  2. {
  3.     GtkRange::slider-width = 40
  4. }
  6. class "GtkRange" style "wide_range"
and this in your code, in an appropriate init statement for your whole gui:
Expand|Select|Wrap|Line Numbers
  1. gtk.rc_parse(your_filename_here.rc)
(tested in gtk 2.12 on a windows XP machine)

What this will do, specifically, is change the slider width of all instances of gtk.Range or it's subclasses (of which VScrollbar is one). If you read the references above, you should be able to figure out how to make that more specific if desired, it involves using either "widget" or "widget_class" where "class" is in the resource file described above.

From what I read in the gtk reference, it seems like the difference between an RcStyle and a Style is that the RcStyle class, in addition to being created by this funky notation, is only a partial specification of a style (for instance, just the slider width). Gtk combines all the RcStyles applied to a class via the RcStyle files into one nice happy Style object which everything gets.

It's worth noting that it is possible that your device has a theme which somehow overwrites your changes. I don't know too much about that, unfortunately, but if it doesn't work right away on the device check if it works on a desktop as well.

One last note: class names in resource files should follow the gtk class naming convention, NOT the pygtk class naming convention:
(ex. GtkRange in lieu of gtk.Range). It should be pretty easy to figure out after that.
Aug 14 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Edwin Young | last post: by
4 posts views Thread by j_mckitrick | last post: by
6 posts views Thread by Mark Mitchell | last post: by
5 posts views Thread by Egbert Bouwman | last post: by
1 post views Thread by Thomas Bartkus | last post: by
14 posts views Thread by Rod W | last post: by
1 post views Thread by krishnakant Mane | last post: by
reply views Thread by He Jibo | last post: by
reply views Thread by Michael Palmer | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.