mirror of
https://github.com/smallevilbeast/ntchat.git
synced 2025-07-07 12:56:07 +08:00
新增cmd窗口关闭例子
This commit is contained in:
parent
4391ad51b1
commit
8fc2592926
34
examples/close_log1.py
Normal file
34
examples/close_log1.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
os.environ['NTCHAT_LOG'] = "ERROR"
|
||||
|
||||
import ntchat
|
||||
|
||||
wechat = ntchat.WeChat()
|
||||
|
||||
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||
wechat.open(smart=True)
|
||||
|
||||
|
||||
# 注册消息回调
|
||||
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
|
||||
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
|
||||
data = message["data"]
|
||||
from_wxid = data["from_wxid"]
|
||||
self_wxid = wechat_instance.get_login_info()["wxid"]
|
||||
room_wxid = data["room_wxid"]
|
||||
|
||||
# 判断消息不是自己发的并且不是群消息时,回复对方
|
||||
if from_wxid != self_wxid and not room_wxid:
|
||||
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")
|
||||
|
||||
|
||||
# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
|
||||
try:
|
||||
while True:
|
||||
time.sleep(0.5)
|
||||
except KeyboardInterrupt:
|
||||
ntchat.exit_()
|
||||
sys.exit()
|
33
examples/close_log2.py
Normal file
33
examples/close_log2.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import sys
|
||||
import time
|
||||
import logging
|
||||
logging.disable(logging.INFO)
|
||||
|
||||
import ntchat
|
||||
|
||||
wechat = ntchat.WeChat()
|
||||
|
||||
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||
wechat.open(smart=True)
|
||||
|
||||
|
||||
# 注册消息回调
|
||||
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
|
||||
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
|
||||
data = message["data"]
|
||||
from_wxid = data["from_wxid"]
|
||||
self_wxid = wechat_instance.get_login_info()["wxid"]
|
||||
room_wxid = data["room_wxid"]
|
||||
|
||||
# 判断消息不是自己发的并且不是群消息时,回复对方
|
||||
if from_wxid != self_wxid and not room_wxid:
|
||||
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")
|
||||
|
||||
|
||||
# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
|
||||
try:
|
||||
while True:
|
||||
time.sleep(0.5)
|
||||
except KeyboardInterrupt:
|
||||
ntchat.exit_()
|
||||
sys.exit()
|
47
examples/cmd_close_event.py
Normal file
47
examples/cmd_close_event.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
import time
|
||||
import ntchat
|
||||
try:
|
||||
import win32api
|
||||
except ImportError:
|
||||
print("Error: this example require pywin32, use `pip install pywin32` install")
|
||||
sys.exit()
|
||||
|
||||
wechat = ntchat.WeChat()
|
||||
|
||||
# 打开pc微信, smart: 是否管理已经登录的微信
|
||||
wechat.open(smart=True)
|
||||
|
||||
|
||||
# 注册消息回调
|
||||
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
|
||||
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
|
||||
data = message["data"]
|
||||
from_wxid = data["from_wxid"]
|
||||
self_wxid = wechat_instance.get_login_info()["wxid"]
|
||||
|
||||
# 判断消息不是自己发的,并回复对方
|
||||
if from_wxid != self_wxid:
|
||||
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")
|
||||
|
||||
|
||||
def exit_application():
|
||||
ntchat.exit_()
|
||||
sys.exit()
|
||||
|
||||
|
||||
def on_exit(sig, func=None):
|
||||
exit_application()
|
||||
|
||||
|
||||
# 当关闭cmd窗口时
|
||||
win32api.SetConsoleCtrlHandler(on_exit, True)
|
||||
|
||||
# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
|
||||
try:
|
||||
while True:
|
||||
time.sleep(0.5)
|
||||
# 当Ctrl+C结束程序时
|
||||
except KeyboardInterrupt:
|
||||
exit_application()
|
Loading…
Reference in New Issue
Block a user