GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 22, 2014 1:53 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Need guidance making some elements blink
PostPosted: Sat Mar 01, 2014 7:45 pm 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
I've got a 3D tic-tac-toe game working (project qubic on sourceforge). I'd like to make the winning elements blink when the game is over.

My mind is full of timers, events, signals and threads, but I'm clueless about which combination makes the most sense in GTK+. The elements are just Cairo crosses or circles drawn on top of everything else. My plan is to draw them alternately in black and white in perhaps half-second intervals. I just don't know how to get timing right so it works with the GTK/GDK/Cairo framework.

Could someone sketch it out for me?


Top
 Profile  
 
 Post subject: Re: Need guidance making some elements blink
PostPosted: Sun Mar 02, 2014 4:11 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
Using python3 and gtk3.0 you can do something like that:

Code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk
from gi.repository import GLib

class Window(Gtk.Window):

    def __init__(self):

        super(Window,self).__init__()

        self.connect("destroy",Gtk.main_quit)

        grid=Gtk.Grid()
        grid.set_orientation(Gtk.Orientation.VERTICAL)
        self.add(grid)

        for i in range(0,3):
            for j in range(0,3):
                da=Gtk.DrawingArea()
                da.set_size_request(100,100)
                grid.attach(da,i,j,1,1)
                if i==1 and j==1:
                    self.da=da

        self.show_all()

        self.provider={}
        self.provider[True]=Gtk.CssProvider()
        css="* { background-color: green} "
        self.provider[True].load_from_data(bytes(css,"ascii"))

        self.provider[False]=Gtk.CssProvider()
        css="* { background-color: red} "
        self.provider[False].load_from_data(bytes(css,"ascii"))

        self.status=False

        self.on_timeout()
        GLib.timeout_add(1000,self.on_timeout)

    def on_timeout(self):
        self.da.get_style_context().add_provider(self.provider[self.status],10)
        self.status=not self.status
        return True

window=Window()
Gtk.main()



One other advice: Don't use threads.


Top
 Profile  
 
 Post subject: Re: Need guidance making some elements blink
PostPosted: Sun Mar 02, 2014 4:31 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
and with cairo:

Code:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import cairo

class Window(Gtk.Window):

    def __init__(self):

        super(Window,self).__init__()

        self.connect("destroy",Gtk.main_quit)

        grid=Gtk.Grid()
        grid.set_orientation(Gtk.Orientation.VERTICAL)
        self.add(grid)

        for i in range(0,3):
            for j in range(0,3):
                da=Gtk.DrawingArea()
                da.set_size_request(100,100)
                grid.attach(da,i,j,1,1)
                if i==1 and j==1:
                    self.da=da
                    da.connect("draw",self.on_draw)

        self.show_all()

        self.status=False

        self.on_timeout()
        GLib.timeout_add(1000,self.on_timeout)

    def on_draw(self,widget,cr):
        if self.status:
            cr.set_source_rgb(1,0,0)
        else:
            cr.set_source_rgb(0,1,0)
        cr.rectangle(0,0,100,100)
        cr.fill()

    def on_timeout(self):
        self.status=not self.status
        self.da.queue_draw()
        return True

window=Window()
Gtk.main()



Top
 Profile  
 
 Post subject: Re: Need guidance making some elements blink
PostPosted: Tue Mar 04, 2014 2:52 pm 
Offline
Familiar Face

Joined: Tue Jan 07, 2014 1:15 am
Posts: 7
Well, this is a start. Thanks.

It's also obvious I would have done well to mention all the existing code is C and GTK 2.24. But I can probably figure out how to do the same thing.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group