0

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I'm using global variables because I need to use it in other functions ...... Its not working... System returns me: Process finished with exit code -1073740791 (0xC0000409) – Felipe Clemente Mar 09 '21 at 17:10
  • @FelipeClemente 1) I think you have implemented it incorrectly so if you want more help then provide a [MRE]. I recommend running your script in the CMD or console to get a more descriptive error message than just a numerical code. 2) why don't you use attributes of the class or pass it as a parameter? The big problem with global variables is that if there is an error they generate silent errors that are difficult to debug. – eyllanesc Mar 09 '21 at 17:12
  • "I'm using global variables because I need to use it in other function" is exactly one of the main reasons behind using classes and instances. For instance, instead of using `global dom; dom = ET.parse(fileName)`, use `self.dom = ET.parse(fileName)`, and then you can access `self.dom` from anywhere in the instance of that class, or from anywhere else as long as you have access to that instance: if the instance is `myWindow`, then it would be `myWindow.dom`. If you don't have access to that instance, then the problem is in your project structure, and should not be solved by using globals. – musicamante Mar 09 '21 at 18:03
  • thx everybody, I've followed yours advices and solved the problem... just self.labelXMLFile.adjusSize() is not working for me – Felipe Clemente Mar 10 '21 at 11:31