【发布时间】: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
Error: type object 'Keys' has no attribute 'chord'
但我发现没有任何东西可以解决我的问题(相信我我试过了)。
另外,当我使用它时,如何通过对话窗口接收来自用户的多个输入?词典词典是否合适,或者有其他东西可以让我的生活更轻松吗?这是因为我还没有实现添加与特定日期相关的新事件的功能,并且有一个起点将是一件幸事——整个情况可能是一场不必要的噩梦。我还觉得如果我解决了这个问题,我将能够在其他操作中重新创建相同的结构(包括添加、编辑、保存和删除一个或所有创建的事件)。
任何帮助将不胜感激。提前谢谢你。
【问题讨论】:
-
这是否意味着 calWidget 不是 mainWindow 的属性? - 是的
-
但我在
mainWindow类中创建了calWidget。
标签: python python-3.x pyqt4