Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Crash when calling Python script with exec() from QMenuBar
QtWS25 Last Chance

Crash when calling Python script with exec() from QMenuBar

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 430 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrigeoy
    wrote on last edited by
    #1

    I am writing my first application using PyQt5 and I have come across some unexpected behaviour.
    The PyQt5 version is 5.15.6 and I am running Python 3.9.10 in PyCharm on Windows 11.

    The 3 scripts show what is happening.

    • Running "number_list.py" directly is good.
    • Running "test1.py" does what I would expect and runs "number_list.py"
    • Running "test2.py" and selecting 'File/Show Number List' will open "number_list.py" but then crashes.

    number_list.py

    from PyQt5.QtWidgets import \
        QWidget, \
        QApplication, \
        QListView, \
        QHBoxLayout
    from PyQt5.QtGui import \
        QStandardItem, \
        QStandardItemModel
    import sys
    
    class window(QWidget):
        def __init__(self):
            super().__init__()
            numList = QListView()
            model = QStandardItemModel()
            numList.setModel(model)
            for x in range(10):
                model.appendRow(QStandardItem(str(x)))
            layout = QHBoxLayout(self)
            layout.addWidget(numList)
            self.show()
    
    if __name__ == '__main__':
        print('Hello World!')
        app = QApplication(sys.argv)
        win = window()
        sys.exit(app.exec_())
    

    test1.py

    if __name__ == '__main__':
        exec(open('<path>/number_list.py').read())
    

    test2.py

    from PyQt5.QtWidgets import \
        QWidget, \
        QMainWindow, \
        QApplication, \
        QAction, \
        QListView, \
        QHBoxLayout
    from PyQt5.QtGui import \
        QStandardItem, \
        QStandardItemModel
    import sys
    
    class window(QMainWindow):
        def __init__(self):
            super().__init__()
            menu = self.menuBar()
            fileMenu = menu.addMenu('&File')
            showList = QAction('&Show Number List', self)
            showList.triggered.connect(self.run_test2)
            fileMenu.addAction(showList)
            self.show()
    
        def run_test2(self):
            exec(open('<path>/number_list.py').read())
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        win = window()
        sys.exit(app.exec_())
    
    

    The debugger in PyCharm doesn't seem to be any help.

    Console Tab

    Connected to pydev debugger (build 213.7172.26)
    Hello World!
    
    Process finished with exit code -1073741819 (0xC0000005)
    

    And the Debugger Tab has no traceback, only "Frames are not available".

    Any comments or fixes would be greatly appreciated.

    1 Reply Last reply
    0
    • CristianMaureiraC Offline
      CristianMaureiraC Offline
      CristianMaureira
      wrote on last edited by
      #2

      That's not what exec is designed for.
      If you want to run other python scripts (or scripts in general) use https://docs.python.org/3/library/subprocess.html but in your case, it might be worth for you just to import the class you want to use from the other file, and then show it on the main python file, for example: from number_list import window from your test1.py file, and then using what you have inside the if __name__ == "__main__" but from test1.py.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrigeoy
        wrote on last edited by
        #3

        @CristianMaureira Thanks for your reply and for pointing me in the right direction.
        My searches all came up with exec as the way to do this and it seems to work most of the time.
        subprocess is working now.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved