I apply setText to a Qlabel of PyQt4 in Python and it does not modify the text in the tag but it is superimposed on the change

0

When I run the code a MainWindow appears in which there are two radioButtons, if I press either of the two, a container widget will be added and displayed in the main window, in it there will be several objects of which I care about the present labels , which will have a definite text, to re-press any of the radioButton a new text should appear on the labels however that is not what is happening, what happens is that the previous text is preserved and the new one is superimposed , that is, the texts are overlapping and the final result is a text incomprehensible to the user. How can I get the new text displayed by deleting the previous one, that's my problem.

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ui_adap1stubnew12

class stub1(QMainWindow,ui_adap1stubnew12.Ui_adap1s):
    def __init__(self, parent=None):
        QWidget.__init__(self,parent)
        self.setupUi(self)
        self.i=False
        self.band1=False
        self.band2=False

        self.connect(self.radioboton1, SIGNAL("clicked()"),self.llamarimpedancia)
        self.connect(self.radioboton2, SIGNAL("clicked()"),self.llamaradmitancia)

        def llamarimpedancia(self):        
        if self.i==False :
            print(self.i)
            self.band1=True
            self.myapp3=self.contenido(self.grupo)
            self.myapp3.show()
            self.i=True
        elif self.band2==True :
            print(self.i)
            self.band2=False
            self.band1=True
            self.myapp3=self.contenido(self.grupo)
            self.myapp3.show()
        else:
            print(self.i)
            self.band1=True
            self.myapp3=self.contenido(self.grupo)
            self.myapp3.show()#sin este comando no es mostrado en la ventana el nuevo widget

    def llamaradmitancia(self):
        if self.i==False :
            print(self.i)
            self.band2=True
            self.myapp3=self.contenido(self.grupo)
            self.myapp3.show()
            self.i=True
        elif self.band1==True :
            print(self.i)
            self.band1=False
            self.band2=True
            self.myapp3=self.contenido(self.grupo)
            self.myapp3.show()
        else:
            print(self.i)
            self.band2=True
            self.myapp3=self.contenido(self.grupo)
            self.myapp3.show()#sin este comando no es mostrado en la ventana el nuevo widget


    def contenido(self, parent):

        self.frame8 = QFrame(parent)
        self.frame8.setGeometry(QRect(11,110,214,374))
        #self.widget.setObjectName(_fromUtf8("widget"))
        self.verticalLayout_5 = QVBoxLayout(self.frame8)
        #self.verticalLayout_5.setObjectName(_fromUtf8("verticalLayout_5"))
        spacerItem2 = QSpacerItem(20, 18, QSizePolicy.Minimum,QSizePolicy.Expanding)
        self.verticalLayout_5.addItem(spacerItem2)
        self.verticalLayout_2 = QVBoxLayout()
        #self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.horizontalLayout_9 = QHBoxLayout()
         #self.horizontalLayout_9.setObjectName(_fromUtf8("horizontalLayout_9"))
        spacerItem3 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_9.addItem(spacerItem3)
        self.etiqueta2 = QLabel(self.frame8)
        self.etiqueta2.setScaledContents(False)
        self.etiqueta2.setWordWrap(False)
        #self.etiqueta2.setObjectName(_fromUtf8("etiqueta2"))
        self.horizontalLayout_9.addWidget(self.etiqueta2)
        spacerItem4 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_9.addItem(spacerItem4)
        self.verticalLayout_2.addLayout(self.horizontalLayout_9)
        self.horizontalLayout_3 = QHBoxLayout()
        #self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        self.etiqueta3 = QLabel(self.frame8)
        #self.etiqueta3.setObjectName(_fromUtf8("etiqueta3"))
        self.horizontalLayout_3.addWidget(self.etiqueta3)
        self.lineEdit1 = QLineEdit(self.frame8)
        self.lineEdit1.setMaximumSize(QSize(90, 16777215))
        #self.lineEdit1.setObjectName(_fromUtf8("lineEdit1"))
        self.horizontalLayout_3.addWidget(self.lineEdit1)
        self.etiqueta4 = QLabel(self.frame8)
        #self.etiqueta4.setObjectName(_fromUtf8("etiqueta4"))
        self.horizontalLayout_3.addWidget(self.etiqueta4)
        self.verticalLayout_2.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QHBoxLayout()
        #self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
        self.etiqueta5 = QLabel(self.frame8)
        #self.etiqueta5.setObjectName(_fromUtf8("etiqueta5"))
        self.horizontalLayout_4.addWidget(self.etiqueta5)
        self.lineEdit2 = QLineEdit(self.frame8)
        self.lineEdit2.setMaximumSize(QSize(90, 16777215))
        #self.lineEdit2.setObjectName(_fromUtf8("lineEdit2"))
        self.horizontalLayout_4.addWidget(self.lineEdit2)
        self.etiqueta6 = QLabel(self.frame8)
        #self.etiqueta6.setObjectName(_fromUtf8("etiqueta6"))
        self.horizontalLayout_4.addWidget(self.etiqueta6)
        self.verticalLayout_2.addLayout(self.horizontalLayout_4)
        self.verticalLayout_5.addLayout(self.verticalLayout_2)
        spacerItem5 = QSpacerItem(20, 13, QSizePolicy.Minimum,QSizePolicy.Expanding)
        self.verticalLayout_5.addItem(spacerItem5)
        self.verticalLayout_3 = QVBoxLayout()
        #self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
        self.horizontalLayout_10 = QHBoxLayout()
        #self.horizontalLayout_10.setObjectName(_fromUtf8("horizontalLayout_10"))
        spacerItem6 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_10.addItem(spacerItem6)
        self.etiqueta7 = QLabel(self.frame8)
        #self.etiqueta7.setObjectName(_fromUtf8("etiqueta7"))
        self.horizontalLayout_10.addWidget(self.etiqueta7)
        spacerItem7 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_10.addItem(spacerItem7)
        self.verticalLayout_3.addLayout(self.horizontalLayout_10)
        self.horizontalLayout_5 = QHBoxLayout()
        #self.horizontalLayout_5.setObjectName(_fromUtf8("horizontalLayout_5"))
        self.etiqueta8 = QLabel(self.frame8)
        #self.etiqueta8.setObjectName(_fromUtf8("etiqueta8"))
        self.horizontalLayout_5.addWidget(self.etiqueta8)
        self.lineEdit3 = QLineEdit(self.frame8)
        self.lineEdit3.setMaximumSize(QSize(90, 16777215))
        #self.lineEdit3.setObjectName(_fromUtf8("lineEdit3"))
        self.horizontalLayout_5.addWidget(self.lineEdit3)
        self.etiqueta9 = QLabel(self.frame8)
        #self.etiqueta9.setObjectName(_fromUtf8("etiqueta9"))
        self.horizontalLayout_5.addWidget(self.etiqueta9)
        self.verticalLayout_3.addLayout(self.horizontalLayout_5)
        self.horizontalLayout_6 = QHBoxLayout()
        #self.horizontalLayout_6.setObjectName(_fromUtf8("horizontalLayout_6"))
        self.etiqueta10 = QLabel(self.frame8)
        #self.etiqueta10.setObjectName(_fromUtf8("etiqueta10"))
        self.horizontalLayout_6.addWidget(self.etiqueta10)
        self.lineEdit4 = QLineEdit(self.frame8)
        self.lineEdit4.setMaximumSize(QSize(90, 16777215))
        #self.lineEdit4.setObjectName(_fromUtf8("lineEdit4"))
        self.horizontalLayout_6.addWidget(self.lineEdit4)
        self.etiqueta11 = QLabel(self.frame8)
        #self.etiqueta11.setObjectName(_fromUtf8("etiqueta11"))
        self.horizontalLayout_6.addWidget(self.etiqueta11)
        self.verticalLayout_3.addLayout(self.horizontalLayout_6)
        self.horizontalLayout_7 = QHBoxLayout()
        #self.horizontalLayout_7.setObjectName(_fromUtf8("horizontalLayout_7"))
        self.etiqueta12 = QLabel(self.frame8)
        #self.etiqueta12.setObjectName(_fromUtf8("etiqueta12"))
        self.horizontalLayout_7.addWidget(self.etiqueta12)
        self.lineEdit5 = QLineEdit(self.frame8)
        self.lineEdit5.setMaximumSize(QSize(90, 16777215))
        #self.lineEdit5.setObjectName(_fromUtf8("lineEdit5"))
        self.horizontalLayout_7.addWidget(self.lineEdit5)
        self.etiqueta13 = QLabel(self.frame8)
        #self.etiqueta13.setObjectName(_fromUtf8("etiqueta13"))
        self.horizontalLayout_7.addWidget(self.etiqueta13)
        self.verticalLayout_3.addLayout(self.horizontalLayout_7)
        self.horizontalLayout_8 = QHBoxLayout()
        #self.horizontalLayout_8.setObjectName(_fromUtf8("horizontalLayout_8"))
        self.etiqueta14 = QLabel(self.frame8)
        #self.etiqueta14.setObjectName(_fromUtf8("etiqueta14"))
        self.horizontalLayout_8.addWidget(self.etiqueta14)
        self.lineEdit6 = QLineEdit(self.frame8)
        self.lineEdit6.setMaximumSize(QSize(90, 16777215))
        #self.lineEdit6.setObjectName(_fromUtf8("lineEdit6"))
        self.horizontalLayout_8.addWidget(self.lineEdit6)
        self.etiqueta15 = QLabel(self.frame8)
        #self.etiqueta15.setObjectName(_fromUtf8("etiqueta15"))
        self.horizontalLayout_8.addWidget(self.etiqueta15)
        self.verticalLayout_3.addLayout(self.horizontalLayout_8)
        self.verticalLayout_5.addLayout(self.verticalLayout_3)
        spacerItem8 = QSpacerItem(20, 13, QSizePolicy.Minimum,QSizePolicy.Expanding)
        self.verticalLayout_5.addItem(spacerItem8)
        self.verticalLayout_7 = QVBoxLayout()
        #self.verticalLayout_7.setObjectName(_fromUtf8("verticalLayout_7"))
        self.horizontalLayout_14 = QHBoxLayout()
        #self.horizontalLayout_14.setObjectName(_fromUtf8("horizontalLayout_14"))
        spacerItem9 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_14.addItem(spacerItem9)
        self.boton1 = QPushButton(self.frame8)
        #self.boton1.setObjectName(_fromUtf8("boton1"))
        self.horizontalLayout_14.addWidget(self.boton1)
        spacerItem10 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_14.addItem(spacerItem10)
        self.verticalLayout_7.addLayout(self.horizontalLayout_14)
        self.horizontalLayout_15 = QHBoxLayout()
        #self.horizontalLayout_15.setObjectName(_fromUtf8("horizontalLayout_15"))
        spacerItem11 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_15.addItem(spacerItem11)
        self.boton2 = QPushButton(self.frame8)
        #self.boton2.setObjectName(_fromUtf8("boton2"))
        self.horizontalLayout_15.addWidget(self.boton2)    
        spacerItem12 = QSpacerItem(40, 20, QSizePolicy.Expanding,QSizePolicy.Minimum)
        self.horizontalLayout_15.addItem(spacerItem12)
        self.verticalLayout_7.addLayout(self.horizontalLayout_15)
        self.verticalLayout_5.addLayout(self.verticalLayout_7)   
        spacerItem13 = QSpacerItem(20, 18, QSizePolicy.Minimum,QSizePolicy.Expanding)
        self.verticalLayout_5.addItem(spacerItem13)


        if self.band1==True and self.band2==False :

            self.etiqueta2.setText("Carga Conectada")#tenia un problema para guardar
            self.etiqueta3.setText("R")#solucione, al copiar textualmente cada linea de estos setText
            self.etiqueta4.setText("a")
            self.etiqueta5.setText("X")
            self.etiqueta6.setText("a")
            self.etiqueta7.setText("Parametros de las lineas")#si coloco acentos el programa se cuelga
            self.etiqueta8.setText("Zo1")
            self.etiqueta9.setText("a")
            self.etiqueta10.setText("Zo2")
            self.etiqueta11.setText("a")
            self.etiqueta12.setText("f")
            self.etiqueta13.setText("MHz").o so
            self.etiqueta14.setText("Vf")
            self.etiqueta15.setText("%C")
            self.boton1.setText('Calcular')
            self.boton2.setText('Ayuda')
            print('Impedancia')

        elif self.band1==False and self.band2==True :

            self.etiqueta2.setText("Carga Conectada")#tenia un problema para guardar
            self.etiqueta3.setText("G")#solucione, al copiar textualmente cada linea de estos setText
            self.etiqueta4.setText("a")
            self.etiqueta5.setText("B")
            self.etiqueta6.setText("a")
            self.etiqueta7.setText("Parametros de las lineas")#si coloco acentos el programa se cuelga
            self.etiqueta8.setText("Yo1")
            self.etiqueta9.setText("a")
            self.etiqueta10.setText("Yo2")
            self.etiqueta11.setText("a")
            self.etiqueta12.setText("f")
            self.etiqueta13.setText("MHz")
            self.etiqueta14.setText("Vf")
            self.etiqueta15.setText("%C")
            self.boton1.setText('Calcular')
            self.boton2.setText('Ayuda')
            print('Admitancia')

        return self.frame8#sin esta linea, el show() invocado en el llamarimpedancia sera desconocido    

