Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySide无法连接已单击的信号()_Python_Pyside - Fatal编程技术网

Python PySide无法连接已单击的信号()

Python PySide无法连接已单击的信号(),python,pyside,Python,Pyside,我有一个应用程序,它应该在点击按钮后打开一个web浏览器地址。主要功能如下所示: class MainWindow(QtGui.QDialog): def __init__( self, parent = None ): super( MainWindow, self ).__init__( parent = parent ) self.button_layout = QtGui.QHBoxLayout(self) self.webButt

我有一个应用程序,它应该在点击按钮后打开一个web浏览器地址。主要功能如下所示:

class MainWindow(QtGui.QDialog):
    def __init__( self, parent = None ):
        super( MainWindow, self ).__init__( parent = parent )
        self.button_layout = QtGui.QHBoxLayout(self)
        self.webButton = PicButton(QtGui.QPixmap("images/button.png"))
        self.filmButton.clicked.connect(openWebpage("http://some.web.adress"))
        self.button_layout.addWidget(self.webButton)
打开web浏览器的功能如下所示:

def openWebpage(address):
    try:
        import webbrowser

        webbrowser.open(address)

    except ImportError as exc:
        sys.stderr.write("Error: failed to import settings module ({})".format(exc))
运行此代码后,没有可见的应用程序窗口,web浏览器立即启动,控制台返回:

Failed to connect signal clicked().

连接到此按钮的简单功能正常工作(例如,将文本打印到控制台)。有什么想法吗?

要在插槽中传递参数,需要构造lambda表达式:

self.filmButton.clicked.connect(lambda: openWebpage("http://some.web.adress"))

为了进一步解释这一点,connect()方法接受一个可调用对象作为其参数。lambda表达式基本上是一个匿名函数,就是这样一个可调用的对象。您还可以将函数调用封装在
functools
模块的
partial()
方法中,以实现相同的功能。有关Python可调用函数的更多信息,请参见前面人们所说的

——使用lambda,或者使用一个普通的
插槽
,它(至少对我来说)提供了更高的可读性

def __init__(self):
    self.filmButton.clicked.connect(self._film_button_clicked)

@pyqtSlot() # note that this is not really necessary
def _film_button_clicked(self):
    self.openWebpage('http://some.web.adress')

self.filmButton.clicked.connect(打开网页(“http://some.web.adress)
正确吗?我的意思正是
openWebpage(“http://some.web.adress“”
-部分。我认为它只能作为
self.filmButton.clicked.connect(openWebpage)
使用。另外,提供一种类型的“filmButton”。@VictorPolevoy是对的。