GithubHelp home page GithubHelp logo

.update() is considered to be harmful, try to reimplement with running asyncio loop in a separate thread about async-tkinter-loop HOT 5 CLOSED

insolor avatar insolor commented on July 19, 2024
.update() is considered to be harmful, try to reimplement with running asyncio loop in a separate thread

from async-tkinter-loop.

Comments (5)

insolor avatar insolor commented on July 19, 2024 1

@rdbende thank you, I used dooneevent in my code. I tried your idea of running the loop until dooneevent returns 0, but in reality dooneevent returns 0 most of the times, except when there are some pending events. So instead I used a nested while root.dooneevent(_tkinter.DONT_WAIT) > 0: loop to process all pending events in one iteration of the outer loop.

while True:
# Process all pending events
while root.dooneevent(_tkinter.DONT_WAIT) > 0:
pass
try:
root.winfo_exists() # Will throw TclError if the main window is destroyed
except TclError:
break
await asyncio.sleep(0.01)

from async-tkinter-loop.

rdbende avatar rdbende commented on July 19, 2024 1

So instead I used a nested while root.dooneevent(_tkinter.DONT_WAIT) > 0: loop to process all pending events in one iteration of the outer loop.

Actually, that is what I wanted to say :D

from async-tkinter-loop.

insolor avatar insolor commented on July 19, 2024

Experimental implementation, see separate_thread branch. It almost works as intended, but tests are faining (it seems that it's because pytest tries to run them in parallel), and I'm not happy that you need to switch asyncio events from a different thread, see start_stop_counter.py

from async-tkinter-loop.

rdbende avatar rdbende commented on July 19, 2024

This is how _tkinter.c implements the mainloop:

https://github.com/python/cpython/blob/f63002755d3b7c0bb2e452f158769f19fcf3b850/Modules/_tkinter.c#L2694-L2724

You can see that it uses the Tcl_DoOneEvent function in the mainloop. Apparently this function is exposed in Tkinter, so it might be useful if you want to avoid the problems caused by update.

A while ago I was thinking about implementing an asynchronous mainloop in Tukaan, and this is what I came up with (transferred to Tkinter):

async def _async_main_loop(root: tkinter.Tk) -> None:
    while root.is_alive:
        root.tk.dooneevent(_tkinter.DONT_WAIT)
        await asyncio.sleep(0)

Though using dooneevent at Python level is a bit slower than Tkinter's actual mainloop.

Edit: It might be an even better approach to run dooneevent until it returns 0 (zero means that there's no more events to process), this way it's almost as fast as an update call.

from async-tkinter-loop.

insolor avatar insolor commented on July 19, 2024

@rdbende ah, ok, now I see. It seems that I misread that statement, but in the end I implemented it in that way as you said.

from async-tkinter-loop.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.