Qt, Python Global problem



  • I wrote very simple python & Qt code.

    @from PyQt4 import QtCore, QtGui

    try:
    _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
    def _fromUtf8(s):
    return s

    try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig)

    class Ui_Form(object):

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(619, 501)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(60, 460, 98, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.comboBox = QtGui.QComboBox(Form)
        self.comboBox.setGeometry(QtCore.QRect(20, 410, 78, 27))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox_2 = QtGui.QComboBox(Form)
        self.comboBox_2.setGeometry(QtCore.QRect(120, 410, 78, 27))
        self.comboBox_2.setObjectName(_fromUtf8("comboBox_2"))
        self.comboBox_2.addItem(_fromUtf8(""))
        self.comboBox_2.addItem(_fromUtf8(""))
        self.comboBox_2.addItem(_fromUtf8(""))
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(20, 380, 81, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(120, 380, 91, 21))
        self.label_2.setObjectName(_fromUtf8("label_2"))
    
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
    
    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "BASLA", None))
        self.comboBox.setItemText(0, _translate("Form", "780", None))
        self.comboBox.setItemText(1, _translate("Form", "1350", None))
        self.comboBox.setItemText(2, _translate("Form", "1850", None))
        self.comboBox_2.setItemText(0, _translate("Form", "64", None))
        self.comboBox_2.setItemText(1, _translate("Form", "128", None))
        self.comboBox_2.setItemText(2, _translate("Form", "256", None))
        self.label.setText(_translate("Form", "USRP FRQ", None))
        self.label_2.setText(_translate("Form", "FFT BOYUTU", None))
    
    def deger(self):
        self.us=int(self.comboBox.currentText()) 
        #print "USRP Frekansı",self.us
        self.fft=int(self.comboBox_2.currentText()) 
        #print "FFT Boyutu",self.fft
      
    def basla(self):
    
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.deger)
    

    if name == "main":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    ui.basla()
    Form.show()
    print ui.us
    app.exec_()
    @

    When I'm running the program, I get an error message.

    @Traceback (most recent call last):
    File "./dene.py", line 88, in <module>
    print ui.us
    AttributeError: 'Ui_Form' object has no attribute 'us'.@

    I tried :

    @def deger():
    global us
    us=int(comboBox.currentText())@

    I am getting the following error at this time.:

    @File "./tt2.py", line 71, in <module>
    print us
    NameError: global name 'us' is not defined@

    What could be the cause of this error?
    How do I avoid this error?
    Help me, Please.



  • ui.us is not defined until you clicked a button.
    And you click button only after you call app.exec_()
    So you need to print ui.us in some other place like inside deger().
    Another option will be to define us in a constructor but it will be empty until you click a button.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.