if __name__=="__main__": 
    app=QApplication(sys.argv)
    myapp=stub1()
    myapp.show()
    app.exec_()
    #sys.exit(app.exec_())
    
asked by Angel Vargas 03.07.2016 в 17:36
source

2 answers

1

It would be good if you give a smaller example I write you a simple example in which you overwrite the label when you press a button or a radio button

First file with the settings of the buttons and window is called tags

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(413, 243)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 140, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.radioButton = QtGui.QRadioButton(Dialog)
        self.radioButton.setGeometry(QtCore.QRect(90, 100, 82, 17))
        self.radioButton.setObjectName(_fromUtf8("radioButton"))
        self.radioButton_2 = QtGui.QRadioButton(Dialog)
        self.radioButton_2.setGeometry(QtCore.QRect(220, 100, 82, 17))
        self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(100, 30, 171, 31))
        self.label.setObjectName(_fromUtf8("label"))

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))
        self.radioButton.setText(_translate("Dialog", "RadioButton", None))
        self.radioButton_2.setText(_translate("Dialog", "RadioButton", None))
        self.label.setText(_translate("Dialog", "HOla mundo", None))

The second file that imports into the labels file is called labels

#Created on Thu Sep 01 21:57:06 2016

#author: Gabriel Asqui


import sys

