我有这个问题。我想创建一个按钮(信号)的按钮,该按钮在通过简单的if / else条件后逐渐调用插槽(函数)。我最大的问题是,我必须按下按钮两次才能级联到第二个插槽,而按下按钮则要三次。如何避免呢?
第一个条件是具有有效路径,第二个条件是具有以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_())
答案 0 :(得分:1)
您当前正在将按钮连接到self.check文件夹。怎么了: 如果单击“ pushButton”,则执行self.checkfolder。如果该文件夹存在,则将按钮连接到下一个函数(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!")