张不识 2023-03-15 12:41 采纳率: 50%
浏览 130
已结题

python print无输出

pycharm中有时print不输出信息

目前只用着pycharm不知道这是python的print的本身bug,还是pycharm软件的bug。

发现:在执行 win32api.mouse_event(win32con.MOUSEEVENTF_MOVE,x, y) 同级别的外层函数中的 所有 print 语句无效。
并不是缓冲区的问题,print 语句加入flush=True也无效!
在main中调用函数,在游戏中的功能也已经实现,然而我想自己输出打印过程信息,检查了很久,最后确认是print 的问题或者与win32api.mouse_event不兼容?

以下是代码片段: 请各位帮忙,虽然也不影响操作游戏窗口,但是遇到无法解决的问题还是想要解决。

class ViewProcess:

    @staticmethod
    def turn_right(angle: int, time_to_wait=0.5):
        time.sleep(time_to_wait)
        x_move = int(angle / 360 * 4650)  # 在本机电脑原神窗口中,鼠标每移动4650旋转一周,不受图像质量影响。
        try:
            win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x_move, 0)
            print(f"向右或顺时针旋转{angle}度")
            ForeKeyboardController.press("w")
        except:
            print("旋转失败")

@staticmethod
    def change_player_direction(aim_angle: int, step=1, allowed_variance=5, allowed_times=2, print_flag=False):
        """
        仅采用向右旋转来矫正方向,采用视角旋转时旋转极快,不在乎向左旋转可能的时间节省。写成递归函数,将角度矫正到精度以内.
        目前发现BUG,如果采用了win32api调用,那么print函数就会暂时失效,即使print加了 flush=True也无效。
        :param step: 内部函数用于递归计数,当前递归次数
        :param print_flag: 是否打印过程信息
        :param allowed_variance: 允许的矫正角度差异
        :param aim_angle: 给定的需要旋转到的角度,正北方向为0,顺时针0-360。
        :param original_angle: 测算的当前游戏角度
        :return: None
        """
        original_angle = ArrowProcess.get_map_arrow_direction()
        angel_to_turn = aim_angle - original_angle
        if print_flag:
            print(f"step:{step},aim_angle:{aim_angle},original_angle:{original_angle},向右旋转视角{angel_to_turn}度。"
                  , flush=True)
        if angel_to_turn > allowed_variance:
            step += 1
            ViewProcess.turn_right(angel_to_turn)
            if step <= allowed_times:
                ViewProcess.change_player_direction(aim_angle, step, allowed_variance, print_flag)
        elif angel_to_turn < -allowed_variance:
            step += 1
            ViewProcess.turn_right(angel_to_turn + 360)
            if step <= allowed_times:
                ViewProcess.change_player_direction(aim_angle, step, allowed_variance, print_flag)
        else:
            return True

if __name__ == "__main__":

    genshin_top()
    start_dt = datetime.datetime.now()
    print("start_datetime:", start_dt)
    print("-" * 20)

    ViewProcess.change_player_direction(20, step=1, allowed_variance=5, allowed_times=2, print_flag=True)

    print("-" * 20)
    end_dt = datetime.datetime.now()
    print("end_datetime:", end_dt)
    print("time cost:", ((end_dt - start_dt).seconds * 1000 + (end_dt - start_dt).microseconds / 1000), "ms")
    # pycharm_activate()
    pass
  

img

  • 写回答

3条回答 默认 最新

  • MarkHan_ 2023-03-15 13:24
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ
    在代码中,当使用 win32api.mouse_event函数来模拟鼠标移动时,print语句的输出被阻塞了。这是因为当win32api.mouse_event函数被调用时,它会触发鼠标移动事件,进而将控制权交给 Windows 系统,直到移动事件处理完成为止。因此,print语句只能在移动事件处理完成后才会执行,这样会导致在移动鼠标期间打印的消息被推迟。
    ·
    为了解决这个问题,可以尝试将打印消息的代码与鼠标事件分开。将需要打印的消息存储到一个列表中,然后在事件处理完成后打印这个列表中的所有消息。这样可以确保打印消息的顺序不会受到事件处理的影响。
    ·
    下面是对代码做出的修改,修改后的代码会将打印消息存储在 message_list 中,然后在 win32api.mouse_event 函数处理完成后输出所有消息:

    class ViewProcess:
     
        @staticmethod
        def turn_right(angle: int, time_to_wait=0.5, message_list=None):
            time.sleep(time_to_wait)
            x_move = int(angle / 360 * 4650)
            try:
                win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x_move, 0)
                if message_list is not None:
                    message_list.append(f"向右或顺时针旋转{angle}度")
                ForeKeyboardController.press("w")
            except:
                if message_list is not None:
                    message_list.append("旋转失败")
     
        @staticmethod
        def change_player_direction(aim_angle: int, step=1, allowed_variance=5, allowed_times=2, print_flag=False):
            original_angle = ArrowProcess.get_map_arrow_direction()
            angel_to_turn = aim_angle - original_angle
            message_list = []
            if print_flag:
                message_list.append(f"step:{step},aim_angle:{aim_angle},original_angle:{original_angle},向右旋转视角{angel_to_turn}度。")
            if angel_to_turn > allowed_variance:
                step += 1
                ViewProcess.turn_right(angel_to_turn, message_list=message_list)
                if step <= allowed_times:
                    ViewProcess.change_player_direction(aim_angle, step, allowed_variance, print_flag)
            elif angel_to_turn < -allowed_variance:
                step += 1
                ViewProcess.turn_right(angel_to_turn + 360, message_list=message_list)
                if step <= allowed_times:
                    ViewProcess.change_player_direction(aim_angle, step, allowed_variance, print_flag)
            else:
                return True
            if print_flag:
                for message in message_list:
                    print(message, flush=True)
     
    if __name__ == "__main__":
     
        genshin_top()
        start_dt = datetime.datetime.now()
        print("start_datetime:", start_dt)
        print("-" * 20)
     
        ViewProcess.change_player_direction(20, step=1, allowed_variance=5, allowed_times=2, print_flag=True)
     
        print("-" * 20)
        end_dt = datetime.datetime.now()
        print("end_datetime:", end_dt)
        print("time cost:", ((end_dt -start_dt).total_seconds()), "seconds")
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 已采纳回答 3月16日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分