Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Combobox query with text file



  • Hello, if I try to match the text from the combobox with the text from my text file, the program crashes, someone can help me ? Thanks.

        def four(self):
            with open("Slot1.txt", "r") as f:
                file_text1 = f.readline()
                combotxt = comboBox.currentText()
    
                file_text1 == combotxt
                print("true")
    

  • Lifetime Qt Champion

    Hi,

    What is the crash back trace ?



  • hi,
    Python has stopped working


  • Lifetime Qt Champion

    That's not a backtrace.

    You should also provide a minimal example that triggers that crash.

    Also, what version of Python are you using ?
    Are you using PyQt5 or PySide2 ?
    Which version ?



  • As soon as I call up the def with my button, the program crashes and I don't get an error message.
    PyQt5
    Version 3.7.4


  • Lifetime Qt Champion

    Where is Slot1.txt located ?
    What version of PyQt5 ?
    What OS are you running ?



  • in the same folder as the py
    windows 5.14.1



  • in the txt file just stands "hello" und genauso in der combobox.
    I want to ask if the texts are the same.



  • *same in the combobox



  • @Julsenn
    Python does not usually "crash". If the code you show really crashes, the most likely would be if comboBox is not set to a QComboBox, but even then I'd be surprised.

    If the file cannot be opened for read, the open() will raise an exception. Again this should not cause a "crash", but I don't know what your code does if an exception is raised.

                file_text1 == combotxt
                print("true")
    

    This always prints true; the first line does nothing. I'm sure you know how to write an if statement correctly in Python.

    If you want to find out how far your code gets before "crashing", and you're not using a debugger, put a print(0) before the first statement, print(1) before the next statement, and so on. We have all done that before!

    I can't remember whether we have discussed this before, but how/where do you write your Python program code? Do you just sit in a text editor (e.g. Notepad++)? You really needs to sit in an "IDE", which will provide help when writing code but also a visual Python debugger for you to use. There is, for example, PyCharm, which I like, or MS VSCode with Python support loaded in, both of which provide debuggers. You really should switch over to an IDE if you are not using one, it will be the best move you ever make!



  • Hello I have now switched to PyCharm and have localized my error with the print query. Thank you @JonB



  • At this point the Process finished.

    combotxt = QComboBox.currentText()
    

  • Lifetime Qt Champion

    You are calling an instance method on a class.



  • @SGaist
    Thanks I found my problem

    Now it works

            with open("Slot8.txt", "r") as f:
                file_text8 = f.readline()
    
                comboText = self.comboBox.currentText()
                if file_text1 == comboText:
                    self.slot1_4cl()
    

Log in to reply