【问题标题】:connect a function when menu title is clicked单击菜单标题时连接功能
【发布时间】:2020-05-24 20:31:31
【问题描述】:

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

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

这是图形用户界面部分:

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

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

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 功能,并使用空闲端口立即更新它?

【问题讨论】:

    标签: python pyqt pyqt5 qpushbutton qmenu


    【解决方案1】:

    你必须使用aboutToShow 信号:

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

    【讨论】:

    • 谢谢!有用!但我现在有一个不同的问题:当我点击它时,它会不断添加端口,即使之前已经添加了端口。有没有办法防止它?比如我点击了一次,出现了COM1端口,我怎么做才能防止COM1多次出现,即使我点击了几次按钮?
    • @Alon123 据了解,您想显示可用端口,然后在添加新端口之前清理 QActions:self.ui.productMenu.clear()for portname in serial.tools.list_ports.comports():self.ui.productMenu.addAction(str(portname))
    猜你喜欢
    • 1970-01-01
    • 2015-11-23
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2012-01-18
    相关资源
    最近更新 更多