美文网首页
PYQT5 pushbutton多次执行clicked事件

PYQT5 pushbutton多次执行clicked事件

作者: kyrin28 | 来源:发表于2021-05-21 11:23 被阅读0次

问题就是按UI上的按钮会导致按钮对应的槽函数执行两边的情况

大概有问题的代码:

class MainCode(QMainWindow,Excel_Data_Select_UI.Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        Excel_Data_Select_UI.Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.selectButton.clicked.connect(self.openfile)
        self.saveButton.clicked.connect(self.saveconf)
        self.runButton.clicked.connect(self.runprograme)

    def openfile(self):
        openfile_name = QFileDialog.getOpenFileName(self,'选择文件','./','Excel file(*.xlsx;*.xls;);;csv file(*.csv)')
        openfile_name = openfile_name[0].replace('/','\\')
        #openpath_name = QFileDialog.getExistingDirectory(self,"请选择要更改的EXCEL文件所在的文件夹","D:/")
        #openpath_name = openpath_name.replace('/','\\')
        print(openfile_name)
        #用\替换/
        self.EXCEL_lineEdit.setText(openfile_name)

查询了网上的解决办法,大概有下面两种

方法一:

办法是给对应函数前加上装饰器 @pyqtSlot()

记得导入from PyQt5.QtCore import pyqtSlot

方法二:

是看有文章说是因为满足了以on_开头,以“_”+行为(如_clicked)结束,就会触发QT的自动连接机制,不需要再connect,不过我这也没有这样命名。如果有这样命名的导致槽函数执行了两次,改名字就是了

相关文章

网友评论

      本文标题:PYQT5 pushbutton多次执行clicked事件

      本文链接:https://www.haomeiwen.com/subject/thhwjltx.html