0

I use the Spyder IDE (5.1)

I have an app structured like so. Basically, the app UI contains a button and when that button is clicked it triggers the function button1 which prompts the user for input and then calls func1 and func2 to do something to the input

The issue is I sometimes want to debug code and need to see the variables changing in func1 or func2 but those functions as well as button1 function aren't called until the button is pressed. The debugger reaches the end of the code before the UI window even appears (the window doesn't seem to appear until sys.exit(app.exec_()) )

I'm not super experienced with the debugger. How do you debug code structured like this with a UI?

def button1(self):

    def func1(input):
        *Do something*
        return

    def func2(input):
        *Do something*
         return 

    input=QFileDialog().getOpenFileName(self, "Select Input","","Excel (*.xlsx)")

     func1(input)
     func2(input)

    return

class Window(QMainWindow):
    def __init__(self):
        *UI layout*
        self.Button1=QPushButton("Button1")
        self.Button1.setStyleSheet("background-color: green")
        self.Button1.clicked.connect(lambda: button1(self))
        layout.addWidget(self.Button1)

if __name__ == "__main__":
        app = QApplication(sys.argv)
        QApplication.setStyle("Fusion")
        mainWin = Window()
        mainWin.show()
        sys.exit(app.exec_())
Gingerhaze
  • 664
  • 2
  • 5
  • 13
  • "The debugger reaches the end of the code before the UI window even appears": the fact that the debugger has reached the `app.exec` line doesn't mean that it has finished its job: it simply follows the flow of the program, which in your case is to define classes, evaluate the `if __name__`, create the application, an instance of the window and, most importantly, *start the event loop*, which is what allows you to show windows and interact with them. If you add a breakpoint in those functions, they will work when the functions are called. For basic debugging, just use print statements. – musicamante Nov 01 '21 at 14:33
  • Also, note that all static functions of `QFileDialog` (except for those related to directories) always return a *tuple* consisting of the (possibly empty) path of the selected file(s) and the file type filter. – musicamante Nov 01 '21 at 14:34
  • See also: [How to run PyQt applications within Spyder](https://github.com/spyder-ide/spyder/wiki/How-to-run-PyQt-applications-within-Spyder). – ekhumoro Nov 01 '21 at 14:34

0 Answers0