【问题标题】:PyQT Python errors - Cannot create a QWidget without QApplicationPyQT Python 错误 - 无法在没有 QApplication 的情况下创建 QWidget
【发布时间】:2014-04-17 07:11:12
【问题描述】:

我在 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__()

但是在执行时,我得到了这个错误:

QWidget: Cannot create a QWidget without QApplication

【问题讨论】:

    标签: python pyqt4 pyqt5


    【解决方案1】:

    这行得通:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 2016-02-10
      • 2014-03-04
      相关资源
      最近更新 更多