简体   繁体   English

pyqt4 python QT全球

[英]pyqt4 python QT global

I wrote very simple python & Qt code. 我写了非常简单的python&Qt代码。

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 print ui.us AttributeError: 'Ui_Form' object has no attribute 'us'. 追溯(最近一次呼叫最近):打印ui.us中的文件“ ./dene.py”,第88行,AttributeError:'Ui_Form'对象没有属性'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 print us NameError: global name 'us' is not defined 我们正在打印文件“ ./tt2.py”,行71,NameError:未定义全局名称“ us”

What could be the cause of this error? 造成此错误的原因是什么? How do I avoid this error? 如何避免此错误? Help me, Please 请帮帮我

Firstly, don't use globals. 首先,不要使用全局变量。

On your problem. 关于你的问题。 You get import error because self.us is not assigned before your print statement (you didn't call method "deger"). 您收到导入错误,因为在打印语句之前未分配self.us(您未调用方法“ deger”)。 Anyway, you should create ui files using PyQt ui generator, then you wouldn't have such errors. 无论如何,您应该使用PyQt ui生成器创建ui文件,这样就不会出现此类错误。

I have altered a bit your code. 我修改了您的代码。 Now it doesn't give an error message anymore, and you get the contents of the combo boxes printed when you click the button. 现在,它不再发出错误消息,并且单击按钮后,您将获得打印组合框的内容。 If you still have any problems, please let me know. 如果仍然有任何问题,请告诉我。 I would be happy to help you. 我很乐意为您服务。

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 __init__(self):
        self.us = None

    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.pushButton.clicked.connect(self.buttonAction)
        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 buttonAction(self):
        print("The button has been clicked")
        print("Text in comboBox 1: " + self.comboBox.currentText())
        print("Text in comboBox 2: " + self.comboBox_2.currentText())


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    print(ui.us)   
    app.exec_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM