AttributeError:“Ui\u Dialog”对象没有属性“setWindowFlags”

2024-09-29 20:31:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能将setWindowFlags与我的QDialog小部件一起使用。我试图隐藏关闭按钮或删除整个框架,以便用户无法关闭它使用默认方法。在

我尝试在我的类的setupUi函数中添加这两行中的一行:

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

以及

self.setWindowFlag(QtCore.Qt.WindowCloseButtonHint, False)但两者都给了我相同的错误。在

以下是我更好地检查的代码:

import os
import sys

import psycopg2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMessageBox

import backoffice


class Ui_Dialog(object):

    def setupUi(self, Dialog):
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 274)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(self.resource_path("./images/pos_logo_small.png")), QtGui.QIcon.Normal,
                       QtGui.QIcon.Off)
        Dialog.setWindowIcon(icon)
        Dialog.setLayoutDirection(QtCore.Qt.RightToLeft)
        Dialog.setStyleSheet("background:#a1d0f4;")
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")
        self.formLayout = QtWidgets.QFormLayout()
        self.formLayout.setHorizontalSpacing(4)
        self.formLayout.setObjectName("formLayout")
        self.label_2 = QtWidgets.QLabel(Dialog)
        font = QtGui.QFont()
        font.setFamily("Shorooq_N1")
        font.setPointSize(18)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.label_3 = QtWidgets.QLabel(Dialog)
        font = QtGui.QFont()
        font.setFamily("Shorooq_N1")
        font.setPointSize(18)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
        self.lineEdit.setSizePolicy(sizePolicy)
        self.lineEdit.setStyleSheet("background:white;")
        self.lineEdit.setObjectName("lineEdit")

        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
        self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
        self.lineEdit_2.setSizePolicy(sizePolicy)
        self.lineEdit_2.setStyleSheet("background:white;")
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
        self.closeToolButton = QtWidgets.QToolButton(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.closeToolButton.sizePolicy().hasHeightForWidth())
        self.closeToolButton.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("Shorooq_N1")
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.closeToolButton.setFont(font)
        self.closeToolButton.setStyleSheet("background:red;color:white;")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(self.resource_path("./images/close-window-256.ico")), QtGui.QIcon.Normal,
                        QtGui.QIcon.Off)
        self.closeToolButton.setIcon(icon1)
        self.closeToolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.closeToolButton.setObjectName("closeToolButton")
        self.closeToolButton.clicked.connect(self.exit_app)
        self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.closeToolButton)
        self.logInToolButton = QtWidgets.QToolButton(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.logInToolButton.sizePolicy().hasHeightForWidth())
        self.logInToolButton.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("Shorooq_N1")
        font.setPointSize(16)
        self.logInToolButton.setFont(font)
        self.logInToolButton.setStyleSheet("background:#0a74c4;color:white;")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(self.resource_path("./images/check-mark-11-256.ico")), QtGui.QIcon.Normal,
                        QtGui.QIcon.Off)
        self.logInToolButton.setIcon(icon2)
        self.logInToolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.logInToolButton.setObjectName("logInToolButton")
        self.dialog = Dialog
        self.logInToolButton.clicked.connect(self.log_in)
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.logInToolButton)
        self.gridLayout.addLayout(self.formLayout, 1, 1, 1, 1)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 1, 1, 1)
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def exit_app(self):
        QtCore.QCoreApplication.instance().quit()

    def log_in(self):
        print('activated')
        try:
            connection = psycopg2.connect(user="*******",
                                          password="********",
                                          host="127.0.0.1",
                                          port="5432",
                                          database="********")
            cursor = connection.cursor()
            postgreSQL_select_Query = "select * from users"
            cursor.execute(postgreSQL_select_Query)
            print("Selecting rows from users table using cursor.fetchall")
            user_records = cursor.fetchall()
            print("Print each row and it's columns values")
            input_username = self.lineEdit.text()
            input_password = self.lineEdit_2.text()
            for row in user_records:
                if row[1] == input_username:
                    if row[2] == input_password:
                        backoffice.isAuthenticated = True
                        print('logged in ')
                        self.dialog.close()
                else:
                    pass
                print(input)
        except (Exception, psycopg2.Error) as error:
            print("Error while fetching data from PostgreSQL", error)
        finally:
            # closing database connection.
            connection = psycopg2.connect(user="*********",
                                          password="*******",
                                          host="127.0.0.1",
                                          port="5432",
                                          database="*******")
            cursor = connection.cursor()
            if (connection):
                cursor.close()
                connection.close()
                print("PostgreSQL connection is closed")

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Vision Store Manager"))
        self.label_2.setText(_translate("Dialog", "إسم المستخدم"))
        self.label_3.setText(_translate("Dialog", "كلمة المرور"))
        self.closeToolButton.setText(_translate("Dialog", "خروج"))
        self.logInToolButton.setText(_translate("Dialog", "تسجيل الدخول"))
        self.label.setText(_translate("Dialog",
                                      "<html><head/><body><p><img src=\":/images/images/logo_small.png\"/></p></body></html>"))

    @staticmethod
    def resource_path(relative_path):
        if hasattr(sys, '_MEIPASS'):
            return os.path.join(sys._MEIPASS, relative_path)
        return os.path.join(os.path.abspath("."), relative_path)


import login_rcs_rc

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()

    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

执行行时得到错误AttributeError: 'Ui_Dialog' object has no attribute 'setWindowFlags'

更新

我改了: self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

^{pr2}$

现在的错误是:TypeError: setWindowFlags(self, Union[Qt.WindowFlags, Qt.WindowType]): first argument of unbound method must have type 'QWidget'


Tags: selfqtlabeldialogfontqtguiqtcoreqtwidgets
1条回答
网友
1楼 · 发布于 2024-09-29 20:31:48

你的继承权做得不对。在

在声明中应该怎样? 类Ui_Dialog应该从QtWidgets.QDialog继承所有属性。你的Dialog变量应该是self。不应该用Dialog变量调用函数。在

你的主要错误:

class Ui_Dialog => class Ui_Dialog(QtWidgets.QDialog)
self.gridLayout = QtWidgets.QGridLayout(Dialog) => self.gridLayout = QtWidgets.QGridLayout(self) 
#all should be changed like this
ui.setupUi(Dialog)=>ui.setupUi()

代码:

^{pr2}$

相关问题 更多 >

    热门问题