Python线程:“RuntimeError:thread .__ init __()not called”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python线程:“RuntimeError:thread .__ init __()not called”相关的知识,希望对你有一定的参考价值。

我正和乌龟打乒乓球比赛。但是,赋值是代码必须具有多线程。我无法正确应用多线程。

代码在编辑器中没有显示错误,但它不起作用。我们如何修复此运行时错误?当编辑器中没有错误时很难找到错误。

import turtle
from threading import *
from time import sleep

wn = turtle.Screen()
wn.title("Ping pong by Cagatay em")
wn.bgcolor("blue")
wn.setup(width=900, height=600)
wn.tracer(0)  #oyunu hizlandirir silersen cok yavaslar 

class PaddleFirst(Thread):
    def __init__(self):
        self.pen = turtle.Turtle()
        self.pen.penup()
        self.pen.speed(0)
        self.pen.shape("square")
        self.pen.shapesize(stretch_wid=5, stretch_len=1)
        self.pen.penup()
        self.pen.goto(-350, 0)

    def run(self):
        y = self.pen.ycor()
        y += 20
        self.pen.sety(y)
        k = self.pen.ycor()
        k -= 20
        self.pen.sety(k)


class PaddleSecond(Thread):
    def __init__(self):
        self.pen = turtle.Turtle()
        self.pen.penup()
        self.pen.speed(0)
        self.pen.shape("square")
        self.pen.shapesize(stretch_wid=5, stretch_len=1)
        self.pen.penup()
        self.pen.goto(350, 0)

    def run(self):
        y = self.pen.ycor()
        y += 20
        self.pen.sety(y)
        k = self.pen.ycor()
        k -= 20
        self.pen.sety(k)



class Ball(Thread):
    def __init__(self):

        self.pen = turtle.Turtle()
        self.pen.penup()
        self.pen.speed(0)
        self.pen.shape("circle")
        self.pen.color("red")
        self.pen.penup()
        self.pen.goto(0, 0)
        self.pen.dx = 00.1
        self.pen.dy = 00.1

    def run(self):
        self.pen.setx(self.pen.xcor() + self.pen.dx)
        self.pen.sety(self.pen.ycor() + self.pen.dy)
        if self.pen.ycor() > 290:
            self.pen.sety(290)
            self.pen.dy *= -1

        if self.pen.ycor() < -290:
            self.pen.sety(-290)
            self.pen.dy *= -1

        if self.pen.xcor() > 390:
            self.pen.goto(0, 0)
            self.pen.dx *= -1

        if self.pen.xcor() < -390:
            self.pen.goto(0, 0)
            self.pen.dx *= -1


class Wall(Thread):
   def run(self):
        if ball.pen.xcor() > 340 and (ball.pen.ycor() < paddle2.pen.ycor() + 40 and ball.pen.ycor() > paddle2.pen.ycor() - 40):
           ball.pen.dx *= -1
        if ball.pen.xcor() < -340 and (ball.pen.ycor() < paddle1.pen.ycor() + 40 and ball.pen.ycor() > paddle1.pen.ycor() - 40):
            ball.pen.dx *= -1


paddle1 = PaddleFirst()

paddle2 = PaddleSecond()

ball = Ball()

wall = Wall()


wn.listen()
wn.onkeypress(paddle1.run, "w")
wn.onkeypress(paddle1.run(), "s")
wn.onkeypress(paddle2.run(), "Up")
wn.onkeypress(paddle2.run, "Down")



while True:
    wn.update() # everytime uptades the screen
    ball.start()
    sleep(0.2)
    wall.start()
    sleep(0.2)
    paddle1.start()
    sleep(0.2)
    paddle2.start()
答案

我的猜测是你得到的错误信息是由于你没有在你的__init__()子类中调用超类'Thread方法。

当编辑器中没有错误时很难找到错误。

你需要学习调试的艺术。

我无法正确应用多线程。

我认为这里有两个问题。首先,要使用在tkinter上构建的turtle线程,你需要让所有图形命令通过主线程进行路由 - 辅助线程不应该尝试直接修改屏幕。

其次,你的代码很乱。除了线程之外,你不清楚你理解对象编程,因为你有两个相同的类,除了X坐标。这应该是一个带有初始化器参数的类。由于wn.onkeypress(paddle1.run(), "s")永远不会起作用,所以不清楚你是不是龟。你可能不应该在你的代码工作之前搞乱tracer()update()。你不应该在主龟线上有一个while True:

我把你的代码拆开了,然后把它放在一起。只有球是一个单独的线程,(单)桨类现在继承自Turtle。主线程处理来自球线程的图形命令以及标准的乌龟事件:

from turtle import Screen, Turtle
from threading import Thread, active_count
from queue import Queue

QUEUE_SIZE = 1

class Paddle(Turtle):
    def __init__(self, xcor):
        super().__init__(shape='square')

        self.shapesize(stretch_wid=5, stretch_len=1)
        self.speed('fastest')
        self.penup()
        self.setx(xcor)
        self.dy = 10

    def down(self):
        y = self.ycor() - self.dy

        if y > -250:
            self.sety(y)

    def up(self):
        y = self.ycor() + self.dy

        if y < 250:
            self.sety(y)

class Ball(Thread):
    def __init__(self):
        super().__init__(daemon=True)

        self.pen = Turtle('circle')
        self.pen.speed('fastest')
        self.pen.color('red')
        self.pen.penup()
        self.dx = 1
        self.dy = 1

    def run(self):
        x, y = self.pen.position()

        while True:
            x += self.dx
            y += self.dy

            if x > 330 and (paddle2.ycor() - 60 < y < paddle2.ycor() + 60):
                self.dx *= -1
            elif x < -330 and (paddle1.ycor() - 60 < y < paddle1.ycor() + 60):
                self.dx *= -1

            if y > 290:
                y = 290
                self.dy *= -1
            elif y < -290:
                y = -290
                self.dy *= -1
            elif not -440 < x < 440:
                x, y = 0, 0
                self.dx *= -1

            actions.put((self.pen.setposition, x, y))

def process_queue():
    while not actions.empty():
        action, *arguments = actions.get()
        action(*arguments)

    if active_count() > 1:
        screen.ontimer(process_queue, 100)

screen = Screen()
screen.title("Ping pong rework by cdlane")
screen.bgcolor('blue')
screen.setup(width=900, height=600)

actions = Queue(QUEUE_SIZE)

paddle1 = Paddle(-350)
paddle2 = Paddle(350)
ball = Ball()

screen.onkeypress(paddle1.up, 'w')
screen.onkeypress(paddle1.down, 's')
screen.onkeypress(paddle2.up, 'Up')
screen.onkeypress(paddle2.down, 'Down')
screen.listen()

ball.start()

process_queue()

screen.mainloop()

代码仍然是不完整的并且有点错误(例如,没有干净地关闭) - 你可以继续工作的东西。但它基本上以合理的方式玩游戏。

以上是关于Python线程:“RuntimeError:thread .__ init __()not called”的主要内容,如果未能解决你的问题,请参考以下文章

python多线程并发数量控制

python 多线程

什么是Python线程?Python线程如何创建?

python每个线程消费不用数据

Python 线程创建和传参

python多线程作用