Pyqt基于用户操作向窗体传递参数

Pyqt基于用户操作向窗体传递参数,qt,pyqt,pyqt4,Qt,Pyqt,Pyqt4,我有一个表单和一个弹出表单,如下所示(部分代码): Myform是我的主窗体,MyPopupForm是弹出窗体。我需要这样做,当我按下一个按钮时,它将打印一些字符串并显示该字符串。当我按下另一个按钮时,我必须用不同的字符串调用相同的表单。我该怎么做(我使用Qtdesigner创建UI) python中的MyPopupForm代码: # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'popu

我有一个表单和一个弹出表单,如下所示(部分代码):

Myform是我的主窗体,MyPopupForm是弹出窗体。我需要这样做,当我按下一个按钮时,它将打印一些字符串并显示该字符串。当我按下另一个按钮时,我必须用不同的字符串调用相同的表单。我该怎么做(我使用Qtdesigner创建UI)

python中的MyPopupForm代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'popup.ui'
#
# Created: Sun Jan  8 11:18:43 2012
#      by: PyQt4 UI code generator 4.7.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(207, 170)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(60, 120, 92, 27))
        self.pushButton.setObjectName("pushButton")
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(58, 30, 81, 41))
        #self.label.setText("")
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), Form.close)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "OK", None, QtGui.QApplication.UnicodeUTF8))

最简单的方法是向MyPopupForm类的
\uuuu init\uuuu
方法添加一个参数

def\uuuu init\uuuu(self,string\u to\u be\u passed=None,parent=None):

然后当你把它叫做

self.mypopup=MyPopupForm(“值到显示”)

使用
\uuuu init\uuu
方法中传递的字符串\u to\u be\u来显示值

另一种方法是向MyPopupForm类添加一个方法,以设置要显示的字符串,然后

self.mypopup=MyPopupForm()
self.mypopup.setValueToDisplay("value")
self.mypopup.show()

使用
setValueToDisplay()
在需要的地方显示字符串。

最简单的方法是向类MyPopupForm的
\uuuuu init\uuuuu
方法添加一个参数

def\uuuu init\uuuu(self,string\u to\u be\u passed=None,parent=None):

然后当你把它叫做

self.mypopup=MyPopupForm(“值到显示”)

使用
\uuuu init\uuu
方法中传递的字符串\u to\u be\u来显示值

另一种方法是向MyPopupForm类添加一个方法,以设置要显示的字符串,然后

self.mypopup=MyPopupForm()
self.mypopup.setValueToDisplay("value")
self.mypopup.show()

使用
setValueToDisplay()
在需要的地方显示字符串。

使用
pyuic4
编译python模块时,我认为最好使用
-w
选项,这将创建一个简单得多的ui类,而不需要所有的
setupUi
废话

这样做还意味着类的名称与您在Designer中给它的名称完全相同(而不是带有
Ui\uu
前缀的糟糕版本),如果需要,对它进行子类化也要简单得多

下面,我包含了一个
ui
文件,其中包含一个简单的
对话框
类,用于显示消息。还有一个脚本演示如何使用它

ui
文件另存为
dialog.ui
,然后执行以下操作:

pyuic4 -w dialog.ui > dialog.py
编译python模块。然后从同一目录运行脚本

用户界面文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>125</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QLabel" name="labelMessage">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="buttonClose">
     <property name="text">
      <string>Close</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonClose</sender>
   <signal>clicked()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>194</x>
     <y>107</y>
    </hint>
    <hint type="destinationlabel">
     <x>402</x>
     <y>88</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>
from PyQt4 import QtGui, QtCore
from dialog import Dialog

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.buttonOne = QtGui.QPushButton('Test One', self)
        self.buttonOne.clicked.connect(self.handleButtonOne)
        self.buttonTwo = QtGui.QPushButton('Test Two', self)
        self.buttonTwo.clicked.connect(self.handleButtonTwo)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.buttonOne)
        layout.addWidget(self.buttonTwo)

    def showDialog(self, message):
        dialog = Dialog(self)
        dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.labelMessage.setText(message)
        dialog.show()

    def handleButtonOne(self):
        self.showDialog('This is the message for Button One')

    def handleButtonTwo(self):
        self.showDialog('This is the message for Button Two')

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

在使用
pyuic4
编译python模块时,我认为最好使用
-w
选项,这将创建一个简单得多的ui类,而不需要所有的
setupUi
废话

这样做还意味着类的名称与您在Designer中给它的名称完全相同(而不是带有
Ui\uu
前缀的糟糕版本),如果需要,对它进行子类化也要简单得多

下面,我包含了一个
ui
文件,其中包含一个简单的
对话框
类,用于显示消息。还有一个脚本演示如何使用它

ui
文件另存为
dialog.ui
,然后执行以下操作:

pyuic4 -w dialog.ui > dialog.py
编译python模块。然后从同一目录运行脚本

用户界面文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>125</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QLabel" name="labelMessage">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="buttonClose">
     <property name="text">
      <string>Close</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonClose</sender>
   <signal>clicked()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>194</x>
     <y>107</y>
    </hint>
    <hint type="destinationlabel">
     <x>402</x>
     <y>88</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>
from PyQt4 import QtGui, QtCore
from dialog import Dialog

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.buttonOne = QtGui.QPushButton('Test One', self)
        self.buttonOne.clicked.connect(self.handleButtonOne)
        self.buttonTwo = QtGui.QPushButton('Test Two', self)
        self.buttonTwo.clicked.connect(self.handleButtonTwo)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.buttonOne)
        layout.addWidget(self.buttonTwo)

    def showDialog(self, message):
        dialog = Dialog(self)
        dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.labelMessage.setText(message)
        dialog.show()

    def handleButtonOne(self):
        self.showDialog('This is the message for Button One')

    def handleButtonTwo(self):
        self.showDialog('This is the message for Button Two')

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

我使用qt设计器来设计表单。我在
self.ui=fm()
之后给出了
self.ui.setText(val)
。现在它给了我一个错误
AttributeError:'Ui_Form'对象没有属性'setText'
你应该使用
self.Ui..setText(val)
,其中
是QLabel或QLineEdit,或者任何你用来显示文本的控件(其中哪一个必须有
setText()
method。您编写的代码,按照您的代码示例,试图调用
MyPopupForm
的方法
setText()
,这是一个QWidget没有的方法。很抱歉,这个转储问题。我只是一个初学者。我给出了
self.ui.QLabel.setText(val)
在mypopupform类中,并获取错误
AttributeError:“Ui_Form”对象没有属性“QLabel”
QLabel是类的名称,而不是小部件的名称。如果使用Qt设计器,请在属性窗口中检查小部件的属性
objectName
中的QLabel名称。如果不更改名称,则为可能
label
我使用QtDesigner,名称是label。因此,当我这样调用
self.ui.label.setText(val)
,我得到错误
AttributeError:'ui\u Form'对象没有属性'label'。问题中给出了弹出式表单pythoc代码。
我使用QtDesigner设计表单。我给出了
self.ui.setText(val)
self.ui=fm()
之后。它现在给了我一个错误
AttributeError:'ui_Form'对象没有属性'setText'
你应该使用
self.ui..setText(val)
其中
是QLabel或QLineEdit,或者是你用来显示文本的任何控件(你的哪一个必须有
setText()
method。您编写的代码,按照您的代码示例,试图调用
MyPopupForm
的方法
setText()
,这是一个QWidget没有的方法。很抱歉,这个转储问题。我只是一个初学者。我给出了
self.ui.QLabel.setText(val)
在mypopupform类中,并获取错误
AttributeError:“Ui_Form”对象没有属性“QLabel”
QLabel是类的名称,而不是小部件的名称。如果使用Qt设计器,请在属性窗口中检查小部件的属性
objectName
中的QLabel名称。如果不更改名称,则为可能
label
我使用QtDesigner,名称是label。因此,当我这样调用
self.ui.label.setText(val)
,我得到错误
AttributeError:'ui\u Form'对象没有属性'label'。问题中给出了弹出式表单pythoc代码。