Python Forum

Full Version: [pygtk] fixed.move not working as expected
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anyone explain why the following code isn't working as I expect?  What I expect is a soccer ball png image would appear in the upper left corner and make several small moves until it was in the upper right corner.  What happens is it displays in the UL, then in the UR without the interim moves.  
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject
import time
#--------------------------------------------------------------------
class MWin(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        self.set_default_size(800,600) #set window size
        self.set_border_width(5)

        self.fixed = Gtk.Fixed()       #add fixed container to window
        self.add(self.fixed)
        self.ExitButton = Gtk.Button(label=" Exit ")
        self.fixed.put(self.ExitButton,10,600-100)
        self.ExitButton.connect("clicked", self.exit_button_clicked)

        self.SoccerBall = Gtk.Image()
        self.SoccerBall.set_from_file("/usr/local/bin/graphics/soccerball.png")
        self.fixed.put(self.SoccerBall,1,1)

        self.DiagButton = Gtk.Button(label=" Dialog ")
        self.fixed.put(self.DiagButton,180,600-100)
        self.DiagButton.connect("clicked", self.ButtonClicked)

    def exit_button_clicked(self, widget):
        exit()

    def ButtonClicked (self, widget):
        self.SoccerBall.show()
        self.PtrJ = 0
        while self.PtrJ < 10 :
            time.sleep(0.3)
            self.PtrJ += 1
            self.fixed.move(self.SoccerBall,self.PtrJ*60,1)
            self.SoccerBall.show()
        return
#---------------------------------------------------------------------------------------
MainWin = MWin()
MainWin.show_all()
Gtk.main()
In gui programming, while loops and sleeps block the gui mainloop which stops the updating of the gui.
This is probably why you only see what happened before the while loop and what happened after it.

I don't know pygtk  but I found this Threads/Concurrency with Python and the GNOME Platform
FWIW, I did try it using a GObject.timeout_add (every 300ms) and the result was the same. I'll look at this some more in the AM.
I wasn't aware of the blocking during while loops. The logic behind that escapes me. It's a little disheartening to find out I should have split my application into threads after a large portion is already written. One of the hazards of learning a new language. :( Is there a place that has a tutorial on proper program structure for gui based python applications?
I tried this in my code:
self.queue_draw()

And it seems to be what I was looking for, kind of a super self.show_all().  When I run it the screen is updated.

I also found the following (haven't tested):

while gtk.events_pending():
      gtk.main_iteration(False)