Seite 1 von 1

comboBox: sobald ich in ihr etwas auswähle "Fehler"

Verfasst: Mittwoch 8. November 2017, 10:47
von Dank
Hi ich bin noch am anfang und lerne gerade erst Python und Qt. Mein problem ist die comboBox erzeugt immer einen fehler nach dem auswählen eines Objektes in der Gui.

Der Fehler:
Traceback (most recent call last):
File "/home/pi/pyqt5.py", line 48, in choosen
for count in range(self.box.count()):
AttributeError: 'Fenster' object has no attribute 'box'



Mein Code:

Code: Alles auswählen

import sys
from PyQt4.QtCore import*
from PyQt4.QtGui import*
from PyQt4.uic import*

class Fenster(QWidget):
    def __init__(self):
        super().__init__()
        self.initMe()
        
        
    def initMe(self):
        button = QRadioButton("X",self)
        button2 = QRadioButton("Y",self)
        button3 = QRadioButton("Z",self)
        
        box = QComboBox(self)
        box.addItem("Motor1")
        box.addItem("Motor2")
        box.addItem("Motor3")
        box.addItem("C")
        
        button.move(50,50)
        button2.move(50,200)
        button3.move(200,200)
        button.clicked.connect(self.gedrueckt1)
        button2.clicked.connect(self.gedrueckt2)
        button3.clicked.connect(self.gedrueckt3)
        
        box.currentIndexChanged.connect(self.choosen)
        self.setGeometry(50,50,800,480)
        self.setWindowTitle("H")
        self.show()
        print (box.count)
        
    def gedrueckt1(self):
        print("X")
        
    def gedrueckt2(self):
        print("y")

    def gedrueckt3(self):
        print("Z")
        
    def choosen(self,i):
        print ("Items in the list are :")
		
        for count in range(self.box.count()):
            print (box.itemText(count))
            print ("Current index",i,"selection changed ",self.box.currentText())





app= QApplication(sys.argv)
w= Fenster()
sys.exit(app.exec_())

Re: comboBox: sobald ich in ihr etwas auswähle "Fehler"

Verfasst: Mittwoch 8. November 2017, 12:02
von Sirius3
@Dank: was erwartest Du? Du bindest niemals `box` an als Attribut an die Fenster-Instanz. `box` existiert nur als lokale Variable in `initMe`.

Löse `initMe` auf und verschiebe alles darin nach `__init__`. Binde die Elemente, die Du später noch einmal brauchst an `self`. Fenster sollten sich nicht selbst anzeigen, veschiebe also das `self.show` nach unten und rufe `w.show()` auf.

Vermeide *-Importe, gib explizit an, was Du aus PyQt4.QtGui und andere brauchst.

Re: comboBox: sobald ich in ihr etwas auswähle "Fehler"

Verfasst: Mittwoch 8. November 2017, 13:21
von Dank
genau sowas erwarte ich.
danke ich werde das mal jetzt mal umsetzen.