Skip to content

SeparateChat

独立聊天窗口,继承自 ChatWeixinWindow,同时具备聊天操作和窗口管理能力。

继承关系

SeparateChat → Chat + WeixinWindow

SeparateChat 拥有 Chat 的所有消息发送、读取、群管理方法,以及 WeixinWindow 的窗口操作方法。

构造

通常不直接构造,而是通过以下方式获得:

python
# 通过 add_chat_listen 获得
chats = wx.add_chat_listen(["张三"])
separate_chat = chats[0]

# 通过 separate 方法获得
chat = wx.chat_with("张三")
separate_chat = chat.separate()

# 通过 SessionItem 双击获得
separate_chat = wx.session.separate_by_click("张三")

窗口操作(继承自 WeixinWindow)

python
separate_chat.activate()       # 激活窗口
separate_chat.minimize()       # 最小化
separate_chat.maximize()       # 最大化
separate_chat.restore()        # 还原
separate_chat.close()          # 关闭窗口
separate_chat.pin()            # 窗口置顶
separate_chat.unpin()          # 取消置顶
separate_chat.move_to(x, y)    # 移动窗口
separate_chat.resize_to(w, h)  # 调整大小
separate_chat.move_offscreen() # 移到屏幕外
separate_chat.move_back()      # 移回原位

窗口属性(继承自 WeixinWindow)

属性/方法类型说明
exists_windowbool窗口是否存在
is_topmostbool是否置顶
is_minimizedbool是否最小化
is_maximizedbool是否最大化
is_visible()bool是否可见
is_offscreenbool是否在屏幕外
rectTuple[int,int,int,int]窗口矩形 (left, top, right, bottom)
sizeTuple[int,int]窗口大小 (width, height)
positionTuple[int,int]窗口位置 (x, y)

聊天操作(继承自 Chat)

所有 Chat 类的方法均可使用:

python
# 发送消息
separate_chat.send_text("你好")
separate_chat.send_file("C:/doc.pdf")

# 读取消息
messages = separate_chat.get_visible_messages()

# 群管理(如果是群聊)
separate_chat.set_room_name("新群名")

在消息监听中使用

消息监听回调中的 chat 参数就是 SeparateChat 实例:

python
@wx.on(Event.TEXT)
def on_text(weixin, chat, message):
    # chat 是 SeparateChat 实例
    chat.send_text("收到")
    print(chat.chat_name)

示例

python
from pywxauto import Weixin

wx = Weixin(background=True, offscreen=True)

# 打开独立窗口
chats = wx.add_chat_listen(["客服群", "张三"])

for chat in chats:
    print(f"{chat.chat_name} - 窗口位置: {chat.position}")
    chat.send_text("机器人已上线")