Reputation: 151
I want to show popup info/warning/etc.. message to user, when user is submitting blank or incorrect info as Username and Password in PyQT5-GUI app.
I confused and i don't know where is the root cause of problem.
Windows 10 - 64 bit ___ Python 3.6 and 3.7 - 64 bit ____ PyQT5 - 5.11.3
class Ui_MainWindow(object):
def __init__(self):
<skipped>
self.btn_signin.clicked.connect(self.check_userPass) #### User/Pass checking.
<skipped>
def check_userPass(self): # an internal method/function from `Ui_MainWindow`.
username = self.txt_user.text() #a QlineEdit
password = self.txt_pass.text() #a QlineEdit
if len(username) < 2 and len(password) < 2:
QtWidgets.QMessageBox.question(self, "Checkk", "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Cancel)
###error for above line:
##Traceback (most recent call last):
## File ".\demo_codes.py", line 215, in check_userPass
## QtWidgets.QMessageBox.question(self, 'List manipulation', "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
##TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'
QtWidgets.QMessageBox.question("Checkk", "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Cancel)
###error for above line:
##Traceback (most recent call last):
## File ".\demo_codes.py", line 215, in check_userPass
## QtWidgets.QMessageBox.question('List manipulation', "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
##TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'str'
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
#ui.setupUi(MainWindow) # i renamed `setupUi` to __init__.
MainWindow.show()
sys.exit(app.exec_())
Upvotes: 0
Views: 4165
Reputation: 13681
Try it:
from PyQt5 import QtWidgets, QtCore, QtGui
class Ui_MainWindow(object):
# def __init__(self):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 350)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.txt_user = QtWidgets.QLineEdit(self.centralwidget)
self.txt_user.setGeometry(QtCore.QRect(320, 130, 113, 20))
self.txt_pass = QtWidgets.QLineEdit(self.centralwidget)
self.txt_pass.setGeometry(QtCore.QRect(320, 170, 113, 20))
self.btn_signin = QtWidgets.QPushButton("btn_signin\n`Fusion` style", self.centralwidget)
self.btn_signin.setGeometry(QtCore.QRect(320, 200, 113, 50))
MainWindow.setCentralWidget(self.centralwidget)
# <skipped>
self.btn_signin.clicked.connect(self.check_userPass)
# <skipped>
def check_userPass(self):
username = self.txt_user.text() #a QlineEdit
password = self.txt_pass.text() #a QlineEdit
if len(username) < 2 and len(password) < 2:
"""
# QtWidgets.QMessageBox.question(
QtWidgets.QMessageBox.information(
MainWindow, # - self , + MainWindow !!!
"Checkk",
"Helllooo `Ali reza`",
# QtWidgets.QMessageBox.Ok,
# QtWidgets.QMessageBox.Cancel
)
"""
# +++
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Information)
msg.setText("لطفا نام کاربری و رمزعبور خود را با دقت وارد نمایید")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")
msg.setStandardButtons(QtWidgets.QMessageBox.Ok ) #| QtWidgets.QMessageBox.Cancel)
msg.buttonClicked.connect(self.msgbtn)
retval = msg.exec_() # <<<---- !!!
print("value of pressed message box button:", retval)
# +++
def msgbtn(self, i):
print( "Button pressed is:",i.text() )
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion') # <<<--- convert app to FUSION style in win10
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Upvotes: 2