在Python3中,如何在睡眠期间忽略用户输入?

在Python3中,如何在睡眠期间忽略用户输入?,python,input,tkinter,python-idle,Python,Input,Tkinter,Python Idle,我是编程新手,在我正在制作的一个小型RPG游戏中遇到了这个问题。它主要基于文本,但使用tkinter作为简单的GUI 在战斗中,我有一个for循环,它通过所有参与战斗的生物,一个接一个地执行它们的动作,每个动作之间都有小睡眠。我的问题是,当用户按下键或单击按钮时,命令被缓冲并在for循环完成后执行。有没有一种简单的方法可以在循环进行时忽略用户命令?我尝试过解除键绑定和禁用按钮,然后在循环完成后重新启用它们,但仍然不起作用。我也读过关于刷新用户输入的内容,但我搞不懂。因为我是个新手,我想我缺少一些

我是编程新手,在我正在制作的一个小型RPG游戏中遇到了这个问题。它主要基于文本,但使用tkinter作为简单的GUI

在战斗中,我有一个for循环,它通过所有参与战斗的生物,一个接一个地执行它们的动作,每个动作之间都有小睡眠。我的问题是,当用户按下键或单击按钮时,命令被缓冲并在for循环完成后执行。有没有一种简单的方法可以在循环进行时忽略用户命令?我尝试过解除键绑定和禁用按钮,然后在循环完成后重新启用它们,但仍然不起作用。我也读过关于刷新用户输入的内容,但我搞不懂。因为我是个新手,我想我缺少一些基本的概念

这是我的密码:

def battle_round(self, command):
    ''' (Battle, command) -> NoneType
    A round of battle where every creature gets an action.
    '''
    # Pause player input with mode variable.

    self.player.mode = 'wait'

    # Keep track of number of rounds.
    self.round_number += 1

    # Get initiative order for all mobs, party members, and player.
    ordered_creatures = self.initiative()

    # Get commands from each mob.
    for mob in self.mobs:
        mob.command = self.determine_mob_command()

    # Check battle speed option.
    delay = 1

    # Begin actions for all creatures.
    for creature in ordered_creatures:
        # Delay between actions. Write a space between lines.
        self.text_window.update_idletasks()
        self.write(self.text_window, '\n')
        time.sleep(delay)
        # Player action.
        if type(creature) == Player:
            if command == 'standard_attack':
                self.standard_player_attack()
            if command == 'retreat':
                self.retreat()

        if type(creature) == PartyMember:
            pass

        # MOB action.  
        if type(creature) == MOB:
            if creature.command == 'standard_attack':
                self.standard_mob_attack(creature)

    self.start_next_round()
我将Python3.2.3与TK8.5和IDLE 3.2.3一起使用。我的操作系统是Windows7

提前谢谢

编辑:感谢大家迄今为止的回复。我可能是在我的头以上,因为我甚至不知道线程是什么,直到现在,我不知道我将如何去阅读和忽略用户输入。至于用户输入的代码,我有很多。我将在此处复制并粘贴一些:

def attack():
    if player.in_battle == True:
        if player.mode != 'wait':
            player.current_battle.battle_round('standard_attack')

def retreat():
    if player.in_battle == True:
        if player.mode != 'wait':
            player.current_battle.battle_round('retreat')

# Set up battle buttons.
attack_button = Button(battle_button_frame, text='(A)ttack', command=attack,
                     width=10,)
attack_button.grid(column=1, columnspan=1, row=2, padx=5, pady=5)

retreat_button = Button(battle_button_frame, text='(R)etreat', command=retreat,
                     width=10,)
retreat_button.grid(column=2, columnspan=1, row=2, padx=5, pady=5)


battle_button_list = [attack_button, retreat_button]
例如,这些按钮可以让用户攻击选定的怪物,或者试图逃离战场

我还有几个键绑定:

# Bind Keys
root.bind('<Escape>', func=keyboard_cancel)

root.bind('<Control-Key-m>', func=keyboard_move_mode)
root.bind('<Control-Key-M>', func=keyboard_move_mode)
root.bind('<Control-Key-l>', func=keyboard_look_mode)
root.bind('<Control-Key-L>', func=keyboard_look_mode)
root.bind('<Control-Key-t>', func=keyboard_talk_mode)
root.bind('<Control-Key-T>', func=keyboard_talk_mode)

root.bind('<space>', func=keyboard_begin_battle)

root.bind('<Left>', func=arrow_key_select_left)
root.bind('<Right>', func=arrow_key_select_right)
#绑定密钥
root.bind(“”,func=keyboard\u cancel)
root.bind(“”,func=键盘移动模式)
root.bind(“”,func=键盘移动模式)
root.bind(“”,func=keyboard\u look\u mode)
root.bind(“”,func=keyboard\u look\u mode)
root.bind(“”,func=keyboard\u talk\u mode)
root.bind(“”,func=keyboard\u talk\u mode)
root.bind(“”,func=keyboard\u begin\u battle)
root.bind(“”,func=arrow\u key\u select\u left)
root.bind(“”,func=arrow\u key\u select\u right)

我的问题仍然是,当使用睡眠的for循环进行时,如果用户按下按钮或使用键绑定,它将在战斗回合结束后立即执行。(如果有6个怪物和玩家,大约7秒)我是一个完全的初学者,所以我很抱歉我的帖子不清楚,我的代码乱七八糟

对于那个循环使用线程怎么样?你的问题是关于处理用户输入,但是你粘贴了关于其他内容的代码部分,据我所知。据我猜测,通过阅读用户输入并忽略结果来“刷新”用户输入是正确的。也许你有一个bug,但是我们没有看到相应的代码。你可以通过简单地不使用
time.sleep
,而是在事件循环中使用
time.time
进行一些计算来避免这个问题。我相信
widget['state']='disabled'
足以使widget无响应。使用
“正常”
将其还原。