久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='ylvu8'></tfoot>

<small id='ylvu8'></small><noframes id='ylvu8'>

      <bdo id='ylvu8'></bdo><ul id='ylvu8'></ul>
    1. <legend id='ylvu8'><style id='ylvu8'><dir id='ylvu8'><q id='ylvu8'></q></dir></style></legend>
      <i id='ylvu8'><tr id='ylvu8'><dt id='ylvu8'><q id='ylvu8'><span id='ylvu8'><b id='ylvu8'><form id='ylvu8'><ins id='ylvu8'></ins><ul id='ylvu8'></ul><sub id='ylvu8'></sub></form><legend id='ylvu8'></legend><bdo id='ylvu8'><pre id='ylvu8'><center id='ylvu8'></center></pre></bdo></b><th id='ylvu8'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ylvu8'><tfoot id='ylvu8'></tfoot><dl id='ylvu8'><fieldset id='ylvu8'></fieldset></dl></div>
    2. PyQt5按鈕未連接

      PyQt5 buttons not connecting(PyQt5按鈕未連接)
      <tfoot id='uKKoK'></tfoot>
    3. <legend id='uKKoK'><style id='uKKoK'><dir id='uKKoK'><q id='uKKoK'></q></dir></style></legend>

          • <bdo id='uKKoK'></bdo><ul id='uKKoK'></ul>

              <tbody id='uKKoK'></tbody>
            <i id='uKKoK'><tr id='uKKoK'><dt id='uKKoK'><q id='uKKoK'><span id='uKKoK'><b id='uKKoK'><form id='uKKoK'><ins id='uKKoK'></ins><ul id='uKKoK'></ul><sub id='uKKoK'></sub></form><legend id='uKKoK'></legend><bdo id='uKKoK'><pre id='uKKoK'><center id='uKKoK'></center></pre></bdo></b><th id='uKKoK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='uKKoK'><tfoot id='uKKoK'></tfoot><dl id='uKKoK'><fieldset id='uKKoK'></fieldset></dl></div>

                <small id='uKKoK'></small><noframes id='uKKoK'>

                本文介紹了PyQt5按鈕未連接的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在嘗試使用 PyQT5 構建一個簡單的 GUI,有 3 個按鈕用于打開文件瀏覽器,還有一個按鈕用于對所選文件進行處理,但我無法讓我的按鈕連接到執行此操作所需的功能.

                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.

                Ctrl 類中,_connect_signals 函數似乎沒有調用 _input_select.誰能幫我弄清楚為什么?

                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()
                
                

                以防萬一,我開始屠殺 這個示例代碼來自一個真正的 Python 教程,但一定是在這個過程中破壞了它.

                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.

                推薦答案

                問題是您沒有保留對您正在創建的 Ctrl() 實例的任何持久引用.這會導致 python 垃圾在實例創建后立即收集它.

                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_())
                

                一些注意事項:

                • 雖然將邏輯與接口分離通常是一種很好的做法,但它是一個需要謹慎使用的概念,因為有時它只會使事情變得比應有的復雜.大多數時候(尤其是對于簡單的程序),它只會創建更大的代碼庫而沒有帶來任何實際好處:它更難閱讀和調試,并且您可能最終會不斷地從您的邏輯部分和 ui 部分切換代碼;
                • 您的代碼顯示了該概念的一個缺點:當您創建文件對話框時,您使用的是 self,但在這種情況下,它指的是 Ctrl實例,而參數應該是 UI 實例(這將導致崩潰,因為 Qt 將獲得意外的參數類型);您可以改用 self._view,但是,如前所述,這種情況下的整個分離只會使事情變得不必要地復雜;
                • 對引用內部對象的字典鍵使用字符串很少是一個好主意(尤其是在像您這樣使用長描述性字符串時);
                • 從一個模塊中導入多個元素時,通常最好將它們分組而不是使用單行導入:它使代碼更整潔,更易于閱讀和檢查:from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QFileDialog)
                • 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)

                這篇關于PyQt5按鈕未連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                <legend id='L34de'><style id='L34de'><dir id='L34de'><q id='L34de'></q></dir></style></legend>
              1. <i id='L34de'><tr id='L34de'><dt id='L34de'><q id='L34de'><span id='L34de'><b id='L34de'><form id='L34de'><ins id='L34de'></ins><ul id='L34de'></ul><sub id='L34de'></sub></form><legend id='L34de'></legend><bdo id='L34de'><pre id='L34de'><center id='L34de'></center></pre></bdo></b><th id='L34de'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='L34de'><tfoot id='L34de'></tfoot><dl id='L34de'><fieldset id='L34de'></fieldset></dl></div>

                <small id='L34de'></small><noframes id='L34de'>

                    • <tfoot id='L34de'></tfoot>

                          <tbody id='L34de'></tbody>
                          <bdo id='L34de'></bdo><ul id='L34de'></ul>

                        • 主站蜘蛛池模板: 日韩免费av | 91在线观看视频 | 欧美精品1区| 国产精品精品久久久 | 91精品国产乱码久久久久久久久 | 国产精品日韩欧美一区二区三区 | 国产精品一区二区精品 | 中文字幕 在线观看 | 一级做a爰片性色毛片 | 久久99精品国产 | 狠狠操你| 99re视频在线 | 性高湖久久久久久久久aaaaa | 在线成人免费视频 | 欧美一级在线免费观看 | 日韩精品视频在线 | 在线观看免费福利 | 在线播放国产一区二区三区 | 欧美a v在线 | 精品国产亚洲一区二区三区大结局 | 国产免费一区二区三区 | 天堂av影院 | 久久精品免费 | 国产精品成人久久久久 | 欧美日韩成人 | 波多野吉衣久久 | 国产亚洲精品精品国产亚洲综合 | 日韩一区二区三区视频 | 妞干网视频 | 久久小视频 | 精品一区二区三区91 | 欧美综合一区 | 久久电影一区 | 久久不卡 | 一区二区三区免费在线观看 | 欧美在线视频网 | 免费在线观看毛片 | 亚洲精品视频在线播放 | 爱爱免费视频 | 亚洲精品视频在线播放 | 国产黄色网址在线观看 |