1 #!/usr/bin/env python
2
3 # example text.py
4
5 import gtk
6
7 class TextExample:
8 def text_toggle_editable(self, checkbutton,text):
9 text.set_editable(checkbutton.active)
10
11 def text_toggle_word_wrap(self, checkbutton, text):
12 text.set_word_wrap(checkbutton.active)
13
14 def close_application(self, widget):
15 gtk.mainquit()
16
17 def __init__(self):
18 window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
19 window.set_usize(600, 500)
20 window.set_policy(gtk.TRUE, gtk.TRUE, gtk.FALSE)
21 window.connect("destroy", self.close_application)
22 window.set_title("Text Widget Example")
23 window.set_border_width(0)
24
25 box1 = gtk.GtkVBox(gtk.FALSE, 0)
26 window.add(box1)
27 box1.show()
28
29 box2 = gtk.GtkVBox(gtk.FALSE, 10)
30 box2.set_border_width(10)
31 box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
32 box2.show()
33
34 table = gtk.GtkTable(2, 2, gtk.FALSE)
35 table.set_row_spacing(0, 2)
36 table.set_col_spacing(0, 2)
37 box2.pack_start(table, gtk.TRUE, gtk.TRUE, 0)
38 table.show()
39
40 # Create the GtkText widget
41 text = gtk.GtkText()
42 text.set_editable(gtk.TRUE)
43 table.attach(text, 0, 1, 0, 1,
44 gtk.EXPAND | gtk.SHRINK | gtk.FILL,
45 gtk.EXPAND | gtk.SHRINK | gtk.FILL, 0, 0)
46 text.show()
47
48 # Add a vertical scrollbar to the GtkText widget
49 vscrollbar = gtk.GtkVScrollbar(text.get_vadjustment())
50 table.attach(vscrollbar, 1, 2, 0, 1,
51 gtk.FILL, gtk.EXPAND | gtk.SHRINK | gtk.FILL, 0, 0)
52 vscrollbar.show()
53
54 # Get the window's color map and allocate the color red
55 cmap = window.get_colormap()
56 color = cmap.alloc('red')
57
58 # Load a fixed font
59 fixed_font = gtk.load_font(
60 "-misc-fixed-medium-r-*-*-*-140-*-*-*-*-iso8859-1")
61
62 # Realizing a widget creates a window for it,
63 # ready for us to insert some text
64 text.realize()
65
66 # Freeze the text widget, ready for multiple updates
67 text.freeze()
68
69 # Insert some colored text
70 text.insert(None, text.get_style().black, None, "Supports ")
71 text.insert(None, color, None, "colored ")
72 text.insert(None, text.get_style().black, None, "text and different ")
73 text.insert(fixed_font, text.get_style().black, None, "fonts\n\n")
74
75 # Load the file text.py into the text window
76 infile = open("text.py", "r")
77
78 if infile:
79 string = infile.read()
80 infile.close()
81 text.insert(fixed_font, None, None, string)
82
83 # Thaw the text widget, allowing the updates to become visible
84 text.thaw()
85
86 hbox = gtk.GtkHButtonBox()
87 box2.pack_start(hbox, gtk.FALSE, gtk.FALSE, 0)
88 hbox.show()
89
90 check = gtk.GtkCheckButton("Editable")
91 hbox.pack_start(check, gtk.FALSE, gtk.FALSE, 0)
92 check.connect("toggled", self.text_toggle_editable, text)
93 check.set_active(gtk.TRUE)
94 check.show()
95 check = gtk.GtkCheckButton("Wrap Words")
96 hbox.pack_start(check, gtk.FALSE, gtk.TRUE, 0)
97 check.connect("toggled", self.text_toggle_word_wrap, text)
98 check.set_active(gtk.FALSE)
99 check.show()
100
101 separator = gtk.GtkHSeparator()
102 box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 0)
103 separator.show()
104
105 box2 = gtk.GtkVBox(gtk.FALSE, 10)
106 box2.set_border_width(10)
107 box1.pack_start(box2, gtk.FALSE, gtk.TRUE, 0)
108 box2.show()
109
110 button = gtk.GtkButton("close")
111 button.connect("clicked", self.close_application)
112 box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
113 button.set_flags(gtk.CAN_DEFAULT)
114 button.grab_default()
115 button.show()
116 window.show()
117
118 def main():
119 gtk.mainloop()
120 return 0
121
122 if __name__ == "__main__":
123 TextExample()
124 main()
|