October 31, 2014
 
 
RSSRSS feed

PythonGTK Programming part 3: Screensaver, Objects, and User Input - page 3

The Fullscreen Function

  • June 10, 2009
  • By Akkana Peck
#!/usr/bin/env python

import gtk, random, gobject

# The QixWindow class that holds variables specific to each window:
class QixWindow :
    def __init__(self) :
        self.xgc = None
        self.x1 = None
        self.x2 = None
        self.y1 = None
        self.y2 = None
        self.r = random.randint(0, 65535)
        self.g = random.randint(0, 65535)
        self.b = random.randint(0, 65535)
        self.movesize = 20
        self.colorjump = 2048
        self.is_fullscreen = None

    @staticmethod
    def clip(i, min, max) :
        if i < min :
            return min
        if i > max :
            return max
        return i

    # Called whenever any key is pressed:
    def key_press_event(self, widget, event) :
        if event.string == "q" :
            gtk.main_quit()
        elif event.string == "f" :
            if self.is_fullscreen :
                widget.window.unfullscreen()
                self.is_fullscreen = False
            else :
                widget.window.fullscreen()
                self.is_fullscreen = True

    def idle_handler(self, widget) :

        # Get the window size every time, in case it's been resized:
        w, h = widget.window.get_size()

        # Initialize the GC and xy values if this is the first time through:
        if (self.xgc == None) :
            self.xgc = widget.window.new_gc()
            self.x1 = random.randint(0, w)
            self.x2 = random.randint(0, w)
            self.y1 = random.randint(0, h)
            self.y2 = random.randint(0, h)

        # Change the line boundaries a little bit:
        self.x1 = QixWindow.clip(self.x1 +
                                 random.randint(-self.movesize, self.movesize),
                                 0, w)
        self.x2 = QixWindow.clip(self.x2 +
                                 random.randint(-self.movesize, self.movesize),
                                 0, w)
        self.y1 = QixWindow.clip(self.y1 +
                                 random.randint(-self.movesize, self.movesize),
                                 0, h)
        self.y2 = QixWindow.clip(self.y2 +
                                 random.randint(-self.movesize, self.movesize),
                                 0, h)

        # Change the color a little bit:
        self.r = QixWindow.clip(self.r + random.randint(-self.colorjump,
                                                         self.colorjump),
                                0, 65535)
        self.g = QixWindow.clip(self.g + random.randint(-self.colorjump,
                                                         self.colorjump),
                                0, 65535)
        self.b = QixWindow.clip(self.b + random.randint(-self.colorjump,
                                                         self.colorjump),
                                0, 65535)
        self.xgc.set_rgb_fg_color(gtk.gdk.Color(self.r, self.g, self.b))

        # Draw the new line
        widget.window.draw_line(self.xgc, self.x1, self.y1, self.x2, self.y2)

        # Return True so we'll be called again:
        return True

    def make_window(self) :
        # These used to be globals, but now they're made into class variables
        # by putting "self." in front of each one:
        self.xgc = None
        self.x1 = None
        self.x2 = None
        self.y1 = None
        self.y2 = None
        self.r = random.randint(0, 65535)
        self.g = random.randint(0, 65535)
        self.b = random.randint(0, 65535)
        self.movesize = 20
        self.colorjump = 2048

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

        # Handle key events
        win.connect("key-press-event", self.key_press_event)
        self.is_fullscreen = True

        # 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)

        # set our drawing function
        idle_id = gobject.idle_add(self.idle_handler, drawing_area)

        drawing_area.show()

        # Obey the window manager quit signal:
        win.connect("destroy", gtk.main_quit)

        vbox.show()
        win.show()

        gtk.main()

w = QixWindow()
w.make_window()
Sitemap | Contact Us