全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 359|回复: 5

求教python大佬,有个函数调用问题

[复制链接]
发表于 2024-12-5 03:25:42 | 显示全部楼层 |阅读模式
我前些天发了一个“鼠标点击精灵”,是用按键精灵做的,
有人说不如用python做。
我就学。
现在有个问题解决不了。

如下
  1.     def 鼠标位置(self):
  2.         width, height = pyautogui.position()
  3.         x=str(width)
  4.         y=str(height)
  5.         self.lineEditx5.setText(x)
  6.         self.lineEdity5.setText(y)
复制代码


这个函数是把鼠标的坐标显示到输入框中去。
因为有多个输入框,有个值是要变的,就是 lineEditx5,lineEdity5,
调用函数时,我需要把后面的5改成其他数字

但是这个地方根本没有办法作为一个参数传递进去。
我在网上找也没办法,
有大佬知道怎么弄吗?
求解决。
 楼主| 发表于 2024-12-5 06:09:46 | 显示全部楼层
网上找到方法了,用
self.button.clicked.connect(lambda:self.mouse(5))
要在前面加上一个 lambda:
发表于 2024-12-5 03:27:56 | 显示全部楼层
def 鼠标位置(self, num):
    width, height = pyautogui.position()
    x = str(width)
    y = str(height)
   
    # 使用getattr动态访问lineEdit属性
    x_input = getattr(self, f'lineEditx{num}')
    y_input = getattr(self, f'lineEdity{num}')
   
    x_input.setText(x)
    y_input.setText(y)

self.鼠标位置(5)  # 更新lineEditx5和lineEdity5
self.鼠标位置(6)  # 更新lineEditx6和lineEdity6
 楼主| 发表于 2024-12-5 05:01:47 | 显示全部楼层
本帖最后由 云生 于 2024-12-5 05:54 编辑
杜若言叶 发表于 2024-12-5 03:27
def 鼠标位置(self, num):
    width, height = pyautogui.position()
    x = str(width)


非常感谢,如果只用 self.鼠标位置(5) 是可以运行的,

但是我现在的情况是,需要点击按钮触发这个函数,
我用
self.button.clicked.connect(self.mouse(5,self))  调用时,
总是出错,提示
takes 2 positional arguments but 3 were given


如果用
self.button.clicked.connect(self.mouse(5))
则又提示
argument 1 has unexpected type 'NoneType'

我看能不能写成 if 语句,检测到点击了按钮则运行 self.鼠标位置(5)
发表于 2024-12-5 13:50:07 | 显示全部楼层
  1. def get_mouse_position(self, num):
  2.     def set_position():
  3.         width, height = pyautogui.position()
  4.         x = str(width)
  5.         y = str(height)
  6.         
  7.         x_input = getattr(self, f'lineEditx{num}')
  8.         y_input = getattr(self, f'lineEdity{num}')
  9.         
  10.         x_input.setText(x)
  11.         y_input.setText(y)
  12.    
  13.     return set_position
复制代码


试试看这个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-5-15 10:38 , Processed in 0.069219 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表