from PyQt4 import QtGui,QtCore
from etiquetas import Ui_Dialog

class MiFormulario(QtGui.QDialog):
    def __init__(self,parent=None):  
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        #conectamos las señales con los eventos---------------------------
        QtCore.QObject.connect(self.ui.radioButton, QtCore.SIGNAL('isChecked()'), self.cambiar1)
        QtCore.QObject.connect(self.ui.radioButton_2, QtCore.SIGNAL('isChecked()'), self.cambiar2)
        QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.cambiar3)
        #definimos los eventos--------
    def cambiar1(self):
        self.ui.label.setText('Miau')
    def cambiar2(self):
        self.ui.label.setText('Guau')
    def cambiar3(self):
        self.ui.label.setText('Pio pio')

if __name__=="__main__": 
    app=0 #previene que el nucleo muera y tener que reiniciarlo cada vez
    app=QtGui.QApplication(sys.argv)
    myapp=MiFormulario()
    myapp.show()
    sys.exit(app.exec_())

The two files create them in the same directory and run the labels file. Greetings

    
answered by 02.09.2016 в 05:37
1

Make things easier, instead of looking for your widget directly since you are working specifically with only one label in this case.

In the first file class Ui_dialog define:

def setText(str)
   self.label.setText(str)

and from the main file

def cambiar1(self):
        self.ui.setText('Miau')
    def cambiar2(self):
        self.ui.setText('Guau')
    def cambiar3(self):
        self.ui.setText('Pio pio')
    
answered by 20.01.2017 в 02:37