PyGTK – Toolbar Class

PyGTK – Toolbar Class ”; Previous Next Toolbar class is inherited from the gtk.Container class. It holds and manages a set of buttons and other widgets. One or more horizontal strips of buttons are normally seen just below the menu bar in a top level window. The Toolbar can also be put in a detachable window called HandleBox. By default, the buttons in the gtk.Toolbar widget are laid horizontally. Vertical toolbar can be set up by setting the orientation property to gtk.ORIENTATION_VERTICAL. The toolbar can be configured to show buttons with icons, text, or both. The style enumerators are − gtk.TOOLBAR_ICONS These buttons display only icons in the toolbar. gtk.TOOLBAR_TEXT These buttons display only text labels in the toolbar. gtk.TOOLBAR_BOTH These buttons display text and icons in the toolbar. gtk.TOOLBAR_BOTH_HORIZ These buttons display icons and text alongside each other, rather than vertically stacked. A Toolbar widget is set up using the following constructor − bar = gtk.Toolbar() The constituents of Toolbar are instances of the gtk.ToolItem. The items can be ToolButton, RadioToolButton, ToggleToolButton, or SeparatorToolItem. In order to assign icon to the ToolItem object, images with predefined stock_ID can be used or a custom image can be assigned by the set_image() method. The following examples show how to construct different ToolItems − ToolButton newbtn = gtk.ToolButton(gtk.STOCK_NEW) RadioToolButton rb1 = gtk.RadioToolButton(None,gtk.STOCK_JUSTIFY_LEFT) rb2 = gtk.RadioToolButton(rb1,gtk.STOCK_JUSTIFY_RIGHT) Note that multiple radio buttons are put in the same group. SeparatorToolItem sep = gtk.SeparatorToolItem() These items are put in the toolbar by calling its insert method. gtk.Toolbar.insert(item, index) For example, bar.insert(new,0) You can also assign a tooltip to the ToolButton using the set_tooltip_text() nethod. For example, New tooltip is assigned to the new ToolButton. newbtn.set_tooltip_text(“New”) Example The following code shows a toplevel window with a tool bar set up to contain normal tool item, radio items and a separator item. import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Toolbar Demo”) self.set_default_size(250, 200) self.set_position(gtk.WIN_POS_CENTER) toolbar = gtk.Toolbar() toolbar.set_style(gtk.TOOLBAR_ICONS) toolbar.set_orientation(gtk.ORIENTATION_HORIZONTAL) newbtn = gtk.ToolButton(gtk.STOCK_NEW) newbtn.set_tooltip_text(“New”) openbtn = gtk.ToolButton(gtk.STOCK_OPEN) savebtn = gtk.ToolButton(gtk.STOCK_SAVE) sep = gtk.SeparatorToolItem() rb1 = gtk.RadioToolButton(None,gtk.STOCK_JUSTIFY_LEFT) 53 rb2 = gtk.RadioToolButton(rb1,gtk.STOCK_JUSTIFY_RIGHT) prv = gtk.ToggleToolButton(gtk.STOCK_PRINT_PREVIEW) quitbtn = gtk.ToolButton(gtk.STOCK_QUIT) toolbar.insert(newbtn, 0) toolbar.insert(openbtn, 1) toolbar.insert(savebtn, 2) toolbar.insert(sep, 3) toolbar.insert(rb1,4) toolbar.insert(rb2,5) toolbar.insert(prv,6) toolbar.insert(quitbtn, 7) quitbtn.connect(“clicked”, gtk.main_quit) vbox = gtk.VBox(False, 2) vbox.pack_start(toolbar, False, False, 0) self.add(vbox) self.connect(“destroy”, gtk.main_quit) self.show_all() def on_checked(self, widget, data = None): state = “Button1 : “+str(self.btn1.get_active())+” Button2 : “+str(self.btn2.get_active()) self.lbl.set_text(state) if __name__ == ”__main__”: PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;

PyGTK – Label CLass

PyGTK – Label Class ”; Previous Next A Label widget is useful to display non-editable text. Label is used by many other widgets internally. For example, Button has a label to show text on the face. Similarly, MenuItem objects have a label. A label is a windowless object, so it cannot receive events directly. Label class has a simple constructor − gtk.Label(str = None) The following useful methods can be used with Label object − S.NO Methods and Description 1 set_text() This sets new text as label 2 get_text() This returns text from label 3 set_use_underline() If true, an underscore in the text indicates the next character should be used for the mnemonic accelerator key. 4 set_justify This sets the alignment of the lines in the text of the label relative to each other. Possible values are – gtk.JUSTIFY_LEFT, gtk.JUSTIFY_RIGHT, gtk.JUSTIFY_CENTER, and gtk.JUSTIFY_FILL. 5 Set_line_wrap() If true, the line will be wrapped 6 set_selectable() If true, the text in the label can be selected for copy-paste 7 set_width_chars() This sets the width of a label The following signals are emitted by label widget − activate-current-link This gets emitted when the user activates a link in the label. activate-link This gets emitted to activate a URI. copy-clipboard This gets emitted when text is copied from the label to the clipboard. Print Page Previous Next Advertisements ”;

