【问题标题】:How can I use function with 2 arguments includes 'self' in Python?如何在 Python 中使用带有 2 个参数的函数,包括“self”?
【发布时间】:2022-01-21 23:37:39
【问题描述】:

为了解决封闭的问题,我在这里发布完整的代码。

import os
import sys
from PyQt5.QtWidgets import * 
from PyQt5 import uic

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
form_class = uic.loadUiType(BASE_DIR + "./test.ui")[0]

class WindowClass(QMainWindow, form_class):
    def __init__(self) :
        super().__init__()
        self.setupUi(self)

        self.button.clicked.connect(self.func)

    def func(self, a):
        global b
        b = a

    
if __name__ == "__main__" :
    app = QApplication(sys.argv) 
    myWindow = WindowClass() 
    myWindow.show()
    app.exec_()

还需要在同一目录下的ui文件,

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>298</width>
    <height>197</height>
   </rect>
  </property>
  <property name="maximumSize">
   <size>
    <width>298</width>
    <height>197</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="button">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>80</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

在这段代码中,

class WindowClass(QMainWindow, form_class):
    def __init__(self) :
        super().__init__()
        self.setupUi(self)

        self.button.clicked.connect(self.func(1))
    
    def func(self, a):
        global b
        b = a

这会导致参数错误(TypeError: argument 1 has unexpected type 'NoneType')。有没有办法使用func这个功能?

我尝试了wc = WindowClass() 并将代码更改为:

class WindowClass(QMainWindow, form_class):
    def __init__(self) :
        super().__init__()
        self.setupUi(self)
        wc = WindowClass()
        self.button.clicked.connect(wc.func(1))
    
    def func(self, a):
        global b
        b = a

这会导致致命的初始化错误(致命的 Python 错误:_Py_CheckRecursiveCall:无法从堆栈溢出中恢复。 Python 运行时状态:已初始化 当前线程 0x000097e8(最近调用优先):)。

代码在以下情况下完美运行:

class WindowClass(QMainWindow, form_class):
    def __init__(self) :
        super().__init__()
        self.setupUi(self)

        self.button.clicked.connect(self.func)
    
    def func(self):
        global b
        b = 1

但是,我必须使用 func 作为 2 个参数。我该怎么做?

【问题讨论】:

  • 始终包含完整的错误回溯。

标签: python function arguments


【解决方案1】:

尝试使用部分:

from functools import partial

self.button.clicked.connect(partial(self.func, 1))

我的猜测是 connect 需要一个 0 参数函数。部分固定参数并创建一个 0 参数函数。

【讨论】:

  • 感谢您的帮助!它工作正常!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多