I want to make my lineEdits turn red if they are empty when I click submit button. After adding something back I want to make my lineEdit turn back to white.
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_())
First I tried seperating this input_validate as a class :
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
The error I got was
if not input_validate(self.lineEdit1) or not input_validate(self.lineEdit2):
TypeError: input_validate.init() takes 1 positional argument but 2 were given
So then I tried putting it inside the MainWindow class itself but this time it says if not input_validate(self.lineEdit1) or not input_validate(self.lineEdit2): NameError: name 'input_validate' is not defined
So I put it outside the MainWindow class to make it global and the first code block is what I have.
Now my error is if not input_validate(self.lineEdit1) or not input_validate(self.lineEdit2): TypeError: input_validate() missing 1 required positional argument: 'lineedit'
I don't know how can I solve this now. The long way would be to define the input_validate with linedit1 and lineedit2 separately but I want to figure out how can I reuse that code.
Also I wanted to know if it is ok to inherit QMainWindow instead of QWidget. This is supposed to be a one window thing for now with this as the first window to appear, so I selected it as a main window. Max it will have is a success popup or error popup message on clicking submit (which I guess I have to implement using QMessageBox)
input_validate
is a class. Using it in anif
condition makes absolutely no sense, and especially since that class has no such attribute. Use a standard function, and pass the widget as argument. Also, please take your time to study more about classes and instances, as it seems clear that you don't really understand what you're doing there, and randomly trying things like this isn't very effective.self
in python.