User Tools

Site Tools


python:pygtk_widgetdemo

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:pygtk_widgetdemo [2013/03/16 17:41] (current)
Line 1: Line 1:
 +=== Small introduction to a few interesting pyGTK widgets ===
 +
 +
 +==== Intro ====
 +
 +This is how it looks like (just to make you curious how it's made):
 +
 +{{:​python:​widgetsdemo.png| Widgets Demo }}
 +
 +
 +We'll show:
 +
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtklabel.html | gtk.Label
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkcheckbutton.html | gtk.CheckButton
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkimage.html | gtk.Image()
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkimage.html | gtk.Image()
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkentry.html | gtk.Entry()
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkhscale.html | gtk.HScale()
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkdrawingarea.html | gtk.DrawingArea()
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtktogglebutton.html | gtk.ToggleButton()
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkcalendar.html | gtk.Calendar()
 +
 +
 +==== Full Source code ====
 +
 +
 +Just follow the comments inside the source file to find out how it's made each of the components; we use a VBox Layout, and from time to time, HBox to align horizontally two related elements.
 +
 +<code python>
 +#​!/​usr/​bin/​env python2.4
 +# -*- coding: utf8 -*-
 +
 +import gtk
 +
 +class WidgetsDemo(gtk.Window):​
 +    def __init__(self):​
 +        super(WidgetsDemo,​ self).__init__()
 +        ​
 +        self.set_position(gtk.WIN_POS_CENTER)
 +        self.set_title("​Widgets Demo")
 +        self.set_border_width(10)
 +        self.connect("​destroy",​ gtk.main_quit)
 +        ​
 +        # we gonna create a vertical layout
 +        vert = gtk.VBox(False,​ 3)
 +        ​
 +        # create a label
 +        txtlabel = """​
 +            This a line of text
 +            This is the second line of text
 +            And of course, this is the end of the poem!
 +        """​
 +        self.label = gtk.Label(txtlabel)
 +        vert.pack_start(self.label)
 +        ​
 +        # create a CheckButton
 +        chkbtn = gtk.CheckButton("​Show label text")
 +        chkbtn.set_active(True)
 +        chkbtn.unset_flags(gtk.CAN_FOCUS)
 +        chkbtn.connect("​clicked",​ self.chkbtn_clicked)
 +        vert.pack_start(chkbtn)
 +        ​
 +        # create a ComboBox
 +        # combo_box_new_text() is a function is a convenience function that constructs ​
 +        # a new text combo box, which is a gtk.ComboBox just displaying strings.
 +        combo = gtk.combo_box_new_text()
 +        combo.connect("​changed",​ self.combo_on_changed)
 +        combo.append_text("​Linux"​)
 +        combo.append_text("​FreeBSD"​)
 +        combo.append_text("​NetBSD"​)
 +        combo.set_active(1)
 +        vert.pack_start(combo)
 +        ​
 +        # image
 +        image = gtk.Image()
 +        image.set_from_file("​testimage.jpg"​)
 +        vert.pack_start(image)
 +        ​
 +        # entry text
 +        # first, the label where the entered text will be displayed
 +        self.lblEntry = gtk.Label()
 +        entry = gtk.Entry()
 +        entry.add_events(gtk.gdk.KEY_RELEASE_MASK)
 +        entry.connect("​key-release-event",​ self.entry_onkey_release)
 +        vert.pack_start(self.lblEntry)
 +        vert.pack_start(entry)
 +        ​
 +        # horizontal scale
 +        self.lblScale = gtk.Label()
 +        self.lblScale.set_text('​0.00'​)
 +        scale = gtk.HScale()
 +        scale.set_range(0,​ 100)
 +        scale.set_increments(1,​ 10)
 +        scale.set_digits(0)
 +        scale.set_size_request(150,​ 30)
 +        scale.connect("​value-changed",​ self.scale_on_changed)
 +        ​
 +        scaleLayout = gtk.HBox()
 +        scaleLayout.pack_start(self.lblScale)
 +        scaleLayout.pack_start(scale)
 +        vert.pack_start(scaleLayout)
 +
 +        # toggle buttons with a drawing area
 +        self.color = [0, 0, 0]
 +                ​
 +        self.darea = gtk.DrawingArea()
 +        self.darea.set_size_request(50,​ 50)
 +        self.darea.modify_bg(gtk.STATE_NORMAL,​ gtk.gdk.color_parse("​white"​))
 +        red = gtk.ToggleButton("​Red"​)
 +        red.set_size_request(80,​ 35)
 +        red.connect("​clicked",​ self.onred)
 +        green = gtk.ToggleButton("​Green"​)
 +        green.set_size_request(80,​ 35)
 +        green.connect("​clicked",​ self.ongreen)
 +        blue = gtk.ToggleButton("​Blue"​)
 +        blue.set_size_request(80,​ 35)
 +        blue.connect("​clicked",​ self.onblue)
 +
 +        vert.pack_start(self.darea)
 +        hcolors = gtk.HBox()
 +        hcolors.pack_start(red)
 +        hcolors.pack_start(green)
 +        hcolors.pack_start(blue)
 +        vert.pack_start(hcolors)
 +    ​
 +        # calendar
 +        calendar = gtk.Calendar() ​
 +        vert.pack_start(calendar)
 +        ​
 +        self.add(vert)
 +        self.show_all()
 +    ​
 +    def chkbtn_clicked(self,​ widget):
 +        if widget.get_active():​
 +            self.label.show()
 +        else:
 +            self.label.hide()
 +
 +    def combo_on_changed(self,​ widget):
 +        self.set_title(widget.get_active_text())
 +
 +    def entry_onkey_release(self,​ widget, event):
 +        self.lblEntry.set_text(widget.get_text())
 +
 +    def scale_on_changed(self,​ widget):
 +        self.lblScale.set_text(str(widget.get_value()))
 +
 +    def onred(self, widget):
 +        if widget.get_active():​
 +            self.color[0] = 65535
 +        else: self.color[0] = 0
 +
 +        self.darea.modify_bg(gtk.STATE_NORMAL,​ gtk.gdk.Color(self.color[0], ​
 +            self.color[1],​ self.color[2]))
 +
 +    def ongreen(self,​ widget):
 +        if (widget.get_active()):​
 +            self.color[1] = 65535
 +        else: self.color[1] = 0
 +
 +        self.darea.modify_bg(gtk.STATE_NORMAL,​ gtk.gdk.Color(self.color[0],​
 +            self.color[1],​ self.color[2]))
 +
 +    def onblue(self,​ widget):
 +        if (widget.get_active()):​
 +            self.color[2] = 65535
 +        else: self.color[2] = 0
 +
 +        self.darea.modify_bg(gtk.STATE_NORMAL,​ gtk.gdk.Color(self.color[0], ​
 +            self.color[1],​ self.color[2]))
 +
 +
 +
 +if __name__ == "​__main__":​
 +    WidgetsDemo()
 +    gtk.main()
 +</​code>​
 +
 +
 +==== Credits & Thanks ====
 +
 +For [[http://​zetcode.com/​tutorials/​pygtktutorial/​ | this]] tutorial (Jan Bodnar).
  
python/pygtk_widgetdemo.txt ยท Last modified: 2013/03/16 17:41 (external edit)