def max_apps_windows(): """判断当前是否最小化,如果是,则点击任务栏图标,实现最大化""" # 找到最大化窗口图标位置 im, bin, xrange, yrange = read_im_info('界面_已最大化窗口') # 截图,判断匹配位置 pic = get_screenshot() pic2 = to_bin_img(pic.copy(), bin) x, y = find_location(pic2, im, xrange, yrange, 'center') # 判断是否已经最大化 if x > 0 or y > 0: log.info('窗口已经最大化,无需点击') return 0 # 没有最大化,找到点击窗口图标 log.info('窗口未最大化,需要最大化窗口') im, bin, xrange, yrange = read_im_info('界面_需最大化窗口') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x, y = find_location(pic2, im, xrange, yrange, 'center') if x == -1 or y == -1: log.info('没有找到最大化窗口图标,请检查是否最小化或者锁频了') return -1 else: mouse_click(x, y) log.info('点击窗口最大化 %d %d ,解锁交易界面' % (x, y))
def unlock(passwd='123456'): """判断是否需要解锁界面""" # 找到锁屏标志 im, bin, xrange, yrange = read_im_info('界面_解锁密码') # 截图,判断匹配位置 pic = get_screenshot() pic2 = to_bin_img(pic.copy(), bin) x, y = find_location(pic2, im, xrange, yrange, 'center') # 判断是否锁屏 if x == -1 or y == -1: log.info('交易界面没有锁定,无需解锁') else: log.info('交易界面锁定,需要解锁') mouse_click(x, y) keyboard_input(passwd) log.info('移动到 %d %d ,输入解锁密码' % (x, y)) # 找到确认按钮 im, bin, xrange, yrange = read_im_info('界面_解锁确认') im = cut_blank(im) x1 = (x - 100) / pic.shape[0] if x - 100 >= 0 else 0 x2 = (x + 100) / pic.shape[0] if x + 100 <= pic.shape[0] else 1 y1 = (y - 200) / pic.shape[1] if y - 200 >= 0 else 0 y2 = (y + 200) / pic.shape[1] if y + 200 <= pic.shape[1] else 1 xrange, yrange = [x1, x2], [y1, y2] pic2 = to_bin_img(pic.copy(), bin) xc, yc = find_location(pic2, im, xrange, yrange, 'center') mouse_click(xc, yc) log.info('点击确认按钮 %d %d ,解锁交易界面' % (xc, yc)) return
def get_position_table(): """获取指定列的数据""" # 获取焦点 get_focus() # 功能键F1,转到买入选项卡 keyboard_input(function_key='F4') log.info('获取程序焦点,调出股票持仓查询页面') # 第一步是先获取屏幕截图 pic=get_screenshot() log.info('获取屏幕截图') # 获取表格下边框,防止搜索过界 info = config.pic_info['持仓_汇总'] bin = info['bin'] xrange = info['xrange'] yrange = info['yrange'] # 获取子图 file = os.path.join(config.gvariable.work_path, 'pic', '持仓_汇总.png') print(file) im = read_im(file, threshold=bin) # 因为已经是二值图,这里的阈值就没必要了 im = cut_blank(im) # 获取主图的二值图 pic2 = to_bin_img(pic.copy(), threshold=bin) # 接下来就是找到边界了 maxdown,y=find_location(pic2,im,xrange,yrange,'leftup') log.info('获取最大搜索下边界 %d'%maxdown) # 开始循环获取数字串 cols = ['持仓_证券代码', '持仓_股票余额', '持仓_可用余额', '持仓_冻结数量', '持仓_成本价', '持仓_市价', '持仓_市值'] # col=cols[0] tables=[] for col in cols: # 获取准备好的子图信息 info=config.pic_info[col] bin=info['bin'] xrange=info['xrange'] yrange=info['yrange'] # 获取子图 file=os.path.join(config.gvariable.work_path,'pic',col+'.png') im=read_im(file,threshold=bin) # 因为已经是二值图,这里的阈值就没必要了 im=cut_blank(im) # 获取主图的二值图 pic2=to_bin_img(pic.copy(),threshold=bin) # 接下来就是获取列的信息了 col_info=get_cols_pic_info(pic2,im,xrange,yrange,maxdown) tables.append(col_info) log.info('完成 %s 数据解析'%col) # 转成dataframe position=pd.DataFrame(tables,index=cols).T log.info('完成表格所有列解析\n'+str(position)) return position
def get_focus(): """获取界面焦点""" im, bin, xrange, yrange = read_im_info('界面_标题') # 截屏 pic = get_screenshot() pic = to_bin_img(pic, bin) # 查找位置 x, y = find_location(pic, im, xrange, yrange, 'center') mouse_click(x, y) log.info('点击,获取界面焦点 %d %d' % (x, y))
def get_app_focus(): """点击一下软件的任务栏图标,获取焦点""" # 获取图标信息 name = '界面_任务栏图标' im, bin, xrange, yrange = read_im_info(name) # 截屏 pic = get_screenshot() pic = to_bin_img(pic, bin) # 查找位置 x_ico, y_ico = find_location(pic, im, xrange, yrange, 'center') log.info('找到任务栏图标位置 %d %d' % (x_ico, y_ico)) # 接下来是鼠标点击 # 由于无法实现鼠标点击任务栏图标事件,暂时空着 # return x_ico, y_ico
def get_mony_summary(): """获取资金汇总""" # 获取焦点 get_focus() # 功能键F4,转到买入选项卡 keyboard_input(function_key='F4') log.info('获取程序焦点,调出股票持仓查询页面') # 第一步是先获取屏幕截图 pic = get_screenshot() # plt.imshow(pic) log.info('获取屏幕截图') # 获取下面几个子图的坐标 # 注意,尽量不要使用循环,而是一个个写,防止位置顺序乱了 # 资金余额 x1_zjye, x2_zjye, y1_zjye, y2_zjye = get_coordinate( pic.copy(), '资金汇总_资金余额') # 可取金额 ,注意 ‘可用金额’ 和 ‘可取金额’ 还是不一样的 x1_kqje, x2_kqje, y1_kqje, y2_kqje = get_coordinate( pic.copy(), '资金汇总_可取金额') # 冻结金额 x1_djje, x2_djje, y1_djje, y2_djje = get_coordinate( pic.copy(), '资金汇总_冻结金额') # 股票市值 x1_gpsz, x2_gpsz, y1_gpsz, y2_gpsz = get_coordinate( pic.copy(), '资金汇总_股票市值') # 可用金额 x1_kyje, x2_kyje, y1_kyje, y2_kyje = get_coordinate( pic.copy(), '资金汇总_可用金额') # 总资产 x1_zzc, x2_zzc, y1_zzc, y2_zzc = get_coordinate(pic.copy(), '资金汇总_总资产') log.info('获取资金汇总的6个字段的上下左右坐标') # 下面计算数字所在表格的高度和宽度 xhigh = x2_zjye - x1_zjye + 2 # 上下增加1行空白 ywidth = y1_kqje - y2_zjye - 4 # 所以说顺序很重要 # 下面分别取对应的区域数字 # 资金余额 x1, x2, y1, y2 = x1_zjye - 1, x2_zjye + 2, y2_zjye + 2, y2_zjye + 2 + ywidth bin = config.pic_info['资金汇总_资金余额']['bin'] zjye = rec_nums(to_bin_img(pic[x1:x2, y1:y2].copy(), bin)) # 可取金额 x1, x2, y1, y2 = x1_kqje - 1, x2_kqje + 2, y2_kqje + 2, y2_kqje + 2 + ywidth bin = config.pic_info['资金汇总_可取金额']['bin'] kqje = rec_nums(to_bin_img(pic[x1:x2, y1:y2].copy(), bin)) # 冻结金额 x1, x2, y1, y2 = x1_djje - 1, x2_djje + 2, y2_djje + 2, y2_djje + 2 + ywidth bin = config.pic_info['资金汇总_冻结金额']['bin'] djje = rec_nums(to_bin_img(pic[x1:x2, y1:y2].copy(), bin)) # 股票市值 x1, x2, y1, y2 = x1_gpsz - 1, x2_gpsz + 2, y2_gpsz + 2, y2_gpsz + 2 + ywidth bin = config.pic_info['资金汇总_股票市值']['bin'] gpsz = rec_nums(to_bin_img(pic[x1:x2, y1:y2].copy(), bin)) # 可用金额 x1, x2, y1, y2 = x1_kyje - 1, x2_kyje + 2, y2_kyje + 2, y2_kyje + 2 + ywidth bin = config.pic_info['资金汇总_可用金额']['bin'] kyje = rec_nums(to_bin_img(pic[x1:x2, y1:y2].copy(), bin)) # 总资产 x1, x2, y1, y2 = x1_zzc - 1, x2_zzc + 2, y2_zzc + 2, y2_zzc + 2 + ywidth bin = config.pic_info['资金汇总_总资产']['bin'] zzc = rec_nums(to_bin_img(pic[x1:x2, y1:y2].copy(), bin)) # 将数据组合成dataframe data = pd.DataFrame([zjye, kqje, djje, gpsz, kyje, zzc], index=['资金余额', '可取金额', '冻结金额', '股票市值', '可用金额', '总资产']).T log.info('资产状况\n %s' % str(data.T)) return data
def buy(symbol, price, amount): """买入股票""" # 获取焦点 get_focus() # 功能键F1,转到买入选项卡 keyboard_input(function_key='F1') log.info('获取程序焦点,调出股票买入页面') # 截图 pic = get_screenshot() # 获取 买入股票 标志坐标 im, bin, xrange, yrange = read_im_info('买入_买入页') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), threshold=bin) x, y = find_location(pic2, im, xrange, yrange, 'leftup') log.info('买入股票 坐标 %d %d ' % (x, y)) # 计算其他几个标志的查找范围 high = pic.shape[0] * 0.3 width = pic.shape[1] * 0.3 x1 = (x - 10) / pic.shape[0] if x - 10 >= 0 else 0 x2 = (x + high) / pic.shape[0] if x + high <= pic.shape[0] else 1 y1 = (y - 10) / pic.shape[1] if y - 10 > 0 else 0 y2 = (y + width) / pic.shape[1] if y + width <= pic.shape[1] else 1 # 获取 证券代码,买入价格,买入数量,买入 # 证券代码 im, bin, xrange, yrange = read_im_info('买入_证券代码') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_zqdm, y_zqdm = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('证券代码 坐标 %d %d ' % (x_zqdm, y_zqdm)) # 买入价格 im, bin, xrange, yrange = read_im_info('买入_买入价格') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_mrjg, y_mrjg = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入价格 坐标 %d %d ' % (x_mrjg, y_mrjg)) # 买入数量 im, bin, xrange, yrange = read_im_info('买入_买入数量') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_mrsl, y_mrsl = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入数量 坐标 %d %d ' % (x_mrsl, y_mrsl)) # 买入确认 im, bin, xrange, yrange = read_im_info('买入_买入确认') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_mrqr, y_mrqr = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入确认 坐标 %d %d ' % (x_mrqr, y_mrqr)) # 重填 im, bin, xrange, yrange = read_im_info('买入_重填') im = cut_blank(im) pic2 = to_bin_img(pic.copy(), bin) x_ct, y_ct = find_location(pic2, im, [x1, x2], [y1, y2], 'center') log.info('买入重填 坐标 %d %d ' % (x_ct, y_ct)) # 下面买入股票 # 重置所有输入 mouse_click(x_ct, y_ct) # 输入代码 mouse_click(x_zqdm, y_zqdm) keyboard_input(symbol) # 输入价格 mouse_click(x_mrjg, y_mrjg) keyboard_input(str(price)) # 输入数量 mouse_click(x_mrsl, y_mrsl) keyboard_input(str(amount)) # 点击确认 mouse_click(x_mrqr, y_mrqr) log.info('买入 symbol=%s price=%f amount=%d' % (symbol, price, amount)) # 后面还有弹出确认框的确认 log.info('后面还有弹出确认框的确认, 未处理')