Python PYQT5信号和插槽设计-条件序列中的一个信号调用插槽
我有这个问题。我想创建一个按钮(信号),在通过简单的if/else条件后逐渐调用插槽(函数)。我最大的问题是,我必须按两次按钮才能级联到第二个插槽,而在经过第三个插槽时,我必须按三次按钮。如何避免呢? 第一个条件是有一个有效路径,第二个条件是有一个以Python PYQT5信号和插槽设计-条件序列中的一个信号调用插槽,python,pyqt5,Python,Pyqt5,我有这个问题。我想创建一个按钮(信号),在通过简单的if/else条件后逐渐调用插槽(函数)。我最大的问题是,我必须按两次按钮才能级联到第二个插槽,而在经过第三个插槽时,我必须按三次按钮。如何避免呢? 第一个条件是有一个有效路径,第二个条件是有一个以USR02.txt结尾的文件。 有什么想法吗 from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QApplication, QFil
USR02.txt
结尾的文件。
有什么想法吗
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QLabel, QCheckBox, QWidget, QMessageBox
from os.path import expanduser
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(450, 39)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(120, 10, 311, 21))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(20, 10, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.checkfolder)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Click"))
self.lineEdit.setText(_translate("Dialog", "C:\\Users\\"))
def checkfolder(self):
import sys
import os
import glob
import ctypes
didi = self.lineEdit.text()
if os.path.exists(didi):
print(didi)
self.pushButton.clicked.connect(self.checkfilexist)
print("Valid path!")
elif not os.path.exists(didi):
ctypes.windll.user32.MessageBoxW(0, "Enter the existing path!", "ERROR", 1)
return
def checkfilexist(self):
import sys
import os
import glob
import ctypes
didi = self.lineEdit.text()
fufu = didi + '\\' + '*USR02.txt'
if glob.glob(fufu):
print(fufu)
self.pushButton.clicked.connect(self.abRunnormal)
print("File found!")
elif not os.path.isfile(fufu):
ctypes.windll.user32.MessageBoxW(0, "No files found, please try again!", "ERROR", 1)
return
def abRunnormal(self):
import ctypes
ctypes.windll.user32.MessageBoxW(0, "SUCCESS!", "ERROR", 1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys._excepthook = sys.excepthook
sys.exit(app.exec_())
您当前正在将按钮连接到self.check文件夹。发生了什么: 如果单击“按钮”,将执行自检文件夹。如果文件夹存在,请将按钮连接到下一个函数(self.checkfilexists),该函数不会立即调用,但下次单击按钮时会调用。只需连接到checkfolder并调用checkfilexists:
if os.path.exists(didi):
print(didi)
self.checkfilexist()
print("Valid path!")
这里也一样:
if glob.glob(fufu):
print(fufu)
self.abRunnormal()
print("File found!")
为什么要更改按钮的功能而不只是调用下一个功能
self.butdown.clicked.connect(self.checkfilexist)
->self.checkfilexist()
和self.butdown.clicked.connect(self.abRunnormal)
->self.abRunnormal()