~doctormo/python-snippets/lp-merge-request-example

« back to all changes in this revision

Viewing changes to pygtk/layout.py

  • Committer: Jono Bacon
  • Date: 2009-12-31 01:32:01 UTC
  • Revision ID: jono@ubuntu.com-20091231013201-0jqe2hpla824dafl
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Layout]
 
4
# [SNIPPET_CATEGORIES: PyGTK]
 
5
# [SNIPPET_DESCRIPTION: A simple widget layout example]
 
6
 
 
7
# example layout.py
 
8
 
 
9
import pygtk
 
10
pygtk.require('2.0')
 
11
import gtk
 
12
import random
 
13
 
 
14
class LayoutExample:
 
15
    def WindowDeleteEvent(self, widget, event):
 
16
        # return false so that window will be destroyed
 
17
        return False
 
18
 
 
19
    def WindowDestroy(self, widget, *data):
 
20
        # exit main loop
 
21
        gtk.main_quit()
 
22
 
 
23
    def ButtonClicked(self, button):
 
24
        # move the button
 
25
        self.layout.move(button, random.randint(0,500),
 
26
                         random.randint(0,500))
 
27
 
 
28
    def __init__(self):
 
29
        # create the top level window
 
30
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
31
        window.set_title("Layout Example")
 
32
        window.set_default_size(300, 300)
 
33
        window.connect("delete-event", self.WindowDeleteEvent)
 
34
        window.connect("destroy", self.WindowDestroy)
 
35
        # create the table and pack into the window
 
36
        table = gtk.Table(2, 2, False)
 
37
        window.add(table)
 
38
        # create the layout widget and pack into the table
 
39
        self.layout = gtk.Layout(None, None)
 
40
        self.layout.set_size(600, 600)
 
41
        table.attach(self.layout, 0, 1, 0, 1, gtk.FILL|gtk.EXPAND,
 
42
                     gtk.FILL|gtk.EXPAND, 0, 0)
 
43
        # create the scrollbars and pack into the table
 
44
        vScrollbar = gtk.VScrollbar(None)
 
45
        table.attach(vScrollbar, 1, 2, 0, 1, gtk.FILL|gtk.SHRINK,
 
46
                     gtk.FILL|gtk.SHRINK, 0, 0)
 
47
        hScrollbar = gtk.HScrollbar(None)
 
48
        table.attach(hScrollbar, 0, 1, 1, 2, gtk.FILL|gtk.SHRINK,
 
49
                     gtk.FILL|gtk.SHRINK, 0, 0) 
 
50
        # tell the scrollbars to use the layout widget's adjustments
 
51
        vAdjust = self.layout.get_vadjustment()
 
52
        vScrollbar.set_adjustment(vAdjust)
 
53
        hAdjust = self.layout.get_hadjustment()
 
54
        hScrollbar.set_adjustment(hAdjust)
 
55
        # create 3 buttons and put them into the layout widget
 
56
        button = gtk.Button("Press Me")
 
57
        button.connect("clicked", self.ButtonClicked)
 
58
        self.layout.put(button, 0, 0)
 
59
        button = gtk.Button("Press Me")
 
60
        button.connect("clicked", self.ButtonClicked)
 
61
        self.layout.put(button, 100, 0)
 
62
        button = gtk.Button("Press Me")
 
63
        button.connect("clicked", self.ButtonClicked)
 
64
        self.layout.put(button, 200, 0)
 
65
        # show all the widgets
 
66
        window.show_all()
 
67
 
 
68
def main():
 
69
    # enter the main loop
 
70
    gtk.main()
 
71
    return 0
 
72
 
 
73
if __name__ == "__main__":
 
74
    LayoutExample()
 
75
    main()