我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用gi.repository.Gtk.Viewport()。
def __init__(self, label_text=""): Gtk.Viewport.__init__(self) self.spinner = Gtk.Spinner() self.spinner.set_size_request(48, 48) # use a table for the spinner (otherwise the spinner is massive!) spinner_table = Gtk.Table(3, 3, False) self.spinner_label = Gtk.Label() self.spinner_label.set_markup('<big>%s</big>' % label_text) spinner_vbox = Gtk.VBox() spinner_vbox.pack_start(self.spinner, True, True, 0) spinner_vbox.pack_start(self.spinner_label, True, True, 10) spinner_table.attach(spinner_vbox, 1, 2, 1, 2, Gtk.AttachOptions.EXPAND, Gtk.AttachOptions.EXPAND) #~ self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(1.0, 1.0, 1.0)) self.add(spinner_table) self.set_shadow_type(Gtk.ShadowType.NONE)
def add_nonwrapped_text(self, text): self._make_primary_bold() self._parsep() sc = Gtk.ScrolledWindow() l = Gtk.Label() l.set_markup('<span font_family="monospace">%s</span>' % escape(text)) l.set_line_wrap(False) l.set_alignment(0.0, 0.5) l.show() sc.add_with_viewport(l) self.msg_vbox.pack_start(sc, True, True, 0) sc.show_all() sc.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER) h = l.get_ancestor(Gtk.Viewport).size_request().height max_lines = 5 lines = text.count("\n") + 1 # This is the space the horizontal scrollbar takes scrollbar_height = (sc.get_hscrollbar().size_request().height + sc.style_get_property("scrollbar-spacing")) if lines <= max_lines: # make the scrollwin so high that it can display all lines sc.set_size_request(-1, h) else: sc.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) sc.set_size_request(-1, int(h * max_lines / lines)) adj = sc.get_hscrollbar().get_adjustment() def f(scrollbar, event): sc.set_size_request(-1, int(h * max_lines / lines) + scrollbar_height) scrollbar.disconnect(self._hscroll_expose_id) # f is called if the horizontal scrollbar is visible. This because we # need to allocate space for the scrollbar too. self._hscroll_expose_id = sc.get_hscrollbar().connect('expose-event', f)
def __init__(self): Gtk.Viewport.__init__(self) self.connect("key-press-event", self.on_key_press_event) return
def set_scroll_position(widget, value, direction='vertical'): viewport = widget.get_ancestor(Gtk.Viewport) if direction == 'vertical': viewport.get_vadjustment().set_value(value) if direction == 'horizontal': viewport.get_hadjustment().set_value(value)
def scrollItemIntoView( self, widget, event = None ): viewport = widget.get_parent() while not isinstance( viewport, Gtk.Viewport ): if not viewport.get_parent(): return viewport = viewport.get_parent() aloc = widget.get_allocation() viewport.get_vadjustment().clamp_page(aloc.y, aloc.y + aloc.height)
def __init__( self, iconName, iconSize, labels = None, buttonWidth = 200, buttonHeight = 38 ): GObject.GObject.__init__( self ) self.connections = [ ] self.iconName = iconName self.iconSize = iconSize self.showIcon = True self.set_relief( Gtk.ReliefStyle.NONE ) self.set_size_request( buttonWidth, buttonHeight ) self.viewport = Gtk.Viewport() self.Align1 = Gtk.Alignment.new( 0, 0.5, 1.0, 0 ) self.HBox1 = Gtk.HBox() self.labelBox = Gtk.VBox( False, 2 ) self.buttonImage = Gtk.Image() icon = self.getIcon( self.iconSize ) if icon: self.buttonImage = icon else: #[ iW, iH ] = iconManager.getIconSize( self.iconSize ) self.buttonImage.set_size_request( self.iconSize, self.iconSize ) self.image_box = Gtk.HBox() self.image_box.pack_start(self.buttonImage, False, False, 5) self.image_box.show_all() self.HBox1.pack_start( self.image_box, False, False, 0 ) if labels: for label in labels: if isinstance( label, str ): self.addLabel( label ) elif isinstance( label, list ): self.addLabel( label[0], label[1] ) self.labelBox.show() self.HBox1.pack_start( self.labelBox , True, True, 0) #add right icon for category self.buttonIconRight = Gtk.Image() iconfile = ICON_PATH + "arrow-right-line.png" pixbuf = Pixbuf.new_from_file(iconfile) pixbuf = pixbuf.scale_simple(6, 9, 2) #2 := BILINEAR self.buttonIconRight.set_from_pixbuf(pixbuf) #self.buttonIconRight.set_from_icon_name(ICON_PATH + "arrow-right-line.png", self.iconSize) self.rightbox = Gtk.HBox() self.rightbox.pack_start(self.buttonIconRight, False, False, 5) self.HBox1.pack_start( self.rightbox, False, False, 0 ) self.HBox1.show() self.Align1.add( self.HBox1 ) self.Align1.show() self.add( self.Align1 ) self.connectSelf( "destroy", self.onDestroy ) self.connect( "released", self.onRelease ) # Reload icons when the theme changed self.themeChangedHandlerId = iconManager.connect("changed", self.themeChanged )