Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为Qt中的变量赋值_Python_Qt - Fatal编程技术网

Python 为Qt中的变量赋值

Python 为Qt中的变量赋值,python,qt,Python,Qt,以下是转换为.py的Qt的.ui: from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: _fromUtf8 = lambda s: s class Ui_NewWindow(object): def setupUi(self, NewWindow): NewWindow.setObjectName(_fromUtf8("NewWind

以下是转换为.py的Qt的.ui:

from PyQt4 import QtCore, QtGui

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_NewWindow(object):
  def setupUi(self, NewWindow):
    NewWindow.setObjectName(_fromUtf8("NewWindow"))
    NewWindow.resize(439, 225)
    self.centralwidget = QtGui.QWidget(NewWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
    self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
    self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
    self.widget = QtGui.QWidget(self.centralwidget)
    self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
    self.widget.setObjectName(_fromUtf8("widget"))
    self.verticalLayout = QtGui.QVBoxLayout(self.widget)
    self.verticalLayout.setMargin(0)
    self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
    self.horizontalLayout_2 = QtGui.QHBoxLayout()
    self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
    self.label_3 = QtGui.QLabel(self.widget)
    self.label_3.setObjectName(_fromUtf8("label_3"))
    self.horizontalLayout_2.addWidget(self.label_3)
    self.lineEdit = QtGui.QLineEdit(self.widget)
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    self.horizontalLayout_2.addWidget(self.lineEdit)
    self.label_4 = QtGui.QLabel(self.widget)
    self.label_4.setObjectName(_fromUtf8("label_4"))
    self.horizontalLayout_2.addWidget(self.label_4)
    self.lineEdit_2 = QtGui.QLineEdit(self.widget)
    self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
    self.horizontalLayout_2.addWidget(self.lineEdit_2)
    self.verticalLayout.addLayout(self.horizontalLayout_2)
    self.pushButton = QtGui.QPushButton(self.widget)
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.verticalLayout.addWidget(self.pushButton)
    self.horizontalLayout = QtGui.QHBoxLayout()
    self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
    self.label = QtGui.QLabel(self.widget)
    self.label.setObjectName(_fromUtf8("label"))
    self.horizontalLayout.addWidget(self.label)
    self.label_2 = QtGui.QLabel(self.widget)
    self.label_2.setObjectName(_fromUtf8("label_2"))
    self.horizontalLayout.addWidget(self.label_2)
    self.verticalLayout.addLayout(self.horizontalLayout)
    NewWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(NewWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    NewWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(NewWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    NewWindow.setStatusBar(self.statusbar)

    self.retranslateUi(NewWindow)
    QtCore.QMetaObject.connectSlotsByName(NewWindow)

  def retranslateUi(self, NewWindow):
    NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow",    None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
    self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer  : ", None, QtGui.QApplication.UnicodeUTF8))
    self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))

if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  NewWindow = QtGui.QMainWindow()
  ui = Ui_NewWindow()
  ui.setupUi(NewWindow)
  NewWindow.show()
  sys.exit(app.exec_())
运行上述python程序后,窗口显示为:

但我必须将行编辑中的值指定给变量A和B,以便将它们添加为:

sum = A + B

当我们点击上面窗口中的“添加”按钮时,我已经在标签答案中显示了这个值
sum
,因此请建议对上述代码进行适当的更改。

您需要创建一个新闻操作(
ad\u value
)。然后,您需要将此操作连接到按钮信号(例如,
单击的
)。信号发出后(即单击按钮),操作从
lineEdit
lineEdit_2
读取值,将它们相加,并设置
label_2
-小部件的文本(您可以先将值转换为int[参见其他答案了解简单实现])

我正在使用PySide,但它也应该与PyQT一起使用

你可以像这样做:

def sum(self):
  summed = float(self.lineEdit.text()) + float(self.lineEdit_2.text())
  self.label_2.setText(str(summed))

我使用PyQt尝试了上面修改过的代码,但它只添加了文本。意思是A=20,B=25,那么答案是2025,而不是45。是的,对不起。只要把内贾特的解决方案和我的结合起来就行了。看看我的更正。嘿,如果你知道,请告诉我。我已将.ui文件转换为.py,这是通过单击按钮打开新窗口的程序。但问题是程序只在Qt上运行,但当我尝试运行转换后的.py代码时,第一个主窗口正在打开,但单击按钮后什么也没有发生,这意味着另一个窗口没有打开。嗯,也许你应该在另一个问题中问这个,提供完整代码@dorvak:这是链接:请看一看。
def sum(self):
  summed = float(self.lineEdit.text()) + float(self.lineEdit_2.text())
  self.label_2.setText(str(summed))