I have been creating this calendar object with the possibility to 'assign' events - these are anything the user wants, from picking up milk from the corner shop to organising a wedding to setting out an exam timetable - to specific dates.
Here is a bare-bones version of my application code (written using Python 3.4.3 and PyQt 4.11.4) on which it's possible to recreate the error I continually experience (shown below):
# 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_())
The problem for me is as follows:
I am attempting to make sure the setting changes in the settings dialog actually apply to the calendar but, when I change any of them, the following error constantly appears (it's haunting my dreams already):
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'
What makes it worse is that I don't actually understand the error.
Does it mean calWidget isn't a property of MainWindow?
Is it because the Settings class is separate from the MainWindow class?
I've researched on this forum (and outside it) on anything that relates
on it, but nothing I found relates to my problem. My research includes:
Python Attribute Error: type object has no attribute
Error: type object 'Keys' has no attribute 'chord'
but nothing I found could solve my problem (believe me I tried).
Also, while I'm at it, how could I receive several inputs from the user through a dialog window? Would a dictionary of dictionaries be suitable or is there something else that could make my life easier? This is because I have yet to implement the function to add a new event related to a specific date and having a starting point would be a blessing - this whole situation has probably been an unnecessary nightmare. I also feel that if I get this fixed, I will be able to recreate the same structure in the other actions (which include adding, editing, saving and deleting one or all the events created).
Any help would be greatly appreciated. Thank you in advance.
calWidgetwithin the classmainWindow.