Search code examples
pythonpyqt4pyqt5

PyQT Python errors - Cannot create a QWidget without QApplication


I have created the following code in python:

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

app = QGuiApplication(sys.argv)
try:
    due = QTime.currentTime()
    message = "Alert!"
    if len(sys.argv) < 2 :
        raise ValueError
    hours, min = sys.argv[1].split(":")
    due = Qtime(int(hours), int(min))
    if not due.isValid():
        raise ValueError
    if len(sys.argv) > 2 :
        message = " ".join(sys.argv[2:])
except ValueError :
    message = "Alert: alert.pyw"

if QTime.currentTime() < due :
    time.sleep(20) #20 Seconds


label = QLabel("message")
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.setSingleShot(60000, app.quit() )
app.exec__()

But when executing it, I get this error:

QWidget: Cannot create a QWidget without QApplication

Solution

  • Try using QApplication instead of QGuiApplication:

    app = QApplication(sys.argv)
    label = QLabel("message")
    label.setWindowFlags(Qt.SplashScreen)
    label.show()
    QTimer.singleShot(6000, app.quit)
    app.exec_()