ich habe ein Problem mit der Sichtbarkeit von Variablen: Ich habe drei verschiedene Module:
+ QueryStempelliste.py
+ SelectStempellisteViewDelegate.py
+ DialogSelectStempelliste.py
(Code-Ausschnitte habe ich unten angehängt.
Im Modul QueryStempelliste definiere ich einige Variablen (ID_STEMPEL, TYP, BEZEICHNUNG, ...), die ich als
Index für den Zugriff auf Elemente einer Liste mit meinen Daten brauche.
Das lustige ist nun, dass ich im Modul SelectStempellisteViewDelegate diese Variablen benutzen kann (Methode paint()).
Im Modul DialogSelectStempelliste in der Methode result() erhalte ich jedoch beim Versuch, auf die Variable
QueryStempelliste.ID_STEMPEL zuzugreifen eine Fehlermeldung:
Wenn ich stattTraceback (most recent call last):
File "./DialogSelectStempelliste.py", line 140, in <module>
stempelliste = select.result()
File "./DialogSelectStempelliste.py", line 79, in result
id_stempel = selected_entry[QueryStempelliste.ID_STEMPEL].toInt()[0]
AttributeError: type object 'QueryStempelliste' has no attribute 'ID_STEMPEL'
Code: Alles auswählen
id_stempel = selected_entry[QueryStempelliste.ID_STEMPEL].toInt()[0]
Code: Alles auswählen
id_stempel = selected_entry[0].toInt()[0]
Jetzt bin ich ziemlich ratlos: warum kann ich in einem Modul auf die Variablen zugreifen, in einem anderen
aber nicht? Der Import ist doch derselbe...
Modul QueryStempelliste.py:
Code: Alles auswählen
from PyQt4 import QtCore, QtSql
from sw_exception_classes import DatabaseError, KeyNotFound, TypeNotSupported, \
IndexOutOfRange
NUMBER_OF_FIELDS = 8
ID_STEMPEL, TYP, BEZEICHNUNG, VON, BIS, KW, JAHR, STATUS = range(NUMBER_OF_FIELDS)
class QueryStempelliste(object):
...
Code: Alles auswählen
from select_stempelliste import QueryStempelliste
from PyQt4 import QtGui, QtCore
class SelectStempellisteViewDelegate(QtGui.QAbstractItemDelegate):
def __init__(self):
...
def paint(self, painter, option, index):
...
painter.drawText(rahmen.x()+self.indent, rahmen.top() + 2 * \
self.lineheight, QtCore.QString("KW %1/%2 vom %3 bis zum %4").\
arg(entry[QueryStempelliste.KW].toString()).\
arg(entry[QueryStempelliste.JAHR].toString()).\
arg(entry[QueryStempelliste.VON].toString()).\
arg(entry[QueryStempelliste.BIS].toString()))
Code: Alles auswählen
from select_stempelliste import QueryStempelliste, SelectStempellisteViewDelegate, \
SelectStempellisteViewModel, SelectedStempelliste, AffectedFlight
from PyQt4 import QtGui, QtCore, QtSql
from ui_select_stempelliste import Ui_select_stempelliste as DlgSelectStempelliste
class DialogSelectStempelliste(QtGui.QDialog, DlgSelectStempelliste):
def __init__(self, parent=None):
...
def result(self):
...
if self.result_list.currentIndex().isValid():
selected_entry = self.result_list.currentIndex().data().toList()
id_stempel = selected_entry[QueryStempelliste.ID_STEMPEL].toInt()[0]
return SelectedStempelliste(id_stempel)