python deiconify and show is not working with threading

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

python deiconify and show is not working with threading

Peter Irbizon
Hello, 
please in function topko there are deiconify() and show() in my code but it will not show my window. Please how can should I change it?

my code:
# -*- coding: utf-8 -*-
import sys       
import traceback  
import os 
import threading
import psutil 

def we_are_frozen():
  return hasattr(sys, "frozen")

def module_path():
  encoding = sys.getfilesystemencoding()
  if we_are_frozen():
    return os.path.dirname(unicode(sys.executable, encoding))
  return os.path.dirname(unicode(__file__, encoding))
  
os.environ['LANGUAGE'] = "en" 
import elib.intl                 
import pygtk
pygtk.require("2.0")
import gtk
import gobject
import urllib2
import datetime
import time, platform
from _winreg import *
import warnings 
warnings.simplefilter('ignore', Warning)


def hooo(err, ret=None, error=''): 
  print traceback.format_exc()
  print err
  while gtk.events_pending():
    gtk.main_iteration()
  gtk.gdk.flush()
  gtk.threads_leave() 
  topko()
  topko()

def topko(): 
  app.okno.deiconify()
  gdkwin = app.okno.window
  gdkwin.set_keep_above(True) 
  gdkwin.set_keep_above(False)
  gdkwin.show()  

class jj:
    def __init__(self):

        local_path=os.path.join(module_path(), "lg")   
        elib.intl.install('translation', local_path)     
        self.okno = gtk.Window(gtk.WINDOW_TOPLEVEL) 
        self.okno.iconify()
        self.okno.resize(400,50)
        self.okno.show_all()
                                   
        #self.xx()

    def play(self):
        threading.Thread(target=self.xx).start()
    
    def xx(self):
      db="a.s"
      if os.path.isfile(db.decode('utf8').replace('\\','/')) == False: 
        hooo('5','')  
        return False  

if __name__ == "__main__":
    gtk.threads_init()
    app = jj()
    app.play()
    
    gtk.threads_enter() 
    gtk.main()
    gtk.threads_leave() 

_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: python deiconify and show is not working with threading

Tim Evans-2
On 2013-10-18 07:17, Peter Irbizon wrote:
> Hello,
> please in function topko there are deiconify() and show() in my code but
> it will not show my window. Please how can should I change it?

 From the import of _winreg I can tell you're working on Windows.

On Windows, you cannot interact with gtk widgets from a background
thread. It will just lock up your app, or not work in other ways. This
is caused by the underlying win32 "HWND" belonging to the main thread;
operating on it from another thread needs to be done differently and
carefully and GTK (rightly IMO) doesn't try to do that for you.

The best way to fix this is to do everything GTK-related in the main
thread. The easiest way to do that is via glib.idle_add().

     def update_the_ui():
         with gtk.gdk.lock:
             # put code here that interacts with the UI
             return False

     def function_in_another_thread():
         glib.idle_add(update_the_ui)

Unlike other GTK functions, glib.idle_add() *is* thread safe, so you can
call it from anywhere. The function you pass it will then run in the
main thread and can interact with GTK with no problems.

--
Tim Evans
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/