pygtk:resizing
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Prossima revisione | Revisione precedente | ||
pygtk:resizing [2009/01/14 00:42] – creata pietro | pygtk:resizing [2009/01/14 09:20] (versione attuale) – set_size_request pietro | ||
---|---|---|---|
Linea 1: | Linea 1: | ||
- | == resize tweaks == | + | ====== resize tweaks |
- | First: the method gtk.Window.resize() doesnt' | + | First: the method gtk.Window.resize() doesnt' |
Now: if a gtk.Window **w** has size (100,100) and we run the following code: | Now: if a gtk.Window **w** has size (100,100) and we run the following code: | ||
Linea 32: | Linea 32: | ||
window.set_data(' | window.set_data(' | ||
| | ||
- | def set_width(window, width): | + | def get_width(window): |
- | | + | |
+ | if not width: | ||
+ | width = window.get_size()[0] | ||
+ | return width | ||
| | ||
- | window.set_data('real width', | + | def get_height(window): |
height = window.get_data(' | height = window.get_data(' | ||
if not height: | if not height: | ||
height = window.get_size()[1] | height = window.get_size()[1] | ||
- | | + | return height |
+ | |||
+ | def set_width(window, | ||
+ | ensure_configure_handler(window) | ||
+ | |||
+ | window.set_data(' | ||
+ | height = get_height(window) | ||
window.resize(width, | window.resize(width, | ||
| | ||
Linea 46: | Linea 55: | ||
| | ||
window.set_data(' | window.set_data(' | ||
- | width = window.get_data('real width' | + | width = get_width(window) |
- | if not width: | + | |
- | width = window.get_size()[0] | + | |
- | + | ||
window.resize(width, | window.resize(width, | ||
+ | ==== set_size_request ==== | ||
+ | Notice that set_size_request has a similar problem: if a gtk.Widget **widget**' | ||
+ | widget.set_size_request(100, | ||
+ | widget.set_size_request(-1, | ||
+ | |||
+ | , it won't expand at all, nothing will happen. This is an issue if you wanted to use this as a hack to resize the widget but let to the user the possibility to shrink it again; an hackish way to solve this is instead the following code: | ||
+ | widget.set_size_request(100, | ||
+ | gobject.idle_add(widget.set_size_request, | ||
+ | |||
+ | ====== gtk.ScrolledWindow ====== | ||
+ | While gtk.Containers usually propagate size requests from child(ren) to parent, gtk.ScrolledWindow doesn' | ||
+ | * we have a widget that should take all the space available, but without overflowing the screen; if space isn't enough, and only in that case, the Scrollbar should appear. Notice that to reach that behaviour, we should also change gtk.Window behaviour so that its child(ren)' | ||
+ | * we have a widget that should take all the space needed (even though not known precisely and then not hardcodeable) at startup, but its toplevel gtk.Window shouldn' | ||
+ | |||
+ | I solved this with the following code (**widget** is the widget we want to resize so that it is fully shown), thanks to //owen// in freenode.irc' | ||
+ | | ||
+ | scrolledwindows = [] | ||
+ | scrolledwindows_policies = [] | ||
+ | scrolledwindows_hscrollbars = [] | ||
+ | | ||
+ | while widget: | ||
+ | # The ScrolledWindows stop propagation of queue_redraws, | ||
+ | # have the scrollbars disabled, so here we disable them... | ||
+ | if gobject.type_is_a(widget, | ||
+ | scrolledwindows.append(widget) | ||
+ | scrolledwindows_policies.append(widget.get_policy()) | ||
+ | widget.set_policy(gtk.POLICY_NEVER, | ||
+ | scrolledwindows_hscrollbars.append(widget.get_hscrollbar()) | ||
+ | widget = widget.get_parent() | ||
+ | | ||
+ | toplevel = treeview.get_toplevel() | ||
+ | | ||
+ | # ... then we can take the size... | ||
+ | | ||
+ | new_width = toplevel.size_request()[0] | ||
+ | | ||
+ | # ... add the size of bars... | ||
+ | bars_width = 0 | ||
+ | for index in range(len(scrolledwindows)): | ||
+ | # (not needed if the policy hides it) | ||
+ | if scrolledwindows_policies[index][0] == gtk.POLICY_ALWAYS: | ||
+ | bars_width += scrolledwindows_hscrollbars[index].size_request()[0] | ||
+ | scrolledwindows[index].set_policy(*scrolledwindows_policies[index]) | ||
+ | # print " | ||
+ | | ||
+ | # ... and put together the total size. | ||
+ | | ||
+ | new_width += bars_width |
pygtk/resizing.1231890128.txt.gz · Ultima modifica: 2009/01/14 00:42 da pietro