Files
real_view/pyauto操控-测试.py
2025-07-28 12:15:45 +08:00

114 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pyautogui
import win32gui
import win32con
import datetime
import os
import time
import tkinter as tk
import pygetwindow as gw
class pyauto:
def __init__(self, master):
self.master = master
self.app_name = "东方财富证券"
self.hwnd = None
self.target_window_title = "东方财富证券"
# 添加测试按钮
self.test_button = tk.Button(
master,
text="测试下单",
command=self.place_order,
font=('微软雅黑', 11),
width=12
)
self.test_button.pack()
def click(self, x, y):
pyautogui.click(x, y)
def double_click(self, x, y):
pyautogui.doubleClick(x, y)
def place_order(self):
target_title = self.target_window_title # 假设目标窗口标题为交易窗口名称
# 使用 pygetwindow 获取窗口对象
window_list = gw.getWindowsWithTitle(target_title)
if not window_list:
raise Exception(f"未找到标题为 '{target_title}' 的窗口")
window = window_list[0]
# 先激活窗口
window.restore()
window.activate()
# 使用 win32gui 获取客户区坐标
hwnd = window._hWnd
left, top = win32gui.ClientToScreen(hwnd, (0, 0))
right, bottom = win32gui.ClientToScreen(hwnd, win32gui.GetClientRect(hwnd)[2:])
width = right - left
height = bottom - top
# 调试输出窗口信息
debug_info = f"选中窗口信息:\n" \
f"标题: {window.title}\n" \
f"位置: 左上角 ({left}, {top})\n" \
f"位置: 右下角 ({right}, {bottom})\n" \
f"宽度: {width}\n" \
f"高度: {height}\n" \
f"是否激活: {window.isActive}\n"
print(debug_info) # 同时在控制台输出
# 获取窗口位置和尺寸
left = window.left
top = window.top
print(left, top)
# 先确定在普通交易一栏
b_x = left + 60 # 示例相对位置可按需调整000931
b_y = top + 70 # 示例相对位置,可按需调整
pyautogui.click(x=b_x, y=b_y)
time.sleep(0.1)
buy_x = left + 90 # 示例相对位置,可按需调整
buy_y = top + 112 # 示例相对位置,可按需调整
pyautogui.click(x=buy_x, y=buy_y)
# 清空原始数据
c_x = left + 312
c_y = top + 471
pyautogui.click(x=c_x, y=c_y)
# 输入代码
b_x = left + 380
b_y = top + 185
pyautogui.doubleClick(x=b_x, y=b_y)
pyautogui.typewrite("002566")
time.sleep(0.1)
# 输入价格
c_x = left + 591
c_y = top + 335
pyautogui.doubleClick(x=c_x, y=c_y) # 改为双击选中内容
pyautogui.typewrite(str(7.04))
# 输入数量
c_x = left + 341
c_y = top + 383
pyautogui.doubleClick(x=c_x, y=c_y) # 改为双击选中内容
pyautogui.typewrite(str(100))
time.sleep(0.2)
# 买入点击
c_x = left + 483
c_y = top + 468
pyautogui.click(x=c_x, y=c_y)
# if order_type == 'buy':
# 假设买入按钮位置,这里可以根据窗口位置计算相对坐标
#
#
# # time.sleep(0.1)
# # 点击按钮位置
# buy_b_x = left + 480 # 示例相对位置可按需调整000931
# buy_b_y = top + 471
# pyautogui.click(x=buy_b_x, y=buy_b_y)
# # 示例:模拟输入数量
# # pyautogui.typewrite(str(volume))
if __name__ == "__main__":
root = tk.Tk()
app = pyauto(root)
root.mainloop()