Python 3.x GUI中不同方法的输入
我想创建一个用于数据细化的GUI。它需要文件所在的目录路径和用于详细说明这些文件的附加参数 我想设置一个两级控件,以便用户在没有选择目录时得到通知。如果提供了非数字参数,则必须发出错误消息: 代码如下:Python 3.x GUI中不同方法的输入,python-3.x,pyqt5,Python 3.x,Pyqt5,我想创建一个用于数据细化的GUI。它需要文件所在的目录路径和用于详细说明这些文件的附加参数 我想设置一个两级控件,以便用户在没有选择目录时得到通知。如果提供了非数字参数,则必须发出错误消息: 代码如下: class Window(QtWidgets.QMainWindow, gui.Ui_myGUI): def __init__(self, parent = None): super(Window, self).__init__(parent) self.
class Window(QtWidgets.QMainWindow, gui.Ui_myGUI):
def __init__(self, parent = None):
super(Window, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.GetDirectory)
self.pushButton_2.clicked.connect(self.ValidateInput)
self.show()
def GetDirectory(self):
self.response = QFileDialog.getExistingDirectory(self, caption = 'Select a folder')
os.chdir(self.response)
def ValidateInput(self):
if hasattr(Window, "response"):
exp = str(self.lineEdit.text())
if exp.isnumeric():
self.Process()
else:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Warning")
msg.setText('Numeric data required')
msg.exec_()
else:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Warning")
msg.setText('You need to select a folder')
msg.exec_()
过程是实际数据细化的方法
如果没有选择文件夹,这可以正常工作,但如果在第一条错误消息出现后选择了目录路径,则无法正常工作
事实上,在这种情况下,即使我得到了正确的文件夹,我仍然会得到第一个消息框,告诉我需要选择一个文件夹
我想一个简单的解决方案是为获取目录路径和验证输入数据(并最终开始细化)定义一种方法,但我宁愿将它们分开并链接到两个不同的按钮。问题是窗口没有“响应”属性,但窗口类的实例具有该属性。窗口没有动态属性。因此,在您的情况下,您应该改为:
if hasattr(self, "response"):
即使在这些情况下,我也喜欢使用默认值,例如None,然后使用该值来确定是否已选择目录,例如:
class Window(QtWidgets.QMainWindow, gui.Ui_myGUI):
def __init__(self, parent = None):
super(Window, self).__init__(parent)
self.directory = None
self.setupUi(self)
self.pushButton.clicked.connect(self.get_directory)
self.pushButton_2.clicked.connect(self.validate_input)
self.show()
def get_directory(self):
directory = QFileDialog.getExistingDirectory(self, caption = 'Select a folder')
if directory:
self.directory = directory
os.chdir(self.directory)
def validate_input(self):
message = ""
if self.directory is not None:
exp = self.lineEdit.text()
if exp.isnumeric():
self.Process()
else:
message = "Numeric data required"
else:
message = "You need to select a folder"
if message:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Warning")
msg.setText(message)
msg.exec_()
对不起,我忘了感谢你的帮助!