Python 多线程永远无法运行,并且可以';我不知道发生了什么事

Python 多线程永远无法运行,并且可以';我不知道发生了什么事,python,multithreading,pygame,Python,Multithreading,Pygame,我有两个多线程,我的目标是使用这两个多线程分别打印“mouse1”和“mouse2”。但是这个程序不起作用。它不打印任何内容,也无法正确关闭 导入线程 导入pygame screen=pygame.display.set_模式((800800)) def mouse1(): 尽管如此: 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: pygame.quit() sys.exit() elif event.type==pygame.MOUS

我有两个多线程,我的目标是使用这两个多线程分别打印“mouse1”和“mouse2”。但是这个程序不起作用。它不打印任何内容,也无法正确关闭

导入线程
导入pygame
screen=pygame.display.set_模式((800800))
def mouse1():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.MOUSEBUTTONDOWN:
打印('mouse1')
其他:
通过
def mouse2():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.MOUSEBUTTONDOWN:
打印('mouse2')
其他:
通过
t2=线程。线程(目标=鼠标1)
t1=线程。线程(目标=鼠标2)
t1.start()
t2.start()
t1.join()
t2.join()

我希望当点击鼠标按钮时,输出的是许多“mouse1”和“mouse2”。

给你。为了区分按钮,我从中复制了示例,并按照示例进行了操作

import sys, pygame
pygame.init()

LEFT_MOUSE_BUTTON = 1
MIDDLE_MOUSE_BUTTON = 2
RIGHT_MOUSE_BUTTON = 3

def handleMouseEvents(event):
    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == LEFT_MOUSE_BUTTON:
            print("Left mouse button clicked!")
        elif event.button == MIDDLE_MOUSE_BUTTON:
            print("Middle mouse button clicked!")
        elif event.button == RIGHT_MOUSE_BUTTON:
            print("Right mouse button clicked!")

        sys.stdout.flush()
    else:
        pass

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("intro_ball.gif")
ballrect = ball.get_rect()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        else:
            handleMouseEvents(event)

    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()
导入系统,pygame
pygame.init()
鼠标左键=1
鼠标中键=2
鼠标右键=3
def handleMouseEvents(事件):
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==鼠标左键:
打印(“单击鼠标左键!”)
elif event.button==鼠标中键:
打印(“单击鼠标中键!”)
elif event.button==鼠标右键:
打印(“单击鼠标右键!”)
sys.stdout.flush()
其他:
通过
尺寸=宽度,高度=320240
速度=[2,2]
黑色=0,0,0
screen=pygame.display.set_模式(大小)
ball=pygame.image.load(“intro_ball.gif”)
ballrect=ball.get_rect()
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
其他:
handleMouseEvents(事件)
ballrect=ballrect.移动(速度)
如果ballrect.left<0或ballrect.right>宽度:
速度[0]=-速度[0]
如果ballrect.top<0或ballrect.bottom>高度:
速度[1]=-速度[1]
屏幕填充(黑色)
屏幕。blit(球、球)
pygame.display.flip()
我解决的主要问题是:

  • 在打印到输出后添加了对sys.stdout.flush()的调用。否则,在程序结束并自动刷新输出之前,您不会看到输出
  • 添加了实际设置显示区域并在其上显示内容的示例代码
  • 选中
    event.button
    ,查看正在单击哪个按钮
  • 把穿线的东西处理掉了。Pygame有自己的线程系统,除非您真的知道自己在做什么,否则请遵循它

您需要将
intro_ball.gif
添加到源代码中,以及此代码所在的文件中。我从

两个事件线程中得到了它,您试图实现什么?线程可能无法访问主线程中的事件。您可以在主线程中运行循环,并使用
队列将事件发送到线程
代码在Linux上对我有效,但Python中的线程不能同时工作,因此每次单击都只能被一个线程捕获(活动)线程,所以我随机得到“mouse1”或“mouse2”。