diff options
author | James Henstridge <jamesh@src.gnome.org> | 1998-12-06 10:08:08 +0000 |
---|---|---|
committer | James Henstridge <jamesh@src.gnome.org> | 1998-12-06 10:08:08 +0000 |
commit | 997c302f261c2000d057bdaa989ec12ca00d7fa9 (patch) | |
tree | 32fd0d0cf70a0d804df0765d610518db281801a0 /examples/simple | |
download | pygtk-997c302f261c2000d057bdaa989ec12ca00d7fa9.tar.gz |
Initial revision
Diffstat (limited to 'examples/simple')
-rw-r--r-- | examples/simple/.cvsignore | 6 | ||||
-rw-r--r-- | examples/simple/README | 5 | ||||
-rw-r--r-- | examples/simple/dnd.py | 77 | ||||
-rwxr-xr-x | examples/simple/hello1.py | 32 | ||||
-rwxr-xr-x | examples/simple/hello2.py | 29 | ||||
-rwxr-xr-x | examples/simple/scribble.py | 81 | ||||
-rwxr-xr-x | examples/simple/simple1.py | 37 | ||||
-rwxr-xr-x | examples/simple/simple2.py | 26 | ||||
-rwxr-xr-x | examples/simple/tooltip1.py | 43 | ||||
-rwxr-xr-x | examples/simple/tooltip2.py | 32 |
10 files changed, 368 insertions, 0 deletions
diff --git a/examples/simple/.cvsignore b/examples/simple/.cvsignore new file mode 100644 index 00000000..c2831020 --- /dev/null +++ b/examples/simple/.cvsignore @@ -0,0 +1,6 @@ +Makefile.in +Makefile +*~ +*.pyc +*.pyo + diff --git a/examples/simple/README b/examples/simple/README new file mode 100644 index 00000000..9bcb80cc --- /dev/null +++ b/examples/simple/README @@ -0,0 +1,5 @@ +These are some of the simplest examples you can do. They are only intended +to help you see how to use pygtk. The examples ending in 1.py use the low +level C-like interface, while the ones ending in 2.py are translations that +use Gtkinter. + diff --git a/examples/simple/dnd.py b/examples/simple/dnd.py new file mode 100644 index 00000000..f5bf6d75 --- /dev/null +++ b/examples/simple/dnd.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +from gtk import * + +# a nice easy to read fixed spacing font. +font = load_font("-*-lucidatypewriter-medium-r-*-*-14-*-*-*-*-*-*-*") + +list = "abcdefghijklmnopqrstuvwxyz" + \ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + \ + "0123456789" + \ + "`~!@#$%^&*()-_=+\|[]{};:'\",.<>/? " +printable = '' +for c in map(chr, range(256)): + if c in list: + printable = printable + c + else: + printable = printable + '.' +del list, c + +def format_line(str): + hexstr = reduce(lambda x, a: x + hex(256+ord(a))[-2:] + ' ', str, '') + if len(hexstr) < 48: + hexstr = (hexstr + ' '*48)[:48] + return hexstr + ' ' + reduce(lambda x, a: x + printable[ord(a)],str,'') + +def format_data(str): + ret = '' + while len(str) > 16: + line = str[:16] + str = str[16:] + ret = ret + format_line(line) + '\n' + if str: ret = ret + format_line(str) + return ret + +def dnd_drop(b, event): + data_type.set(event.data_type) + data.delete_text(0, data.get_length()) + data.insert(font, black, white, format_data(event.data)) + pass + +win = GtkWindow() +win.set_title("Drag to Me") +win.border_width(10) + +t = GtkTable(5,4) +win.add(t) +t.show() + +l = GtkLabel("Data Type") +l.set_justify(JUSTIFY_RIGHT) +t.attach(l, 0,1, 0,1, xoptions=FILL) +l.show() + +data_type = GtkLabel("*None*") +data_type.set_justify(JUSTIFY_LEFT) +t.attach(data_type, 1,2, 0,1) +data_type.show() + +l = GtkLabel("Data") +l.set_justify(JUSTIFY_RIGHT) +t.attach(l, 0,1, 1,2, xoptions=FILL) +l.show() + +data = GtkText() +data.set_usize(600, -1) +style = data.get_style() +white = style.white +black = style.black +t.attach(data, 1,2, 1,2) +data.show() + +win.connect("drop_data_available_event", dnd_drop) +win.dnd_drop_set(TRUE, ['text/plain', 'application/x-color', 'ALL'], FALSE) + +win.connect("destroy", mainquit) +win.show() + +mainloop() diff --git a/examples/simple/hello1.py b/examples/simple/hello1.py new file mode 100755 index 00000000..60b9ab18 --- /dev/null +++ b/examples/simple/hello1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +# this is a translation of "Hello World III" from the GTK manual, +# using gtkmodule + +from _gtk import * +from GTK import * + +def hello(*args): + print "Hello World" + gtk_widget_destroy(window) + +def destroy(*args): + gtk_widget_hide(window) + gtk_main_quit() + +gtk_init() + +window = gtk_window_new(WINDOW_TOPLEVEL) +gtk_signal_connect(window, "destroy", destroy) +gtk_container_border_width(window, 10) + +button = gtk_button_new_with_label("Hello World") +gtk_signal_connect(button, "clicked", hello) +gtk_container_add(window, button) +gtk_widget_show(button) + +gtk_widget_show(window) + +gtk_main() + + diff --git a/examples/simple/hello2.py b/examples/simple/hello2.py new file mode 100755 index 00000000..b247f523 --- /dev/null +++ b/examples/simple/hello2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +# this is a translation of "Hello World III" from the GTK manual, +# using gtk.py + +from gtk import * + +def hello(*args): + print "Hello World" + window.destroy() + +def destroy(*args): + window.hide() + mainquit() + +window = GtkWindow(WINDOW_TOPLEVEL) +window.connect("destroy", destroy) +window.border_width(10) + +button = GtkButton("Hello World") +button.connect("clicked", hello) +window.add(button) +button.show() + +window.show() + +mainloop() + + diff --git a/examples/simple/scribble.py b/examples/simple/scribble.py new file mode 100755 index 00000000..d0aee9e0 --- /dev/null +++ b/examples/simple/scribble.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +#this is a simple translation of the scribble example that comes with GTK+ + +import sys +from gtk import * + +pixmap = None + +def configure_event(widget, event): + global pixmap + win = widget.get_window() + pixmap = create_pixmap(win, win.width, win.height, -1) + draw_rectangle(pixmap, widget.get_style().white_gc, TRUE, + 0, 0, win.width, win.height) + return TRUE + +def expose_event(widget, event): + area = event.area + gc = widget.get_style().fg_gc[STATE_NORMAL] + widget.draw_pixmap(gc, pixmap, area[0], area[1], area[0], area[1], + area[2], area[3]) + return FALSE + +def draw_brush(widget, x, y): + rect = (x-5, y-5, 10, 10) + draw_rectangle(pixmap, widget.get_style().black_gc, TRUE, + x-5, y-5, 10, 10) + widget.queue_draw() + +def button_press_event(widget, event): + if event.button == 1 and pixmap != None: + draw_brush(widget, event.x, event.y) + return TRUE + +def motion_notify_event(widget, event): + if event.is_hint: + x, y = event.window.pointer + state = event.window.pointer_state + else: + x = event.x; y = event.y + state = event.state + if state & GDK.BUTTON1_MASK and pixmap != None: + draw_brush(widget, x, y) + return TRUE + +def main(): + win = GtkWindow() + win.set_name("Test Input") + win.connect("destroy", mainquit) + win.border_width(5) + + vbox = GtkVBox(spacing=3) + win.add(vbox) + vbox.show() + + drawing_area = GtkDrawingArea() + drawing_area.size(200, 200) + vbox.pack_start(drawing_area) + drawing_area.show() + + drawing_area.connect("expose_event", expose_event) + drawing_area.connect("configure_event", configure_event) + drawing_area.connect("motion_notify_event", motion_notify_event) + drawing_area.connect("button_press_event", button_press_event) + drawing_area.set_events(GDK.EXPOSURE_MASK | + GDK.LEAVE_NOTIFY_MASK | + GDK.BUTTON_PRESS_MASK | + GDK.POINTER_MOTION_MASK | + GDK.POINTER_MOTION_HINT_MASK) + + button = GtkButton("Quit") + vbox.pack_start(button, expand=FALSE, fill=FALSE) + button.connect("clicked", win.destroy) + button.show() + win.show() + mainloop() + +if __name__ == '__main__': + main() + diff --git a/examples/simple/simple1.py b/examples/simple/simple1.py new file mode 100755 index 00000000..63980ecd --- /dev/null +++ b/examples/simple/simple1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +# translation of "Hello World III" from GTK manual, using gtk_object_new +# with gtkmodule + +from _gtk import * +from GTK import * + +def hello(*args): + print "Hello World" + gtk_widget_destroy(window) + +def destroy(*args): + gtk_widget_hide(window) + gtk_main_quit() + +gtk_init() + +window = gtk_object_new(gtk_window_get_type(), { + 'type': WINDOW_TOPLEVEL, + 'title': 'Hello World', + 'allow_grow': 0, + 'allow_shrink': 0, + 'border_width': 10 +}) +gtk_signal_connect(window, "destroy", destroy) + +button = gtk_object_new(gtk_button_get_type(), { + 'label': 'Hello World', + 'parent': window, + 'visible': 1 +}) +gtk_signal_connect(button, "clicked", hello); + +gtk_widget_show(window) +gtk_main() + diff --git a/examples/simple/simple2.py b/examples/simple/simple2.py new file mode 100755 index 00000000..f541b275 --- /dev/null +++ b/examples/simple/simple2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +# translation of "Hello World III" from gtk manual, using the new() function +# from gtk.py + +from gtk import * + +def hello(*args): + print "Hello World" + window.destroy() + +def destroy(*args): + window.hide() + mainquit() + +window = new(GtkWindow, type=WINDOW_TOPLEVEL, title='Hello World', + allow_grow=FALSE, allow_shrink=FALSE, border_width=10) +window.connect("destroy", destroy) + +print "" +button = new(GtkButton, label="Hello World", parent=window, visible=TRUE) +button.connect("clicked", hello) + +window.show_all() +mainloop() + diff --git a/examples/simple/tooltip1.py b/examples/simple/tooltip1.py new file mode 100755 index 00000000..93f0bcda --- /dev/null +++ b/examples/simple/tooltip1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# translation of "Hello World III" from GTK manual, using gtk_object_new +# with gtkmodule. Also implements a tooltip for the button. + +from _gtk import * +from GTK import * + +def hello(*args): + print "Hello World" + gtk_widget_destroy(window) + +def destroy(*args): + gtk_widget_hide(window) + gtk_main_quit() + +gtk_init() + +tt = gtk_tooltips_new() +gtk_tooltips_set_delay(tt, 500) + +window = gtk_object_new("GtkWindow", { + 'type': WINDOW_TOPLEVEL, + 'title': 'Hello World', + 'allow_grow': 0, + 'allow_shrink': 0, + 'border_width': 10 +}) +gtk_signal_connect(window, "destroy", destroy) + +button = gtk_object_new("GtkButton", { + 'label': 'Hello World', + 'parent': window, + 'visible': 1 +}) +gtk_signal_connect(button, "clicked", hello) + +gtk_tooltips_set_tip(tt, button, 'Prints "Hello World"', '') +gtk_tooltips_enable(tt) + +gtk_widget_show(window) +gtk_main() + diff --git a/examples/simple/tooltip2.py b/examples/simple/tooltip2.py new file mode 100755 index 00000000..3fad341b --- /dev/null +++ b/examples/simple/tooltip2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +# translation of "Hello World III" from GTK manual, using new() function +# from gtk.py. Also implements a tooltip for the button. + +from gtk import * + +def hello(*args): + print "Hello World" + window.destroy() + +def destroy(*args): + window.hide() + mainquit() + +tt = GtkTooltips() +tt.set_delay(500) + +window = new(GtkWindow, type=WINDOW_TOPLEvEL, title="Hello World", + allow_grow=FALSE, allow_shrink=FALSE, border_width=10) +window.connect("destroy", destroy) + +button = new(GtkButton, label="Hello World", parent=window, visible=TRUE) +button.connect("clicked", hello) + +tt.set_tip(button, 'Prints "Hello World"', '') +tt.enable() + +window.show() +mainloop() + + |