I have a form and a popup form as given below (partial code):
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 is my main form and MyPopupForm is the popup form. I need to do in such a way that, when I press a button it will print some string and display that string. When I press another button i have to invoke the same form but with different string. How could I do that (I used Qtdesigner to create UI)
MyPopupForm code in python:
# -*- 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))
The simplest way is to add a parameter to the
__init__method of the class MyPopupFormdef __init__(self, string_to_be_passed=None, parent=None):and then when you call it with
self.mypopup=MyPopupForm("value_to_display")using the string_to_be_passed in the
__init__method to display the value.Another method is to add a method to the class MyPopupForm to set the string to display and then
with the
setValueToDisplay()that display the string where needed.