<legend id='ewt9e'><style id='ewt9e'><dir id='ewt9e'><q id='ewt9e'></q></dir></style></legend>

      <tfoot id='ewt9e'></tfoot>

      <i id='ewt9e'><tr id='ewt9e'><dt id='ewt9e'><q id='ewt9e'><span id='ewt9e'><b id='ewt9e'><form id='ewt9e'><ins id='ewt9e'></ins><ul id='ewt9e'></ul><sub id='ewt9e'></sub></form><legend id='ewt9e'></legend><bdo id='ewt9e'><pre id='ewt9e'><center id='ewt9e'></center></pre></bdo></b><th id='ewt9e'></th></span></q></dt></tr></i><div id='ewt9e'><tfoot id='ewt9e'></tfoot><dl id='ewt9e'><fieldset id='ewt9e'></fieldset></dl></div>
    1. <small id='ewt9e'></small><noframes id='ewt9e'>

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

      单击菜单标题时连接功能

      时间:2023-08-04
            <bdo id='uF6fq'></bdo><ul id='uF6fq'></ul>

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

              <legend id='uF6fq'><style id='uF6fq'><dir id='uF6fq'><q id='uF6fq'></q></dir></style></legend>
              1. <tfoot id='uF6fq'></tfoot>

                  <tbody id='uF6fq'></tbody>

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

              2. 本文介绍了单击菜单标题时连接功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试查找打开的端口并将它们添加到我的菜单中.现在,我成功地对我的菜单进行了操作(例如,查找端口"),并且只有当它被单击时——它才会连接到我的功能,以获得所有可用端口.不幸的是,这不是我想要的.

                I am trying to find open ports and add them to my menu. Right now, what I succeed having an action to my menu (like, "find ports"), and only if it's clicked - it will connect to my function that gets all free ports. Unfortunately, that's not what I am looking for.

                我想点击菜单title,并在我的菜单中获取所有端口.以下是我的代码:

                I want to click on the menu title, and get all port in my menu. Below is the code I have:

                这是图形用户界面部分:

                This is the GUI part:

                from PyQt5 import QtCore, QtGui, QtWidgets
                
                class Ui_MainWindow(object):
                    def setupUi(self, MainWindow):
                        MainWindow.setObjectName("MainWindow")
                        MainWindow.resize(150, 150)
                        self.centralwidget = QtWidgets.QWidget(MainWindow)
                        self.centralwidget.setObjectName("centralwidget")
                
                        self.portList = QtWidgets.QPushButton(self.centralwidget)
                        self.portList.setGeometry(QtCore.QRect(10, 50, 65, 23))
                        self.portList.setObjectName("portList")
                
                        self.productMenu=QtWidgets.QMenu(self.centralwidget)
                #        self.productMenu.addAction("Find Port") <-------- If I add this, then it works when I click on "Find Port"
                
                        self.portList.setMenu(self.productMenu)
                
                        MainWindow.setCentralWidget(self.centralwidget)
                
                        self.retranslateUi(MainWindow)
                        QtCore.QMetaObject.connectSlotsByName(MainWindow)
                
                
                
                
                
                    def retranslateUi(self, MainWindow):
                        _translate = QtCore.QCoreApplication.translate
                        MainWindow.setWindowTitle(_translate("MainWindow", "GUI"))
                        self.portList.setText(_translate("MainWindow", "Ports"))
                

                这是我运行我的功能的地方:

                And this is where I run my functions:

                from PyQt5 import QtWidgets, QtCore, QtGui
                from test1 import Ui_MainWindow
                import serial.tools.list_ports
                import sys
                
                class ApplicationWindow(QtWidgets.QMainWindow):
                    def __init__(self):
                        super(ApplicationWindow, self).__init__()
                
                        self.ui = Ui_MainWindow()
                        self.ui.setupUi(self)
                
                        self.ui.productMenu.triggered.connect(self.findPort)
                
                        self.ui.portList.clicked.connect(self.findPort)
                        ###I tried both lines above, but it doesn't connect to the function###
                    def findPort(self):
                          comPorts = list(serial.tools.list_ports.comports())
                          print("clicked!")
                           for counter in comPorts:
                               strPort=str(counter)
                               print(strPort)
                               self.ui.productMenu.addAction(strPort)
                
                    def portClick(self,action):
                        print(action.text())
                
                
                if __name__ == "__main__":
                    app = QtWidgets.QApplication(sys.argv)
                    application = ApplicationWindow()
                    application.show()
                    sys.exit(app.exec_())
                

                我怎样才能让 findport 功能通过按菜单标题进行连接,并使用空闲端口立即更新它?

                How can I get the findport function to connect by pressing the title of the menu, and get it updated immediately with the free ports?

                推荐答案

                你必须使用 aboutToShow 信号:

                You have to use the aboutToShow signal:

                self.ui.productMenu.aboutToShow.connect(self.findPort)
                

                这篇关于单击菜单标题时连接功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在树莓派上安装 PyQt5 for Python3.6 下一篇:Pyinstaller 和 PyQt5 macOS Mojave 兼容性问题

                相关文章

                最新文章

                <legend id='V7grB'><style id='V7grB'><dir id='V7grB'><q id='V7grB'></q></dir></style></legend>

                1. <tfoot id='V7grB'></tfoot>

                  1. <small id='V7grB'></small><noframes id='V7grB'>

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