Felipe Clemente
Felipe Clemente

Reputation: 57

Changing value but the new value write over last value

I have a button that load a file system and write in a label from where is the file, but when I press a second time the button, the programoverwrite the label. I already tried to update(), replace() and so on unsuccessful. follow bellow my code:

def btnLoadXML_click(self):

    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    fileName, _ = QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "",
                                              "XML Files (*.xml);;All Files (*)", options=options)
    global dom, NameFile

    if fileName:
        print(fileName)
        NameFile = fileName
    dom = ET.parse(fileName)
    ########label here
    labelXMLFile = QLabel(NameFile, self)
    labelXMLFile.setStyleSheet('QLabel {font:bold;font-size:20px}')
    labelXMLFile.resize(800, 50)
    labelXMLFile.move(300, 50)
    labelXMLFile.show()

enter image description here

Upvotes: 0

Views: 31

Answers (1)

eyllanesc
eyllanesc

Reputation: 244291

The problem is that you are creating a new QLabel every time you update the text so you see this behavior, instead just create a QLabel in the constructor and update the text:

def __init__(self, ...):
    # ...
    self.labelXMLFile = QLabel(self)
    self.labelXMLFile.setStyleSheet('QLabel {font:bold;font-size:20px}')
    self.labelXMLFile.move(300, 50)
    self.labelXMLFile.show()
def btnLoadXML_click(self):
    # ...
    ########label here
    self.labelXMLFile.setText(NameFile)
    self.labelXMLFile.adjusSize()

Note: it is not advisable to abuse global variables as indicated in Why are global variables evil?

Upvotes: 2

Related Questions