Forum: Poser Python Scripting


Subject: more on: Running multiple Tkinter scripts.

tromnek opened this issue on Jan 21, 2005 ยท 12 posts


tromnek posted Fri, 21 January 2005 at 3:46 PM

I did some more work on this. It turns out that the real curlprit is Tkinter.mainloop(). I found some stuff on this topic and the best approach for my purpose is to hijack Tkinter's event loop. Here's what I currently have;

<br></br>class prpcdApp:<br></br>    """Your typical application class<br></br>    """<br></br>    def __init__(self, master, textMessage):<br></br>(snip..rest.of.init...)<br></br>    def Update(self):<br></br>      if self.scene: self.scene.ProcessSomeEvents()<br></br>      self.master.lift()<br></br>      self.master.after(50, self.Update)<br></br>(snip..rest.of.class...)<br></br><br></br>#--- Class to Hijack and Restore Tkinters event loops ---<br></br>class NewtMainLoop:<br></br>  """Class to hijack Tkinter and Tkinter.Misc mainloop()<br></br>  """<br></br>  import Tkinter<br></br>  def __init__(self, master):<br></br>      pass<br></br>      self.master = master<br></br><br></br>  def mainloop(self):<br></br>      if self.AmINewt():                        # Return if we are currently hijacked<br></br>       return<br></br>      self.master.after(75, self.ChangeToNewt ) # Hijack Tkinter's event loops, but wait a moment ...<br></br>      self.master.mainloop()                    #   until we run the 'real' event loop.<br></br>      self.GetBetter()                          # Restore the 'real' event loop<br></br><br></br>  def AmINewt(self):<br></br>      if hasattr( Tkinter, 'IGotBetterMainLoop' ):<br></br>        if not Tkinter.IGotBetterMainLoop == None:<br></br>          return 1<br></br>      return 0<br></br><br></br>  def ChangeToNewt(self):<br></br>      # Check if the mainloop() is currently hijacked, if so return.<br></br>      if self.AmINewt():<br></br>        print "I'm already a newt."<br></br>        return<br></br><br></br>      # Save Tkinter's default event loops for later restoration.<br></br>      Tkinter.IGotBetterMainLoop          = Tkinter.mainloop<br></br>      Tkinter.Misc.IGotBetterMiscMainLoop = Tkinter.Misc.mainloop<br></br><br></br>      # Our dummy event loops.<br></br>      def ChangeToNewtMainLoop(n=0):<br></br>        pass<br></br>        print 'call to mainloop()'<br></br>      def ChangeToNewtMiscMainLoop(self, n=0):<br></br>        pass<br></br>        print 'call to Misc.mainloop()'<br></br><br></br>      # Hijack the event loops.<br></br>      Tkinter.mainloop      = ChangeToNewtMainLoop<br></br>      Tkinter.Misc.mainloop = ChangeToNewtMiscMainLoop<br></br>      print "She turned me into a newt."<br></br><br></br>  def GetBetter(self):<br></br>      # Restore Tkinter's default event loops<br></br>      Tkinter.mainloop      = Tkinter.IGotBetterMainLoop<br></br>      Tkinter.Misc.mainloop = Tkinter.Misc.IGotBetterMiscMainLoop<br></br>      print "I Got Better."<br></br><br></br>      # Empty our stuff so we can use it for a logic test.<br></br>      Tkinter.IGotBetterMainLoop          = None<br></br>      Tkinter.Misc.IGotBetterMiscMainLoop = None<br></br><br></br>#--- Activate the loop ---------------<br></br>prpcd = prpcdApp(Tkinter.Tk(), '')      # Create our app, pass Tkinter.Tk() to self.master<br></br>prpcd.Update()<br></br>NewtMainLoop(prpcd.master).mainloop()   # Hijack and run mainloop(), pass a Tkinter.Tk() instance<br></br>print "Left mainloop() in main"         #   so it can call Tk().after() and Tk().mainloop()<br></br><br></br>#--- End Tk loop ------ --------------<br></br>

Let me know what you folks think.