November 28, 2014
 
 
RSSRSS feed

Graphical Python Programming With PyGTK - page 3

Making a Push Button

  • May 14, 2009
  • By Akkana Peck

Here's the final program:

#!/usr/bin/env python

import gtk, random

# This function will be called whenever you click on the button:
def click_handler(widget) :
    # quit the application:
    gtk.main_quit()

# This function will be called whenever the drawing area is exposed:
def expose_handler(widget, event) :
    w, h = widget.window.get_size()
    xgc = widget.window.new_gc()

    xgc.set_rgb_fg_color(gtk.gdk.color_parse("yellow"))
    widget.window.draw_arc(xgc, True, 200, 100, 200, 200, 0, 360*64)
    xgc.set_rgb_fg_color(gtk.gdk.Color(0, 0, 0))             # black
    widget.window.draw_arc(xgc, True, 240, 145, 30, 40, 0, 360*64)
    widget.window.draw_arc(xgc, True, 330, 145, 30, 40, 0, 360*64)
    xgc.line_width = 6
    widget.window.draw_arc(xgc, False, 240, 150, 120, 110, 200*64, 140*64)

# Create the main window:
win = gtk.Window()

# Organize widgets in a vertical box:
vbox = gtk.VBox()
win.add(vbox)

# Create an area to draw in:
drawing_area = gtk.DrawingArea()
drawing_area.set_size_request(600, 400)
vbox.pack_start(drawing_area)

drawing_area.connect("expose-event", expose_handler)

drawing_area.show()

# Make a pushbutton:
button = gtk.Button("Quit")

# When it's clicked, call our handler:
button.connect("clicked", click_handler)

# Add it to the window:
vbox.pack_start(button)
button.show()
# Obey the window manager quit signal:
win.connect("destroy", gtk.main_quit)

vbox.show()
win.show()

gtk.main()
Sitemap | Contact Us