如何通过不单击按钮来调用功能?

时间:2019-05-09 12:04:01

标签: python python-3.x pyqt pyqt5

我正在使用PyQt5 GUI在Python 3.5上进行编码。我项目的原型是一个应用程序,它可以创建文本矩阵并逐个更改每个“单元格”的颜色。现在,只需单击按钮即可,该按钮与该功能关联。

我需要调用无需更改任何用户与应用程序交互即可更改BrowserText配置的函数。换句话说,调色板应在特定的时间间隔内自行更改颜色。

因此,QPushButton小部件中是否有任何方法(或任何继承的方法),例如,在一段时间内未单击该函数后调用该函数?如果没有,我可以通过某种方式自动调用函数吗?

设计类代码如下:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def __init__(self):
        self.width = int(20)
        self.height = int(16)
        self.i = 0
        self.j = 0

    def setColor(self):
        if (self.i < self.height) and (self.j < self.width):
            self.texts[self.i][self.j].setStyleSheet("QTextBrowser {background-color:white}")
            if self.j == self.width - 1:
                self.j = -1
                self.i += 1
            self.j += 1
            self.texts[self.i][self.j].setStyleSheet("QTextBrowser {background-color:red}")


    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(40 * int(self.width), 40 * int(self.height) + 10)
        Dialog.setStyleSheet("QPushButton{\n"
                             "    width = 0px;\n"
                             "}")
        self.gridLayoutWidget = QtWidgets.QWidget(Dialog)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 40 * self.width, 40 * self.height))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")



        self.texts = [[QtWidgets.QTextBrowser(self.gridLayoutWidget) for j in range(self.width)] for i in
                      range(self.height)]
        for i in range(self.height):
            for j in range(self.width):
                self.texts[i][j] = QtWidgets.QTextBrowser(self.gridLayoutWidget)
                self.texts[i][j].setObjectName("text" + str(i) + str(j))
                self.gridLayout.addWidget(self.texts[i][j], i, j)

        self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.gridLayout.addWidget(self.pushButton, self.height, 1, 1, self.width - 2)
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(lambda: self.setColor())

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

1 个答案:

答案 0 :(得分:1)

QTimer类提供重复和单次计时器。

QTimer类为计时器提供了高级编程接口。要使用它,请创建一个QTimer,将其timeout()信号连接到适当的插槽,然后调用start()。从那时起,它将以固定的时间间隔发出timeout()信号。

尝试一下:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class Ui_Dialog(object):

    def __init__(self):
        self.width = int(20)
        self.height = int(16)
        self.i = 0
        self.j = 0

    """
    def setColor(self):
        if (self.i < self.height) and (self.j < self.width):
            self.texts[self.i][self.j].setStyleSheet("QTextBrowser {background-color:white}")
            if self.j == self.width - 1:
                self.j = -1
                self.i += 1
            self.j += 1
            self.texts[self.i][self.j].setStyleSheet("QTextBrowser {background-color:red}")
    """

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(40 * int(self.width), 40 * int(self.height) + 10)
        Dialog.setStyleSheet("QPushButton{\n"
                             "    width = 0px;\n"
                             "}")
        self.gridLayoutWidget = QtWidgets.QWidget(Dialog)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 40 * self.width, 40 * self.height))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")



        self.texts = [[QtWidgets.QTextBrowser(self.gridLayoutWidget) for j in range(self.width)] for i in
                      range(self.height)]
        for i in range(self.height):
            for j in range(self.width):
                self.texts[i][j] = QtWidgets.QTextBrowser(self.gridLayoutWidget)
                self.texts[i][j].setObjectName("text" + str(i) + str(j))
                self.gridLayout.addWidget(self.texts[i][j], i, j)

        self.pushButton = QtWidgets.QPushButton("Button", self.gridLayoutWidget)
        self.gridLayout.addWidget(self.pushButton, self.height, 1, 1, self.width - 2)
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(lambda: self.setColor())

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))


class Window(QWidget, Ui_Dialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.setupUi(self)

        self.width = int(20)
        self.height = int(16)
        self.i = 0
        self.j = 0

        self.timer = QtCore.QTimer(interval=1000)                 # <-------
        self.timer.timeout.connect(self.setColor)
        self.timer.start()

    def setColor(self):
        if (self.i < self.height) and (self.j < self.width):
            self.texts[self.i][self.j].setStyleSheet("QTextBrowser {background-color:white}")
            if self.j == self.width - 1:
                self.j = -1
                self.i += 1
            self.j += 1
            self.texts[self.i][self.j].setStyleSheet("QTextBrowser {background-color:red}")



if __name__ == "__main__": 
    application = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setWindowTitle('How to call function by not clicking the button?')  
    window.show()
    sys.exit(application.exec_())