Show time in QStatusBar

1

Good afternoon I am trying to show the time in a statusbar in pyqt. This is my code:

from PyQt5.QtWidgets import QMainWindow,QApplication, QDesktopWidget,QLabel
from PyQt5.QtCore import *
from PyQt5 import uic
from PyQt5 import QtCore
import time


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("23.ui",self)


        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.bmax.clicked.connect(self.W_Size)
        self.bmin.clicked.connect(self.showMinimized)
        self.bclose.clicked.connect(self.close)
        self.statusbar.showMessage(self.hora) # error
        self.statusbar.setStyleSheet("position:right;") # No cambia la posision del texto


    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def W_Size(self):
        if self.isMaximized():
            self.showNormal()
            qRect = self.frameGeometry()
            centerPoint  = QDesktopWidget().availableGeometry().center()
            qRect.moveCenter(centerPoint)
            self.move(qRect.topLeft())
        else:
            self.showMaximized()
    def hora(self):
        self.time = ''
        while True:
            self.time2 = self.time.strftime("%I:%M:%S %p")
            if self.time2 != self.time1:
                self.time1 = self.time2
                print(self.time1)




app = QApplication([])
p = Principal()
p.show()
app.exec_()

but it throws me the following error:

libpng warning: iCCP: CRC error
Traceback (most recent call last):
  File "C:\Users\Angel\Desktop.py", line 53, in <module>
    p = Principal()
  File "C:\Users\Angel\Desktop.py", line 19, in __init__
    self.statusbar.showMessage(self.hora)
TypeError: showMessage(self, str, msecs: int = 0): argument 1 has unexpected type 'method'
[Finished in 2.8s with exit code 1]
[shell_cmd: python -u "C:\Users\Angel\Desktop.py"]
[dir: C:\Users\Angel\Desktop]
[path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\ProgramData\chocolatey\bin;C:\Program Files\Java\jdk1.8.0_172\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\Git\cmd;C:\Users\Angel\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\Angel\AppData\Local\Programs\Python\Python36\;C:\Users\Angel\AppData\Roaming\npm;C:\Users\Angel\AppData\Local\Yarn\bin;C:\Users\Angel\AppData\Local\Android\Sdk\platform-tools]
    
asked by Revsky01 14.07.2018 в 20:36
source

1 answer

1

The error occurs because self.hora is an instance method when QStatusBar.showMessage expects a string (or QString ) as the first argument as shown by the signature of the method:

showMessage(self, str, msecs: int = 0)

Either way, using an infinite loop as you do in the hora method in a GUI is a very bad idea (any blocking call actually), thereby blocking the event loop from the application which will cause the interface to freeze because it can not respond to events.

Instead you can use QTimer to call the method every x milliseconds to update the time. You can use a label to display the time at the far right of the bar instead of using the message. A simplified example:

import sys
from PyQt5 import QtCore, QtWidgets



class Principal(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setCentralWidget(QtWidgets.QWidget())

        self.statusBar = QtWidgets.QStatusBar()
        self.setStatusBar(self.statusBar)
        self.label_hora = QtWidgets.QLabel()
        self.statusBar.addPermanentWidget(self.label_hora, 0)

        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.mostrar_hora)
        self.timer.start()

    def mostrar_hora(self):
        self.label_hora.setText(QtCore.QDateTime.currentDateTime()
                                                .toString("hh:mm:ss AP")
                                   )


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    p = Principal()
    p.show()
    sys.exit(app.exec_())

    
answered by 14.07.2018 / 20:53
source