Gtk.ScrolledWindow
Gtk.ScrolledWindow — Adds scrollbars to its child widget
Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Container
╰── Gtk.Bin
╰── Gtk.ScrolledWindow
╰── Gtk.PlacesSidebar
See also:
Gtk.Scrollable, Gtk.Viewport, Gtk.Adjustment
Functions:
- new
(hadjustment:Gtk.Adjustment=None, vadjustment:Gtk.Adjustment=None) -> Gtk.Widget
- get_hadjustment
(self) -> Gtk.Adjustment
- get_vadjustment
(self) -> Gtk.Adjustment
- get_hscrollbar
(self) -> Gtk.Widget
- get_vscrollbar
(self) -> Gtk.Widget
- set_policy
(self, hscrollbar_policy:Gtk.PolicyType, vscrollbar_policy:Gtk.PolicyType)
- add_with_viewport
(self, child:Gtk.Widget)
- set_placement
(self, window_placement:Gtk.CornerType)
- unset_placement
(self)
- set_shadow_type
(self, type:Gtk.ShadowType)
- set_hadjustment
(self, hadjustment:Gtk.Adjustment)
- set_vadjustment
(self, vadjustment:Gtk.Adjustment)
- get_placement
(self) -> Gtk.CornerType
- get_policy
(self) -> hscrollbar_policy:Gtk.PolicyType, vscrollbar_policy:Gtk.PolicyType
- get_shadow_type
(self) -> Gtk.ShadowType
- get_min_content_width
(self) -> int
- set_min_content_width
(self, width:int)
- get_min_content_height
(self) -> int
- set_min_content_height
(self, height:int)
- set_kinetic_scrolling
(self, kinetic_scrolling:bool)
- get_kinetic_scrolling
(self) -> bool
- set_capture_button_press
(self, capture_button_press:bool)
- get_capture_button_press
(self) -> bool
- set_overlay_scrolling
(self, overlay_scrolling:bool)
- get_overlay_scrolling
(self) -> bool
Signals:
- “edge-overshot”
(scrolled_window, pos, user_data)
- “edge-reached”
(scrolled_window, pos, user_data)
- “move-focus-out”
(scrolled_window, direction_type, user_data)
Description:
Gtk.ScrolledWindow
is a GtkBin subclass: it’s a container the accepts a single child widget. Gtk.ScrolledWindow
adds scrollbars to the child widget and optionally draws a beveled frame around the child widget.
The scrolled window can work in two ways. Some widgets have native scrolling support; these widgets implement the Gtk.Scrollable interface.
Widgets with native scroll support include Gtk.TreeView, GtkTextView, and Gtk.Layout.
For widgets that lack native scrolling support, the Gtk.Viewport widget acts as an adaptor class, implementing scrollability for child widgets that lack their own scrolling capabilities. Use Gtk.Viewport to scroll child widgets such as Gtk.Grid, GtkBox, and so on.
If a widget has native scrolling abilities, it can be added to the Gtk.ScrolledWindow
with Gtk.Container::add()
. If a widget does not, you must first add the widget to a Gtk.Viewport, then add the Gtk.Viewport to the scrolled window. Gtk.Container::add()
will do this for you for widgets that don’t implement Gtk.Scrollable natively, so you can ignore the presence of the viewport.
The position of the scrollbars is controlled by the scroll adjustments.
See Gtk.Adjustment for the fields in an adjustment — for Gtk.Scrollbar, used by Gtk.ScrolledWindow
, the value
field represents the position of the scrollbar, which must be between the lower
field and upper - page_size.
The page_size
field represents the size of the visible scrollable area. The step_increment
and page_increment
fields are used when the user asks to step down (using the small stepper arrows) or page down (using for example the PageDown key).
If a Gtk.ScrolledWindow
doesn’t behave quite as you would like, or doesn’t have exactly the right layout, it’s very possible to set up your own scrolling with Gtk.Scrollbar and for example a Gtk.Grid.
Touch support
Gtk.ScrolledWindow
has built-in support for touch devices. When a touchscreen is used, swiping will move the scrolled window, and will expose 'kinetic' behavior. This can be turned off with the Gtk.ScrolledWindow
also displays visual 'overshoot' indication when the content is pulled beyond the end, and this situation can be captured with the Gtk.ScrolledWindow::edge
-overshot signal.
If no mouse device is present, the scrollbars will overlayed as narrow, auto-hiding indicators over the content. If traditional scrollbars are desired although no mouse is present, this behaviour can be turned off with the Gtk.ScrolledWindow
:overlay-scrolling property.
Function Details:
new()
new (hadjustment:Gtk.Adjustment=None, vadjustment:Gtk.Adjustment=None) -> Gtk.Widget
Creates a new scrolled window.
The two arguments are the scrolled window’s adjustments; these will beshared with the scrollbars and the child widget to keep the bars in sync with the child. Usually you want to pass None
for the adjustments, which will cause the scrolled window to create them for you.
- Returns: a new scrolled window
get_hadjustment()
get_hadjustment (self) -> Gtk.Adjustment
Returns the horizontal scrollbar’s adjustment, used to connect thehorizontal scrollbar to the child widget’s horizontal scrollfunctionality.
- Returns:
the horizontal
Gtk.Adjustment
.
get_vadjustment()
get_vadjustment (self) -> Gtk.Adjustment
Returns the vertical scrollbar’s adjustment, used to connect thevertical scrollbar to the child widget’s vertical scroll functionality.
- Returns:
the vertical
Gtk.Adjustment
.
get_hscrollbar()
get_hscrollbar (self) -> Gtk.Widget
Returns the horizontal scrollbar of scrolled_window
.
Returns: the horizontal scrollbar of the scrolled window.
Since: 2.8
get_vscrollbar()
get_vscrollbar (self) -> Gtk.Widget
Returns the vertical scrollbar of scrolled_window
.
Returns: the vertical scrollbar of the scrolled window.
Since: 2.8
set_policy()
set_policy (self, hscrollbar_policy:Gtk.PolicyType, vscrollbar_policy:Gtk.PolicyType)
Sets the scrollbar policy for the horizontal and vertical scrollbars.
The policy determines when the scrollbar should appear; it is a valuefrom the Gtk.PolicyType
enumeration. If GTK_POLICY_ALWAYS
, thescrollbar is always present; if GTK_POLICY_NEVER
, the scrollbar isnever present; if GTK_POLICY_AUTOMATIC
, the scrollbar is present onlyif needed (that is, if the slider part of the bar would be smallerthan the trough — the display is larger than the page size).
add_with_viewport()
add_with_viewport (self, child:Gtk.Widget)
Gtk.ScrolledWindow:add_with_viewport
has been deprecated since version 3.8 and should not be used in newly-written code.
Gtk.Container:add
() will automatically adda Gtk.Viewport
if the child doesn’t implement Gtk.Scrollable
.
Used to add children without native scrolling capabilities. Thisis simply a convenience function; it is equivalent to adding theunscrollable child to a viewport, then adding the viewport to thescrolled window. If a child has native scrolling, use`Gtk.Container:add
()instead of this function.
The viewport scrolls the child by moving its GdkWindow, and takesthe size of the child to be the size of its toplevel GdkWindow. This will be very wrong for most widgets that support native scrolling;for example, if you add a widget such as
Gtk.TreeViewwith a viewport,the whole widget will scroll, including the column headings. Thus, widgets with native scrolling support should not be used with the
Gtk.Viewportproxy.
A widget supports scrolling natively if it implements the
Gtk.Scrollable` interface.
set_placement()
set_placement (self, window_placement:Gtk.CornerType)
Sets the placement of the contents with respect to the scrollbarsfor the scrolled window.
The default is GTK_CORNER_TOP_LEFT
, meaning the child isin the top left, with the scrollbars underneath and to the right.Other values in Gtk.CornerType
are GTK_CORNER_TOP_RIGHT
,GTK_CORNER_BOTTOM_LEFT
, and GTK_CORNER_BOTTOM_RIGHT
.
See also Gtk.ScrolledWindow:get_placement()
andGtk.ScrolledWindow:unset_placement()
.
unset_placement()
unset_placement (self)
Unsets the placement of the contents with respect to the scrollbarsfor the scrolled window. If no window placement is set for a scrolledwindow, it defaults to GTK_CORNER_TOP_LEFT
.
See also Gtk.ScrolledWindow:set_placement()
andGtk.ScrolledWindow:get_placement()
.
- Since: 2.10
set_shadow_type()
set_shadow_type (self, type:Gtk.ShadowType)
Changes the type of shadow drawn around the contents ofscrolled_window
.
set_hadjustment()
set_hadjustment (self, hadjustment:Gtk.Adjustment)
Sets the Gtk.Adjustment
for the horizontal scrollbar.
set_vadjustment()
set_vadjustment (self, vadjustment:Gtk.Adjustment)
Sets the Gtk.Adjustment
for the vertical scrollbar.
get_placement()
get_placement (self) -> Gtk.CornerType
Gets the placement of the contents with respect to the scrollbarsfor the scrolled window. See Gtk.ScrolledWindow:set_placement()
.
- Returns: the current placement value.
get_policy()
get_policy (self) -> hscrollbar_policy:Gtk.PolicyType, vscrollbar_policy:Gtk.PolicyType
Retrieves the current policy values for the horizontal and verticalscrollbars. See Gtk.ScrolledWindow:set_policy()
.
get_shadow_type()
get_shadow_type (self) -> Gtk.ShadowType
Gets the shadow type of the scrolled window. See Gtk.ScrolledWindow:set_shadow_type()
.
- Returns: the current shadow type
get_min_content_width()
get_min_content_width (self) -> int
Gets the minimum content width of scrolled_window
, or -1 if not set.
Returns: the minimum content width
Since: 3.0
set_min_content_width()
set_min_content_width (self, width:int)
Sets the minimum width that scrolled_window
should keep visible.Note that this can and (usually will) be smaller than the minimumsize of the content.
- Since: 3.0
get_min_content_height()
get_min_content_height (self) -> int
Gets the minimal content height of scrolled_window
, or -1 if not set.
Returns: the minimal content height
Since: 3.0
set_min_content_height()
set_min_content_height (self, height:int)
Sets the minimum height that scrolled_window
should keep visible.Note that this can and (usually will) be smaller than the minimumsize of the content.
- Since: 3.0
set_kinetic_scrolling()
set_kinetic_scrolling (self, kinetic_scrolling:bool)
Turns kinetic scrolling on or off.Kinetic scrolling only applies to devices with sourceGDK_SOURCE_TOUCHSCREEN
.
- Since: 3.4
get_kinetic_scrolling()
get_kinetic_scrolling (self) -> bool
Returns the specified kinetic scrolling behavior.
Returns: the scrolling behavior flags.
Since: 3.4
set_capture_button_press()
set_capture_button_press (self, capture_button_press:bool)
Changes the behaviour of scrolled_window
wrt. to the initialevent that possibly starts kinetic scrolling. When capture_button_press
is set to True
, the event is captured by the scrolled window, andthen later replayed if it is meant to go to the child widget.
This should be enabled if any child widgets perform non-reversibleactions on “button-press-event”. If they don't, and handleadditionally handle “grab-broken-event”, it might be betterto set capture_button_press
to FALSE
.
This setting only has an effect if kinetic scrolling is enabled.
- Since: 3.4
get_capture_button_press()
get_capture_button_press (self) -> bool
Return whether button presses are captured during kineticscrolling. See Gtk.ScrolledWindow:set_capture_button_press()
.
Returns:
True
if button presses are captured during kinetic scrollingSince: 3.4
set_overlay_scrolling()
set_overlay_scrolling (self, overlay_scrolling:bool)
Enables or disables overlay scrolling for this scrolled window.
- Since: 3.16
get_overlay_scrolling()
get_overlay_scrolling (self) -> bool
Returns whether overlay scrolling is enabled for this scrolled window.
Returns:
True
if overlay scrolling is enabledSince: 3.16
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ScrolledWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_title("ScrolledWindow")
self.set_default_size(200, 200)
self.connect("destroy", Gtk.main_quit)
scrolledwindow = Gtk.ScrolledWindow()
self.add(scrolledwindow)
layout = Gtk.Layout()
layout.set_size(800, 600)
layout.set_vexpand(True)
layout.set_hexpand(True)
scrolledwindow.add(layout)
hadjustment = layout.get_hadjustment()
scrolledwindow.set_hadjustment(hadjustment)
vadjustment = layout.get_vadjustment()
scrolledwindow.set_vadjustment(vadjustment)
button = Gtk.Button(label="Button 1")
layout.put(button, 645, 140)
button = Gtk.Button(label="Button 2")
layout.put(button, 130, 225)
button = Gtk.Button(label="Button 3")
layout.put(button, 680, 350)
window = ScrolledWindow()
window.show_all()
Gtk.main()