Python3 值错误:类型对象'mainWindow'(QMainWindow) 没有属性'calWidget' (QWidget)

2024-10-02 00:23:15 发布

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

我已经创建了这个日历对象,可以“分配”事件-这是用户想要的任何东西,从拿起牛奶 街角小店组织婚礼,制定考试时间表——具体日期。在

下面是我的应用程序代码的基本版本(使用python3.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_())

我面临的问题如下:

我正在尝试确保设置对话框中的设置更改 实际上是应用到日历上,但是,当我更改它们中的任何一个时, 以下错误不断出现(它已经在我的梦中萦绕):

^{pr2}$

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

Python Attribute Error: type object has no attribute

Type Object has no attribute

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

但我发现什么也解决不了我的问题(相信我,我试过了)。在

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

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


Tags: thetoimportselfforinitdeffunction
1条回答
网友
1楼 · 发布于 2024-10-02 00:23:15

您可以在函数中这样访问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上的更改时,您需要更改appSettings方法,并在MainWindow类中定义maximumDateChanged,如下所示

^{pr2}$

相关问题 更多 >

    热门问题