Frage

I want to invoke the form below ("First Script") from another script ("the Second Script"), and I want the Second script to populate the label field (presumably using the Qlabel's setText property) with new information.

The following 7 lines in the Second Script first produce the original window w/o the label updated; but after I close the non-updated window, a new window appears with the label updated. I don't know why the non-updated window appears first.

import Form2use4ques4stackoverflow_2013_02_23_am_09_33_45_
app = QApplication(sys.argv)
nuform = Form2use4ques4stackoverflow_2013_02_23_am_09_33_45_.Form()
nuform.prefix_label.setText('newtext')
nuform.show()
#return app.exec_()
app.exec_()
                                            Marc

# -*- coding: latin-1 -*-
"""
Form2use4ques4stackoverflow_2013_02_23_am_09_33_45_.py

"""

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import (Qt, SIGNAL)
from PyQt4.QtGui import (QApplication, QDialog, QHBoxLayout, QLabel,
        QPushButton)


class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.initUI()

    def initUI(self):
        okButton01 = QtGui.QPushButton("OK")
        cancelButton01 = QtGui.QPushButton("Cancel")

        prefix_label = QtGui.QLabel('Prefix') 
        self. prefix_label = prefix_label 

        hbox_prefix_digit_iterations = QtGui.QHBoxLayout()
        hbox_prefix_digit_iterations.addWidget(prefix_label)

        hbox_btnsOK_cancel = QtGui.QHBoxLayout()
        hbox_btnsOK_cancel.addStretch(1)
        hbox_btnsOK_cancel.addWidget(okButton01)
        hbox_btnsOK_cancel.addWidget(cancelButton01)

        vbox0 = QtGui.QVBoxLayout()
        vbox0.addLayout(hbox_prefix_digit_iterations)
        vbox0.addStretch(1)
        vbox0.addLayout(hbox_btnsOK_cancel)

        self.setLayout(vbox0)

        self.setGeometry(300, 300, 600, 300) #class PySide.QtCore.QRectF(left, top, width, height)   http://srinikom.github.com/pyside-docs/PySide/QtCore/QRectF.html#PySide.QtCore.QRectF
        self.setWindowTitle('Duplicate Code Strings W/Increasing Numbers')  
        self.show()


def formm():
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()


if 1 == 1:
    formm()
War es hilfreich?

Lösung

The error message tells it: You must create a QApplication before you can create the form. Basically, formm() from your first script contains everything you must do:

  1. Create a QApplication object
  2. Create the widgets to show
  3. Enter the event loop via QApplication::exec()

In the context of your second script, this would read like:

import Form2use4ques4stackoverflow_2013_02_23_am_09_33_45_
app = QApplication(sys.argv)
nuform = Form2use4ques4stackoverflow_2013_02_23_am_09_33_45_.Form()
nuform.prefix_label.setText('newtext')
nuform.show()
return app.exec()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top