Function Call from different source has different response

Hi all,

62 and trying to learn some python coding.

Currently trying to play with tkintermapview for a flight simulator jobs display feature using data from an online flight simulator addon called FS Economy. This map resides in a map class I created and display in a tkinter notebook tab/page. I pull jobs from there site and show the job airports and job paths on the map. Because there are different types of jobs, I create all jobs on the map at once and set there state to hidden. I have three buttons , one for each type of job. When you select a type, a function in the map page is called that loops through each map path and airport which resets the state according to a job type variable set by clicking the buttons. This is working really well. Selecting the different buttons changes the airports and path state almost instantly. But like all things…There’s a butt…
I noticed that if an airport was outside the map view area, it did not draw the map image(icon), so that if you zoomed out, the path was there but not the airport image. If I toggle the button, it instantly redraws and the airports are there. (not sure if redraw is the right term, basically resetting the state for each item)

In the notebook class, I have a 1 second loop running in a thread that works with a message function to remove messages every ten seconds. I decided to try and use this loop to check the map zoom status and if the zoom level changed, I would call the same function that the buttons call to retrigger the airports and path.

For some reason I cannot figure out, this does what I want but the results are slow. Pressing the button is instantaneous, calling the function from the loop takes 2 to 3 seconds, (you can literally watch the airports repopulate on the map) each time the zoom level changes.

Can anyone explain why I get such a different result from the loop calling the function?


1 Like

Sounds like a cool project. Even with the source code, it may need performance profiling tools to debug.

My general rule of thumb, is that when a function behaves differently between calls, it’s impure, and some external state that the function result depends on has changed between calls.

Pure Functions are one reason why Functional Programming is powerful (I’m not saying FP’s a Panacea, just that some its concepts, especially Pure Functions, are really useful for debugging).

1 Like

For almost all GUI frameworks, you should not access GUI stuff directly from a thread that didn’t create it. I am surprised this works at all, I would have expected it to throw an error.

Instead, the background thread should tell the GUI to redraw, for example via the approach described in this SO post (which also shows the error message I would have expected based on your description)

However, even if you get this approach to work, it would probably be better to not use a different thread for this at all. Instead you could for example use .after(1000, check_scale), which would go the work directly in the gui thread, or figure out of tkinterstreetview provides something to hook into their scaling process (for example a virtual event).

Also, surely others have encountered this problem as well? Have you searched their bug tracker?


Following your provided link:

  1. I created a loop within my map view class and moved my test for zoom change to this thread:

  2. Created the event generator and listener items. The generator is in the loop and triggers on any zoom change. The listener function in the map view class catches the event and calls the same function as the buttons.

Works GREAT.

(This could be useful in many places)

Thank you so much.