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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值