Alex Semenchenko
Alex Semenchenko

Reputation: 77

How to call function by not clicking the button?

I'm coding on Python 3.5 using PyQt5 GUI. A prototype of my project is an app that creates the matrix of texts and changes the color of each "cell", one by one. Now it happens with clicking button, which is connected with the function.

I need to call the function that changes BrowserText configuration without any user interaction with app. In other words, palettes should change color by themselves in concrete time intervals.

So, is there any method in QPushButton widget (or any inherited one), that, for instance, calls the function after not clicking it for some time period? If there isn't, can I call function automatically in some way?

The design class code looks like:

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"))

Upvotes: 2

Views: 82

Answers (1)

S. Nick
S. Nick

Reputation: 13641

The QTimer class provides repetitive and single-shot timers.

The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on, it will emit the timeout() signal at constant intervals.

Try it:

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_())        

Upvotes: 1

Related Questions