运行时错误:线程只能在上启动

2024-04-20 10:53:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个简单的Python游戏。我试图修复以下错误:

RuntimeError: threads can only be started once

我尝试了.cancel()计时器,但似乎没有起作用,并且在执行计时器之前,我做了一个if语句,看看计时器.is_alive是否正常。控制台在ball_char = play_timer.start()处抛出错误。在

def playball(state):
 batbox = [["@", "@", "@", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "@", "@", "@"]]
 play = "playing"
 play_timer = Timer(1.0, pitch(batbox))
 end_timer = Timer(6.0, pitch_end(play))
 play_timer.cancel()
 end_timer.cancel()
 pstate = "idle"
 inning = 1
 outs = 0
 pscore =0
 cscore = 0
 strikes = 0
 ball_row = 0
 ball_col = 0
 ball_char = "."
 while play == "playing":
     batbox = [["@", "@", "@", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "@", "@", "@"]]
     os.system('cls')  # on windows
     os.system('clear') # on linux / os x
     # Playing the game
     print "Press enter to start / hit the pitch"
     print_grid(batbox)
     input = raw_input("")
     pstate = "hitting"
     # Hitting
     if pstate == "hitting":
         ball_row = random_row(batbox) 
         ball_col = random_col(batbox)
         end_timer.start()
         while pstate == "hitting":
             batbox[ball_row][ball_col] = ball_char
             if play_timer.is_alive():
                 play_timer.cancel()
             else:
                 ball_char = play_timer.start()
 else:
     play_timer.cancel()
     end_timer.cancel()
     state = "mainmenue"
     return state

Tags: playifcolstartcancelendrow计时器
1条回答
网友
1楼 · 发布于 2024-04-20 10:53:40

{(a1)是定时器的子类}:

start() Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

即使取消线程(或计时器),也不能再次调用start()。您需要创建一个新的线程/计时器对象,否则这是一个错误。在

相关问题 更多 >