PyQt5 buttons not connecting

孤街浪徒 提交于 2021-02-05 11:27:25

问题


I am trying to build a simple GUI using PyQT5, with 3 buttons to open file browsers and one more to run processing with the selected files, but I can't get my buttons to connect to the functions needed to carry this out.

In the Ctrl class, the _connect_signals function doesn't seem to be calling _input_select. Can anyone help me figure out why?

import sys

# Import QApplication and the required widgets from PyQt5.QtWidgets
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QFileDialog


# Create a subclass of QMainWindow to setup the calculator's GUI
class UI(QMainWindow):
    """App's View (GUI)."""

    def __init__(self):
        """View initializer."""
        super().__init__()
        # Set some main window's properties
        self.setFixedSize(300, 150)
        # Set the central widget and the general layout
        self.generalLayout = QVBoxLayout()
        self._centralWidget = QWidget(self)
        self.setCentralWidget(self._centralWidget)
        self._centralWidget.setLayout(self.generalLayout)
        # Create the buttons
        self._create_buttons()

    def _create_buttons(self):
        """Create the buttons."""
        self.buttons = {}
        buttons_layout = QVBoxLayout()
        # Button text | position on the QVBoxLayout
        buttons = {
            "Select input file...": 0,
            "Select config file...": 1,
            "Select output file...": 2,
            "Run": 3,
        }
        # Create the buttons and add them to the grid layout
        for btnText, pos in buttons.items():
            self.buttons[btnText] = QPushButton(btnText)
            buttons_layout.addWidget(self.buttons[btnText], pos)
        # Add buttons_layout to the general layout
        self.generalLayout.addLayout(buttons_layout)


# Create a Controller class to connect the GUI and the model
class Ctrl:
    """App's Controller."""

    def __init__(self, setup, view):
        """Controller initializer."""
        self._view = view
        self._setup = setup
        # Connect signals and slots
        self._connect_signals()

    def _input_select(self):    # Not being called
        print("input selection")

        options = QFileDialog.Options()
        file_select, _ = QFileDialog.getOpenFileNames(
            self,
            'Select Input File...',
            '',
            'CSV Files (*.csv);;All Files (*)',
            options=options
        )
        if file_select:
            self._setup["input"] = file_select

    def _connect_signals(self):
        """Connect signals and slots."""
        self._view.buttons["Select input file..."].clicked.connect(self._input_select)  # Not working!


# Client code
def main():
    """Main function."""
    # Create an instance of `QApplication`
    app = QApplication(sys.argv)
    # Show the app's GUI
    view = UI()
    view.show()
    setup = {}
    # Create instance of the controller
    Ctrl(setup=setup, view=view)
    # Execute app's main loop
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

In case it helps, I started out by butchering this example code from a Real Python tutorial, but must have broken it along the way.


回答1:


The problem is that you are not keeping any persistent reference to the Ctrl() instance you are creating. This results in python garbage collecting it as soon as the instance is created.

To solve the issue, just assign it to a variable:

def main():
    """Main function."""
    # Create an instance of `QApplication`
    app = QApplication(sys.argv)
    # Show the app's GUI
    view = UI()
    view.show()
    setup = {}
    # Create instance of the controller
    ctrl = Ctrl(setup=setup, view=view)
    # Execute app's main loop
    sys.exit(app.exec_())

Some considerations:

  • while separating logic from interface is usually good practice, it's a concept that needs to be used with care, as sometimes it only makes things more complex than they should be. Most of the times (especially with simple programs), it only makes a bigger codebase without giving any actual benefit: it's harder to read and to debug, and you'll probably end up continuously switching from the logic parts and the ui parts of your code;
  • your code shows one of the drawback of that concept: when you create the file dialog, you're using self, but in that case it refers to the Ctrl instance, while the argument should be the UI instance instead (which will result in a crash, as Qt will get an unexpected argument type); you can use self._view instead, but, as said, the whole separation in this case just makes things unnecessarily complex;
  • using strings for dictionary keys that refer to internal objects is rarely a good idea (especially when using long descriptive strings like you did);
  • when importing more than one element from a module, it's usually better to group them instead of using single line imports: it makes the code tidier and easier to read and inspect: from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QFileDialog)


来源:https://stackoverflow.com/questions/64143060/pyqt5-buttons-not-connecting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!