云生 发表于 2024-12-5 03:25:42

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

我前些天发了一个“鼠标点击精灵”,是用按键精灵做的,
有人说不如用python做。
我就学。
现在有个问题解决不了。

如下
[*]    def 鼠标位置(self):
[*]      width, height = pyautogui.position()
[*]      x=str(width)
[*]      y=str(height)
[*]      self.lineEditx5.setText(x)
[*]      self.lineEdity5.setText(y)复制代码

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

但是这个地方根本没有办法作为一个参数传递进去。
我在网上找也没办法,
有大佬知道怎么弄吗?
求解决。

杜若言叶 发表于 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)
页: [1]
查看完整版本: 求教python大佬,有个函数调用问题