Python Forum
[pygtk] fixed.move not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pygtk] fixed.move not working as expected
#1
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()
Reply
#2
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
Reply
#3
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.
Reply
#4
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?
Reply
#5
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Creating hot spots in PyGTK DrakeSoft 1 1,606 Aug-01-2022, 04:35 PM
Last Post: Axel_Erfurt
  [PyGTK] How to center text on multi-line buttons? Lomax 3 4,230 Jan-23-2021, 03:23 PM
Last Post: Lomax
  [PyGTK] Creating Book Table ramazanemreosmanoglu 3 1,841 Oct-27-2020, 06:47 PM
Last Post: Axel_Erfurt
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 15,455 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020