Comments (5)
@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.
async-tkinter-loop/async_tkinter_loop.py
Lines 16 to 26 in 4c8e500
from async-tkinter-loop.
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.
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.
This is how _tkinter.c
implements the mainloop:
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.
@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)
- Correct way to make cancellation HOT 4
- Example idea: file downloader with a progressbar
- Check if ping.py normally decodes output on the Russian versions of Windows HOT 2
- Try to fix type annotations of the async_handler decorator according to an example from mypy
- TODO: better documentation
- Idea: support of alternative asynchronous libraries (like `trio`, `anyio` and others)
- Integration with alternative tkinter-like libraries
- Dialogs (such as messagebox) block async mainloop HOT 2
- best practise when I want to have a uvicorn server besides with tkinter gui HOT 1
- Can use in TaskGroup
- Add a readme file to the examples
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-tkinter-loop.