【发布时间】:2023-01-24 21:23:18
【问题描述】:
如果单击提交按钮时我的 lineEdits 为空,我想让它们变成红色。添加一些东西后,我想让我的 lineEdit 变回白色。
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
import sys, os
from PyQt5.uic import loadUi
from BlurWindow.blurWindow import blur
def input_validate(self,lineedit):
if self.lineedit.text() == '':
self.lineedit.setStyleSheet('''
QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,0,0,0.6); color:white;
}
QLineEdit:focus {
background-color:rgba(255,255,255,0.5);
} ''')
return False
else:
self.lineedit.setStyleSheet('''
QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,255,255,0.6); color:white;
}
QLineEdit:focus {
background-color:rgba(255,255,255,0.5);
} ''')
return True
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi(r'D:\Workspace\Qt Designer\blur bg\blurtest.ui',self)
self.setAttribute(Qt.WA_TranslucentBackground)
blur(self.winId())
self.setStyleSheet("background-color: rgba(0, 0, 0, 0)")
self.browse1.clicked.connect(self.browsefile1)
self.browse2.clicked.connect(self.browsefile2)
self.submit.clicked.connect(self.submit1)
self.setAttribute(Qt.WA_TranslucentBackground)
blur(self.winId())
self.setStyleSheet("background-color: rgba(0, 0, 0, 0)")
def browsefile1(self):
fname=QFileDialog.getOpenFileName(self,'Open File',os.getcwd())
self.lineEdit1.setText(fname[0])
def browsefile2(self):
fname=QFileDialog.getOpenFileName(self,'Open File',os.getcwd())
self.lineEdit2.setText(fname[0])
def submit1(self):
if not input_validate(self.lineEdit1) or not input_validate(self.lineEdit2):
return
print("successs")
app=QApplication(sys.argv)
mw=MainWindow()
mw.show()
sys.exit(app.exec_())
首先,我尝试将此输入验证分离为一个类:
class input_validate(QMainWindow):
def __init__(self):
super().__init__()
if self.lineEdit.text() == '':
self.lineEdit.setStyleSheet('''
QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,0,0,0.6); color:white;
}
QLineEdit:focus {
background-color:rgba(255,255,255,0.5);
} ''')
return False
else:
self.lineEdit.setStyleSheet('''
QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,255,255,0.6); color:white;
}
QLineEdit:focus {
background-color:rgba(255,255,255,0.5);
} ''')
return True
我得到的错误是
如果不是 input_validate(self.lineEdit1) 或者不是 input_validate(self.lineEdit2):
类型错误:输入验证。在里面() 采用 1 个位置参数,但给出了 2 个
所以我试着把它放在 MainWindow 类本身但是这次它说 如果不是 input_validate(self.lineEdit1) 或者不是 input_validate(self.lineEdit2): NameError:未定义名称“input_validate”
所以我把它放在 MainWindow 类之外,使其成为全局的,第一个代码块就是我所拥有的。
现在我的错误是 如果不是 input_validate(self.lineEdit1) 或者不是 input_validate(self.lineEdit2): 类型错误:input_validate() 缺少 1 个必需的位置参数:'lineedit'
我不知道我现在该如何解决这个问题。很长的路是分别用 linedit1 和 lineedit2 定义 input_validate 但我想弄清楚如何重用该代码。
另外我想知道继承 QMainWindow 而不是 QWidget 是否可以。现在这应该是一个窗口,因为它是第一个出现的窗口,所以我选择它作为主窗口。最多它会有一个成功的弹出窗口或单击提交时的错误弹出消息(我想我必须使用 QMessageBox 来实现)
【问题讨论】:
-
你的
input_validate是一个班级。在if条件下使用它完全没有意义,尤其是因为该类没有这样的属性。使用标准函数,并将小部件作为参数传递。另外,请花点时间研究更多关于类和实例的知识,因为很明显你并不真正理解你在那里做什么,并且随机尝试这样的事情并不是很有效。 -
再次嗨@musicamante 是的,我认为这不是在课堂上使用它的方法,所以我将它作为一个函数移到外面但现在收到错误消息说没有参数传递给该函数所以我很困惑现在该做什么
标签: python user-interface pyqt5