Calling a thread within a module in python
Question: I have a log in screen which calls a module from a file which is
'HomescreenscrollCopy31' . This works perfectly fine.

The only issue is that there is a thread within the module which does not seem
to operate.

Here is the code in the login screen:

    
    
    def login():
        content = nameentry.get()
        content1 = IDentry.get()
        if content == "1" and content1 == "1":
            root.destroy()
            from HomescreenscrollCopy31 import FullScreenApp
    

The HomescreenscrollCopy31.py file looks like this,

    
    
    class FullScreenApp(object):
         def __init__(self, master, **kwargs):
            self.master=master
            pad=3
            self._geom='200x200+0+0'
            master.geometry("{0}x{1}+0+0".format(
                master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
            master.bind('<Escape>',self.toggle_geom)            
        def toggle_geom(self,event):
            geom=self.master.winfo_geometry()
            print(geom,self._geom)
            self.master.geometry(self._geom)
            self._geom=geom
    
        def trick(threadName, sleepTime):
            while 1 < 2:
                .....
    
        try:
            t = threading.Thread(target=trick, args=("Trick running", 5))
            t.daemon = True
            t.start()
        except: Exception,
        print "start up"
    

It seems that it runs through the HomescreenscrollCopy31.py and politely
prints the "start up" only to tease me, as it never actually runs the thread
and thus should print the "Trick running" output.

Why is this?


Answer: I think it's better to write it this way.

    
    
    def login():
        content = nameentry.get()
        content1 = IDentry.get()
        if content == "1" and content1 == "1":
            root.destroy()
            from HomescreenscrollCopy31 import FullScreenApp
            myFullScreenApp = FullScreenApp(...)  # you need to give it some input, like master, to initialize an instance for FullScreenApp class
    
    
    
    
    class FullScreenApp(object):
    
        def __init__(self, master, **kwargs):
            self.master=master
            pad=3
            self._geom='200x200+0+0'
            master.geometry("{0}x{1}+0+0".format(
                master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
            master.bind('<Escape>',self.toggle_geom)   
    
            # these code will be executed when instance initializing 
            t = threading.Thread(target=self.trick, args=["Trick running", 5])
            t.daemon = True
            t.start()
    
    
        def toggle_geom(self,event):
            geom=self.master.winfo_geometry()
            print(geom,self._geom)
            self.master.geometry(self._geom)
            self._geom=geom
    
        def trick(threadName, sleepTime):
            while 1 < 2:
                .....
    

  1. You just need to initialize an instance of the class to get thread running. If using staticmethod way, it will make code more complicated which I don't recommend.
  2. When invoking a new thread, args should be a list([]) but not a tuple(()).
  3. You can use while 1 instead of while 1<2\. Anyway, this is not the major issue. :)

Hope it helps.

