1

我是 Python 和一般编程的新手,一直在玩创建 GUI。我正在尝试设置一个窗口标志,以便我的主窗口始终位于顶部。我试图在我的中央小部件上设置窗口标志,但是出现属性错误。

下面是我的代码:

import os
import csv
from PyQt6 import QtCore, QtGui, QtWidgets
import time
import sys


        

StoreID = os.environ.get("Store")

class Ui_MainWindow(object):
    
     
    def __init__(self, hostName, MainWindow):
        self.hostNames = hostName
        self.MainWindow = MainWindow     

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1052, 776)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Fixed, QtWidgets.QSizePolicy.Policy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(1052, 776))
        MainWindow.setMaximumSize(QtCore.QSize(1052, 776))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../../../Pictures/VF logo.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        **self.centralwidget.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowStaysOnTopHint)**
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(320, 60, 401, 301))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("../../../Pictures/VF logo.png"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(300, 300, 471, 81))
        self.label_2.setObjectName("label_2")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(-70, -90, 1200, 1200))
        self.frame.setMinimumSize(QtCore.QSize(1200, 1200))
        self.frame.setStyleSheet("background-color: rgb(159, 159, 159);")
        self.frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.frame.setObjectName("frame")
        self.listWidget = QtWidgets.QListWidget(self.frame)
        self.listWidget.setGeometry(QtCore.QRect(520, 480, 150, 251))
        self.listWidget.setMinimumSize(QtCore.QSize(150, 10))
        self.listWidget.setMaximumSize(QtCore.QSize(300, 16777215))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.listWidget.setFont(font)
        self.listWidget.setStyleSheet("")
        self.listWidget.setFrameShape(QtWidgets.QFrame.Shape.Box)
        self.listWidget.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.listWidget.setLineWidth(2)
        self.listWidget.setObjectName("listWidget")
        self.createlistitems()
        self.frame.raise_()
        self.label.raise_()
        self.label_2.raise_()
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1052, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Message"))
        self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:22pt; font-weight:700; color:#ffffff;\">Please Turn the Below Asset(s) On</span></p></body></html>"))
        __sortingEnabled = self.listWidget.isSortingEnabled()
        self.listWidget.setSortingEnabled(False)
        self.listWidget.setSortingEnabled(__sortingEnabled)

    def createlistitems(self):
        for Host in self.hostNames:
            item = QtWidgets.QListWidgetItem()
            item.setText(Host)
            self.listWidget.addItem(item)

def ReadCSVReport():
    with open('Offline_Report4_test.csv') as csv_file:

        csv_reader = csv.DictReader(csv_file)
        OfflineHosts= []
        line_count = 0

        for row in csv_reader:

            if line_count == 0:

            

                line_count += 1

            else:
                if row["Status"] =="Offline" and row["StoreID"] ==StoreID:
                    print(row["Hostname"])
                    OfflineHosts.append(row['Hostname'])
                    line_count += 0

        return OfflineHosts

def myExitHandler():
    time.sleep(1)
    x()

def x():
    while True:
        with open('Operator.csv') as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:       
                if row[0] == "1":
                    main()
                else:
                    break

def main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.aboutToQuit.connect(myExitHandler)
    MainWindow = QtWidgets.QMainWindow()
    OfflineHosts = ReadCSVReport()
    ui = Ui_MainWindow(OfflineHosts, MainWindow)
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    x()

我收到的错误是:

Traceback (most recent call last):
  File "c:\Test5.py", line 213, in <module>
    x()

  File "c:\Test5.py", line 197, in x
    main()

  File "c:\Test5.py", line 208, in main
    ui.setupUi(MainWindow)

  File "c:\Test5.py", line 105, in setupUi
    self.centralwidget.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowStaysOnTopHint)
AttributeError: type object 'Qt' has no attribute 'Window'
4

0 回答 0