Implementing QThread into code



  • Hello everybody, small example of GUI with actual time showing and button for calling the graph is here.
    When code is runing actual time is showing and it is updated every second, when button is pressed new window with graph is appeared (some prepared buffer data), but updating of the time on the main GUI is stopped. I would be very pleassed if someone can help me to solve this problem.
    main.py

    import gui
    import sys
    import datetime
    from graph import simple_graph
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtCore import QTimer
    
    
    if __name__ == "__main__":
    
        app = QtGui.QApplication(sys.argv)
    
        def time_show():
            now = datetime.datetime.now()
            master.Time.setText(now.strftime("%H" + ":" + "%M" + ":" + "%S"))
    
        gui_master = gui.QtGui.QTabWidget()
        master = gui.Ui_Izbira()
        master.setupUi(gui_master)
    
        # Buttons
        master.pushButton_Graph.clicked.connect(simple_graph)
    
        # Timer - Trigers updating of time label every second
        timer = QTimer()
        timer.timeout.connect(time_show)
        timer.start(1000)
    
        gui_master.show()
        sys.exit(app.exec_())
    

    gui.py

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'test.ui'
    #
    # Created: Sun Apr  9 08:44:41 2017
    #      by: PyQt4 UI code generator 4.11.2
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt4 import QtCore, QtGui
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s
    
    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)
    
    class Ui_Izbira(object):
        def setupUi(self, Izbira):
            Izbira.setObjectName(_fromUtf8("Izbira"))
            Izbira.resize(530, 363)
            Izbira.setWindowTitle(_fromUtf8(""))
            self.Mainpage = QtGui.QWidget()
            self.Mainpage.setObjectName(_fromUtf8("Mainpage"))
            self.Time = QtGui.QLineEdit(self.Mainpage)
            self.Time.setGeometry(QtCore.QRect(130, 70, 261, 71))
            font = QtGui.QFont()
            font.setPointSize(47)
            font.setBold(False)
            font.setItalic(True)
            font.setWeight(50)
            self.Time.setFont(font)
            self.Time.setObjectName(_fromUtf8("Time"))
            self.pushButton_Graph = QtGui.QPushButton(self.Mainpage)
            self.pushButton_Graph.setGeometry(QtCore.QRect(130, 200, 261, 31))
            self.pushButton_Graph.setObjectName(_fromUtf8("pushButton_Graph"))
            Izbira.addTab(self.Mainpage, _fromUtf8(""))
    
            self.retranslateUi(Izbira)
            Izbira.setCurrentIndex(0)
            QtCore.QMetaObject.connectSlotsByName(Izbira)
    
        def retranslateUi(self, Izbira):
            self.pushButton_Graph.setText(_translate("Izbira", "Graph", None))
            Izbira.setTabText(Izbira.indexOf(self.Mainpage), _translate("Izbira", "Main page", None))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        Izbira = QtGui.QTabWidget()
        ui = Ui_Izbira()
        ui.setupUi(Izbira)
        Izbira.show()
        sys.exit(app.exec_())
    

    graph.py

    import matplotlib.pyplot as plt
    
    
    OUT_Buffer_Zg = [0, 2, 5, 8, 7, 3, 0, 1, 10, 9, 0.5, 0,
        0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0]
    OUT_Buffer_Sp = [4, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
        0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0]
    
    def simple_graph():
        plt.plot(OUT_Buffer_Zg, color='r', label='Zg')
        plt.plot(OUT_Buffer_Sp, color='b', label='Sp')
    
        plt.xlabel('Time')
        plt.ylabel('Temperature degC')
        plt.title('Temperature')
        plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
               ncol=2, mode="expand", borderaxespad=0.)
    
        plt.rcParams['axes.facecolor'] = 'white'
        plt.rcParams['axes.edgecolor'] = 'white'
        plt.rcParams['grid.alpha'] = 1
        plt.rcParams['grid.color'] = "#cccccc"
        plt.grid(True)
        plt.show()
    

  • Lifetime Qt Champion

    Hi,

    Matplot lib's show might be blocking your main event loop hence no update anymore.



  • Exactly, is there any workaround or should I moove with graph to diferent library?
    Vlado


  • Lifetime Qt Champion

    Depending on your needs the QtCharts module might be enough.

    However, you will likely have to change from PyQt4 to PyQt5.



  • Problem solved with mooving to PyQtGraph. Thanks for help.
    Vlado

    import pyqtgraph as pg
    
    
    OUT_Buffer_Time = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]   # 26 spominskih mest
    OUT_Buffer_Zg = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]   # 26 spominskih mest
    OUT_Buffer_Sp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]   # 26 spominskih mest
    
    def simple_graph():
        H = pg.plot(OUT_Buffer_Time, OUT_Buffer_Zg, pen='b')
        H.plot(OUT_Buffer_Time, OUT_Buffer_Zg, pen=(255, 0, 0))
        H.plot(OUT_Buffer_Time, OUT_Buffer_Sp, pen=(0, 255, 200))
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.