python怎么启动线程_如何在Python中同时启动线程

我想做一个小程序,播放一首歌和一个图像弹出时,鼠标光标移动。我有3个功能,可用于3个操作,我想同时运行它们,但我无法完成。你能帮帮我吗?在import random

import threading

import pyautogui

import pygame

from tkinter import *

def play_song():

file = ‘Troll_Song.ogg’

pygame.mixer.init()

pygame.mixer.music.load(file)

pygame.mixer.music.play()

while pygame.mixer.music.get_busy():

pygame.time.Clock().tick(10)

def create_window():

while True:

root = Tk()

root.title(‘Trololo…’)

photo = PhotoImage(file=’trollface.gif’)

label = Label(root, image=photo)

label.pack()

w = 620 # width for the Tk root

h = 620 # height for the Tk root

get screen width and height

ws = root.winfo_screenwidth() # width of the screen

hs = root.winfo_screenheight() # height of the screen

random positions of the window

x = random.randint(0, ws – 620)

y = random.randint(0, hs – 620)

set the dimensions of the screen

and where it is placed

root.geometry(‘%dx%d+%d+%d’ % (w, h, x, y))

root.mainloop()

def mouse_move():

width, height = pyautogui.size()

while True:

x = random.randint(0, width)

y = random.randint(0, height)

pyautogui.moveTo(x, y, duration=0.3)

if name == ‘main‘:

t1 = threading.Thread(target=create_window())

t2 = threading.Thread(target=play_song())

t3 = threading.Thread(target=mouse_move())

t1.start()

t2.start()

t3.start()

Original: https://blog.csdn.net/weixin_39760368/article/details/113669627
Author: weixin_39760368
Title: python怎么启动线程_如何在Python中同时启动线程

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783752/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球