简体   繁体   English

通过单击按钮 PyQt4 从另一个类调用函数

[英]Call a function from another class by clicking a button PyQt4

I have a checkbox and a run button.我有一个复选框和一个运行按钮。 When the checkbox is checked, I want to run some functions by clicking the button.当复选框被选中时,我想通过单击按钮来运行一些功能。 The problem is that the function is in another class outside the button's class.问题是该函数位于按钮类之外的另一个类中。 My example codes are as below.我的示例代码如下。

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Tab1Widget1(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.Tab1Widget1initUI()

    def Tab1Widget1initUI(self):
        self.setLayout(QGridLayout())

        self.T1W1checkBox1 = QCheckBox('a', self)

        self.layout().addWidget(self.T1W1checkBox1, 1, 0)

    def run(self):
        if self.T1W1checkBox1.isChecked() == True:
            pass

class Tab1Layout(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QGridLayout())

        self.group1 = Tab1Widget1(self)
        self.layout().addWidget(self.group1, 0, 0)

        btn = QPushButton('Run', self)
        self.layout().addWidget(btn, 1, 0)
        btn.clicked.connect(Tab1Widget1().run())  ##the problem is in this line.

class Page1(QTabWidget):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.tab1 = Tab1Layout()
        self.addTab(self.tab1, "Tab1")

        self.tab2 = QWidget()
        self.tab3 = QWidget()
        self.addTab(self.tab2, "Tab2")
        self.addTab(self.tab3, "Tab3")
        self.tab2_initUI()
        self.tab3_initUI()

    def tab2_initUI(self):
        grid = QGridLayout()
        self.tab2.setLayout(grid)

    def tab3_initUI(self):
        grid = QGridLayout()
        self.tab3.setLayout(grid)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(300, 200, 600, 370)
        self.startPage1()

    def startPage1(self):
        x = Page1(self)
        self.setWindowTitle("Auto Benchmark")
        self.setCentralWidget(x)
        self.show()

def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

As you can see, I want to run the "run" function in "Tab1Widget1" class.如您所见,我想在“Tab1Widget1”类中运行“run”函数。 However, the button is in "Tab1Layout" class.但是,该按钮位于“Tab1Layout”类中。 When I run the codes, it returns to me "TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'"当我运行代码时,它返回给我“TypeError:connect() 槽参数应该是可调用的或信号,而不是‘NoneType’”

If anyone knows how to solve this, pls let me know.如果有人知道如何解决这个问题,请告诉我。 Appreciated!赞赏!

There is no problem in connecting any callable to a button click regardless of what object it is in. But your code has two specific problems.不管它在什么对象中,将任何可调用对象连接到按钮单击都没有问题。但是您的代码有两个特定问题。 You write你写

 btn.clicked.connect(Tab1Widget1().run())  

The first problem here is that Tab1Widget1() is creating a new Tab1Widget1 but presumably you don't want that.这里的第一个问题是Tab1Widget1()正在创建一个新的Tab1Widget1但大概你不想要那样。 You want to call run on the Tab1Widget1 you have already created and stored in self.group .你想调用runTab1Widget1您已经创建并存储在self.group

The second problem is that when you connect a signal you need to connect it to a callable: the method you want to call.第二个问题是,当您连接信号时,您需要将其连接到可调用对象:您要调用的方法。 Instead here you are calling the run method at connect time and trying to connect to the result of that call (which is None ).相反,您在连接时调用run方法并尝试连接到该调用的结果(即None )。 So you are trying to connect the signal to None which will of course fail.所以你试图将信号连接到None这当然会失败。 You need to refer to the method without calling it: just remove the calling brackets.您需要引用该方法而不调用它:只需删除调用方括号即可。

Putting it together:把它放在一起:

  btn.clicked.connect(self.group1.run)

That seems to work.这似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM