【问题标题】:Python3 Attribute Error: type object 'mainWindow' (QMainWindow) has no attribute 'calWidget' (QWidget)Python3属性错误:类型对象'mainWindow'(QMainWindow)没有属性'calWidget'(QWidget)
【发布时间】:2020-07-15 16:29:24
【问题描述】:

我一直在创建这个日历对象,它可以“分配”事件 - 这些是用户想要的任何东西,从取牛奶 从街角商店组织婚礼到制定考试时间表 - 到特定日期。

这是我的应用程序代码的基本版本(使用 Python 3.4.3 和 PyQt 4.11.4 编写),可以在上面重新创建我经常遇到的错误(如下所示):

# Imports all the needed modules.
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# Defines variables needed for the settings dialog.
currentDate = QtCore.QDate.currentDate()
maxDate = QtCore.QDate(2999, 12, 31)

class AppSettingsDialog(QtGui.QDialog): # The Settings dialog.
    def __init__(self, parent=None):
        # The initializer function for the dialog.
        super(AppSettingsDialog, self).__init__(parent)
        self.setAttribute(Qt.WA_DeleteOnClose)

        self.calendarSpecificSettings()

        mainGridLayout = QtGui.QGridLayout()
        mainGridLayout.addWidget(self.calendarSpecificSettingsBox,
                                 0, 0)
        self.setLayout(mainGridLayout)

        self.exec_()

    def calendarSpecificSettings(self):
        # Creates a separator to put the widgets in.
        self.calendarSpecificSettingsBox = QtGui.QGroupBox(
            'Calendar Specific')

        self.maxDateLabel = QtGui.QLabel('Ma&ximum Date')
        self.maxDateEdit = QtGui.QDateEdit()
        self.maxDateEdit.setDisplayFormat('dd MMM yyyy')
        self.maxDateEdit.setDateRange(currentDate, maxDate)
        self.maxDateEdit.setDate(maxDate)
        self.maxDateLabel.setBuddy(self.maxDateEdit)
        self.maxDateEdit.dateChanged.connect(self.maximumDateChanged)
                    # Call to function error here ^

        self.calendarSpecificGridLayout = QtGui.QGridLayout()
        self.calendarSpecificGridLayout.addWidget(self.maxDateLabel,
                                                  0, 0)
        self.calendarSpecificGridLayout.addWidget(self.maxDateEdit,
                                                  0, 1)

        self.calendarSpecificSettingsBox.setLayout(
            self.calendarSpecificGridLayout)

    def maximumDateChanged(self, date): 
        MainWindow.calWidget.setMaximumDate(date)
        # ^This is the line that causes the error. I have multiple
        # 'MainWindow' calls - one for each of the settings I want
        # to change.

class MainWindow(QtGui.QMainWindow): # The main window.
    def __init__(self):
        # The initializer function for the window.
        super(MainWindow, self).__init__()
        mainMenu = self.menuBar()

        menuOptions = mainMenu.addMenu('Options')
        actionSettings = QtGui.QAction('Settings...', self)
        actionSettings.triggered.connect(self.appSettings)
        menuOptions.addAction(actionSettings)

        calWidget = QtGui.QCalendarWidget(self)
        calWidget.resize(300, 300)
        calWidget.setGridVisible(True)
        calWidget.setNavigationBarVisible(True)
        self.setCentralWidget(calWidget)

        self.statusBar().setSizeGripEnabled(True)
        self.statusBar().showMessage('Ready', 5000)

    def appSettings(self):
        # The function that invokes the dialog.
        AppSettingsDialog()

# One way to initialize the application.
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    GUI = MainWindow()
    GUI.show()
    sys.exit(app.exec_())

我的问题如下:

我正在尝试确保设置对话框中的设置更改 实际上适用于日历,但是,当我更改其中任何一个时, 以下错误不断出现(它已经困扰着我的梦想):

Traceback (most recent call last):
  File "C:/Python34/CalTest.py", line 50, in maximumDateChanged
    mainWindow.calWidget.setMaximumDate(date)
AttributeError: type object 'MainWindow' has no attribute 'calWidget'

更糟糕的是,我实际上并不理解这个错误。 这是否意味着calWidget 不是MainWindow 的属性? 是因为Settings 类与MainWindow 类是分开的吗? 我已经在这个论坛(和它之外)上研究过任何相关的东西 就可以了,但我发现没有什么与我的问题有关。我的研究包括:

Python Attribute Error: type object has no attribute

Type Object has no attribute

Error: type object 'Keys' has no attribute 'chord'

但我发现没有任何东西可以解决我的问题(相信我我试过了)。

另外,当我使用它时,如何通过对话窗口接收来自用户的多个输入?词典词典是否合适,或者有其他东西可以让我的生活更轻松吗?这是因为我还没有实现添加与特定日期相关的新事件的功能,并且有一个起点将是一件幸事——整个情况可能是一场不必要的噩梦。我还觉得如果我解决了这个问题,我将能够在其他操作中重新创建相同的结构(包括添加、编辑、保存和删除一个或所有创建的事件)。

任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

  • 这是否意味着 calWidget 不是 mainWindow 的属性? - 是的
  • 但我在 mainWindow 类中创建了 calWidget

标签: python python-3.x pyqt4


【解决方案1】:

你可以在你的函数中像这样访问 calWidget:

def maximumDateChanged(self, date): 
        mainWindow.centralWidget().setMaximumDate(date)
        # ^This is the line that causes the error. I have multiple
        # 'mainWindow' calls - one for each of the settings I want
        # to change.

我的回答不正确,因为我将 mainWindow 误认为是基于约定的实例。在您的情况下,通过对话框处理 MainWindow 上的更改,您需要更改 appSettings 方法并像这样在 mainWindow 类中定义 maximumDateChanged

def appSettings(self):
        # The function that invokes the dialog.
        appSettingsDialog = AppSettingsDialog()
        appSettingsDialog.maxDateEdit.dateChanged.connect(self.maximumDateChanged)

def maximumDateChanged(self, date):
        self.centralWidget().setMaximumDate(date)

【讨论】:

  • 当我输入你给我的内容时,我得到另一个错误:TypeError: QMainWindow.centralWidget(): first argument of unbound method must have type 'QMainWindow'
  • 我还尝试了上面代码的变体,包括没有括号,括号内有“mainWindow”和“QtGui.QMainWindow”,但仍然存在相同的错误。
  • 在上面添加您编辑的代码时,在更改QDateEdit 对象中的值时,我得到与上面相同的错误(在我的第一条评论中),并且在关闭设置对话框时出现另一个错误,如下:RuntimeError: wrapped C/C++ object of type QDateEdit has been deleted - 这发生在上面代码的第 4 行。有什么值得担心的,因为我以前从未见过这个错误吗?
  • mainWindow 名称编辑为 MainWindow 以符合我设置的关于类名称的约定。
  • 原来的错误依然出现,因为你没有删除Dialog类中的self.maxDateEdit.dateChanged.connect(self.maximumDateChanged)这一行。此外,当对话框关闭时,您必须断开在 appSettings 方法中创建的信号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2018-12-07
  • 2017-11-12
  • 1970-01-01
  • 2019-04-15
  • 2010-12-23
相关资源
最近更新 更多