0

ok so i have a tkinter application which uses a backend function from an external script. This script is working with large datasets and so it takes a good 15-20 seconds to compute although after the initial loading there is no delay.

The problem is that when i import this function to my tkinter GUI script it takes a while to launch as it is loading the module ofcourse.

I was wondering if there was either a way to launch my GUI first and then load the module while the application is running or lauch a loading screen while the module is importing.

any useful links will do, i have tried messing about with constructors in the external script but in that script i have a bunch of code outside any functions as these are required by all the functions in that script.

Thanks in advance for any advice.

Code:

from Content import print_similar_animes # functions from external script
from Content import get_id_from_partial_name


#These functions are within a class and are only 
                        #called when a button is  clicked.

def GetPartial(self):  
    partial=self.NameEntry.get()
    get_id_from_partial_name(partial).

def Rec(self):
    user_anime=self.NameEntry2.get()
    print_similar_animes(user_anime)
Moller Rodrigues
  • 780
  • 5
  • 16

2 Answers2

1

It looks you coupled data loading with UI building. They don't have to be like that. Decouple it, make it load after the click of a button only, or use after() to delay or to load small chunks of data. Or use threads some will say. See here for after method.

progmatico
  • 4,714
  • 1
  • 16
  • 27
  • I have updated my post with some code, the functions should only be executed after a button is clicked. I tried adding .after(xtime) after the functions but that doesnt work – Moller Rodrigues Feb 12 '18 at 16:46
  • @MollerRodrigues Well it will not work by itself, I mean it will register a widget callback to be called after sometime, and can be re-registered in the callback to repeat, so it can be used for background processing, but the rest of the the work you will have to code it, it allows opportunity for the gui to refresh or for other things to be done. Your own answer is both ingenous and fine, because it is actually a way of doing what you asked for. Although I don't like it much for frequent use though to the import location in the code, but for problematic slow imports it is a solution. – progmatico Feb 13 '18 at 14:39
  • Thanks for your help m8, in hindsight a one off 15 second load time is not that unrealistic tbh taking into consideration the data sets i am using. – Moller Rodrigues Feb 14 '18 at 01:21
1

Haha i solved it. To delay an import until the class is instantiated, the import must be inside a method.

LIKE SO:

def GetPartial(self): 
    from Content import get_id_from_partial_name #import here 
    partial=self.NameEntry.get()
    get_id_from_partial_name(partial).

def Rec(self):
    from Content import print_similar_animes # import here
    user_anime=self.NameEntry2.get()
    print_similar_animes(user_anime)

NOTE - Putting the import inside the class but outside of any method still causes the import to occur when the module is initialized.

Moller Rodrigues
  • 780
  • 5
  • 16