简体   繁体   English

Python:如何创建多个线程同时运行

[英]Python: how to create multiple threads running at the same time

I am creating a game using Python and tkinter, and when I tried to add some music, the function making the music stopped the rest of the script (and also the window). I am creating a game using Python and tkinter, and when I tried to add some music, the function making the music stopped the rest of the script (and also the window). So I need to create a new thread I think to play the audio and keep the window working...所以我需要创建一个我认为播放音频并保持 window 工作的新线程......

I tried some things making errors, not running, or stopping the window, I currently have:我尝试了一些出错、不运行或停止 window 的方法,我目前有:

window.after(2000,showText,"Text 1")
window.after(2000,playSound,"Sound 1.wav")
window.after(7000,showText,"Text 2")

Important detail: the playSound function returns nothing, so nothing needs to wait for it.重要细节:playSound function 不返回任何内容,因此无需等待。 I also don't know much about programming in Python, I prefer using LUAU.我对Python中的编程也不太了解,我更喜欢使用LUAU。

Try using the module threading .尝试使用模块threading I found this article very useful for understanding the core concepts and setting up my first multithreaded program我发现这篇文章对于理解核心概念和设置我的第一个多线程程序非常有用

I found another article telling exactly how to make it work and it now works.我发现另一篇文章确切地说明了如何使它工作,现在它可以工作了。 The article is probably in french, but I give the link: http://www.xavierdupre.fr/app/teachpyx/helpsphinx/c_parallelisation/thread.html这篇文章可能是法语的,但我给出了链接: http://www.xavierdupre.fr/app/teachpyx/helpsphinx/c_parallelisation/thread.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM