我有一个表单和一个弹出窗体,如下所示(部分代码):
import sys
import subprocess
import os
from PyQt4 import QtCore, QtGui
from ui_auto import Ui_Form
from popup_ui import Ui_Form as fm
class MyPopupForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = fm()
self.ui.setupUi(self)
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
def when_pressed(self):
self.mypopup=MyPopupForm()
self.mypopup.show()
def when_stopped(self):
self.mypopup=MyPopupForm()
self.mypopup.show()
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))
答案 0 :(得分:2)
最简单的方法是在MyPopupForm类的__init__
方法中添加一个参数
def __init__(self, string_to_be_passed=None, parent=None):
然后用
调用它 self.mypopup=MyPopupForm("value_to_display")
使用__init__
方法中的string_to_be_passed显示值。
另一种方法是在MyPopupForm类中添加一个方法来设置要显示的字符串然后
self.mypopup=MyPopupForm()
self.mypopup.setValueToDisplay("value")
self.mypopup.show()
,setValueToDisplay()
显示需要的字符串。
答案 1 :(得分:2)
使用pyuic4
编译python模块时,我认为最好使用-w
选项,这将创建一个更简单的ui类,而不会产生setupUi
无意义。
这样做也意味着类具有您在Designer中给出的名称(而不是带有Ui_
前缀的令人讨厌的错误版本),如果需要,它也可以更简单地进行子类化。
下面,我添加了一个ui
文件,其中包含一个简单的Dialog
类,用于显示消息。还有一个演示如何使用它的脚本。
将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_())