PyGTK – Scrollbar Class

PyGTK – Scrollbar Class ”; Previous Next This class is an abstract base class for gtk.Hscrollbar and gtk.Vscrollbar widgets. Both are associated with an Adjustment object. The position of the thumb of the scrollbar is controlled by scroll adjustments. The attributes of adjustment object are used as follows − lower The minimum value of the scroll region upper The maximum value of the scroll region value Represents the position of the scrollbar, which must be between lower and upper page_size Represents the size of the visible scrollable area step_increment Distance to scroll when the small stepper arrows are clicked page_increment Distance to scroll when the Page Up or Page Down keys pressed The following program shows an HScale and an HScrollbar widget placed in a VBox added to the toplevel window. Each of them is associated with an adjustment object. adj1 = gtk.Adjustment(0, 0, 101, 0.1, 1, 1) self.adj2 = gtk.Adjustment(10,0,101,5,1,1) An gtk.HScale widget is a slider control attached with adj1. Its update policy, number and position of drawing value are set up as follows − scale1 = gtk.HScale(adj1) scale1.set_update_policy(gtk.UPDATE_CONTINUOUS) scale1.set_digits(1) scale1.set_value_pos(gtk.POS_TOP) scale1.set_draw_value(True) gtk.HScrollbar provides a horizontal scrollbar. It is associated with adj2 object. Its update policy too is set to CONTINUOUS. self.bar1 = gtk.HScrollbar(self.adj2) self.bar1.set_update_policy(gtk.UPDATE_CONTINUOUS) In order to display instantaneous value of the scrollbar, ”value-changed” signal of the adjustment object — adj2 is connected to callback function on_scrolled(). The function retrieves the value property of adjustment object and displays it on a label below the scrollbar. self.adj2.connect(“value_changed”, self.on_scrolled) def on_scrolled(self, widget, data = None): self.lbl2.set_text(“HScrollbar value: “+str(int(self.adj2.value))) Example Observe the following code − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Range widgets Demo”) self.set_default_size(250, 200) self.set_position(gtk.WIN_POS_CENTER) adj1 = gtk.Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0) self.adj2 = gtk.Adjustment(10,0,101,5,1,1) scale1 = gtk.HScale(adj1) scale1.set_update_policy(gtk.UPDATE_CONTINUOUS) scale1.set_digits(1) scale1.set_value_pos(gtk.POS_TOP) scale1.set_draw_value(True) vb = gtk.VBox() vb.add(scale1) lbl1 = gtk.Label(“HScale”) vb.add(lbl1) self.bar1 = gtk.HScrollbar(self.adj2) self.bar1.set_update_policy(gtk.UPDATE_CONTINUOUS) vb.add(self.bar1) self.lbl2 = gtk.Label(“HScrollbar value: “) vb.add(self.lbl2) self.adj2.connect(“value_changed”, self.on_scrolled) self.add(vb) self.connect(“destroy”, gtk.main_quit) self.show_all() def on_scrolled(self, widget, data=None): self.lbl2.set_text(“HScrollbar value: “+str(int(self.adj2.value))) if __name__ == ”__main__”: PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;

PyGTK – Entry Class

PyGTK – Entry Class ”; Previous Next Entry widget is a single-line text entry widget. If the entered text is longer than the allocation of the widget, the widget will scroll so that the cursor position is visible. Entry field can be converted in password mode using set_visibility() method of this class. Entered text is substituted by character chosen by invisible_char() method, default being ”*”. The Entry class has the following constructor − gtk.Entry(max = 0) Here, max stands for maximum length of entry field in characters. The parameter takes a numeric value (0-65536). The following table shows the important methods of an Entry class − S.NO Methods and Description 1 set_visibility(visible) If false, the contents are obscured by replacing the characters with the default invisible character — ”*” 2 set_invisible_char(char) The default ”*” characters in the entry field are replaced by char 3 set_max_length(x) This sets the “max-length” property to the value of x. (0-65536) 4 set_text(str) This sets the “text” property to the value of str. The string in str replaces the current contents of the entry. 5 get_text() This returns the value of the “text” property which is a string containing the contents of the entry. 6 set_alignment() This sets the “xalign” property to the value of xalign. set_alignment() controls the horizontal positioning of the contents in the Entry field. The following signals are emitted by entry widget − activate This is emitted when the entry is activated either by user action or programmatically with the gtk.Widget.activate() method. backspace This is emitted when the Backspace key is entered from the keyboard. copy-clipboard This is emitted when the selection text in the entry is copied to the clipboard. cut-clipboard This is emitted when the selection in the entry is cut and placed in the clipboard. paste-clipboard This is emitted when the contents of the clipboard are pasted into the entry. Print Page Previous Next Advertisements ”;