I have 2 class files login-form.py and welcome.py
In login-form.py,authentication of user in performed with sqlite3 database.
Code for login-form.py
from PyQt5 import QtCore, QtGui, QtWidgets
from welcome import Ui_MainWindow
import sqlite3
class Ui_Dialog2(object):
def login_check(self):
uname = self.U_name_text.text()
passw = self.pass_text.text()
connection = sqlite3.connect("login.db")
result = connection.execute("SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?", (uname, passw))
if (len(result.fetchall()) > 0):
print("Login success")
self.welcomewindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.welcomewindow)
Dialog.hide()
self.welcomewindow.show()
else:
print("invalid login")
def setupUi2(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(301, 386)
Dialog.setStyleSheet("QDialog{\n"
"background-color: rgb(167, 210, 255);\n"
"}\n"
"QPushButton{\n"
"background-color: rgb(255, 255, 255);\n"
"border:none;\n"
"}\n"
"QLabel{\n"
"color:rgb(255, 23, 54);\n"
"font-size:20px;\n"
"}")
self.U_name_Lable = QtWidgets.QLabel(Dialog)
self.U_name_Lable.setGeometry(QtCore.QRect(20, 110, 111, 21))
self.U_name_Lable.setObjectName("U_name_Lable")
self.Pass_Lable = QtWidgets.QLabel(Dialog)
self.Pass_Lable.setGeometry(QtCore.QRect(20, 150, 111, 21))
self.Pass_Lable.setObjectName("Pass_Lable")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(60, 30, 201, 31))
font = QtGui.QFont()
font.setPointSize(-1)
self.label.setFont(font)
self.label.setStyleSheet("QLabel#label{\n"
"font-size:30px;\n"
"}")
self.label.setObjectName("label")
self.pass_text = QtWidgets.QLineEdit(Dialog)
self.pass_text.setGeometry(QtCore.QRect(140, 150, 131, 20))
self.pass_text.setObjectName("pass_text")
self.login_button = QtWidgets.QPushButton(Dialog)
self.login_button.setGeometry(QtCore.QRect(140, 190, 61, 23))
self.login_button.setObjectName("login_button")
##############button event################
self.login_button.clicked.connect(self.login_check)
##########################################
self.sighup_button = QtWidgets.QPushButton(Dialog)
self.sighup_button.setGeometry(QtCore.QRect(210, 190, 61, 23))
self.sighup_button.setObjectName("sighup_button")
self.U_name_text = QtWidgets.QLineEdit(Dialog)
self.U_name_text.setGeometry(QtCore.QRect(140, 110, 131, 20))
self.U_name_text.setStyleSheet("")
self.U_name_text.setObjectName("U_name_text")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.U_name_Lable.setText(_translate("Dialog", "USER NAME"))
self.Pass_Lable.setText(_translate("Dialog", "PASSWORD"))
self.label.setText(_translate("Dialog", "LOGIN FORM"))
self.login_button.setText(_translate("Dialog", "Login"))
self.sighup_button.setText(_translate("Dialog", "Sign-up"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog2()
ui.setupUi2(Dialog)
Dialog.show()
sys.exit(app.exec_())
After login i want to display username in QlineEdit in welcome.py file
code for welcome.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(220, 90, 151, 61))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 300, 551, 61))
self.lineEdit = QtWidgets.QLineEdit(MainWindow)
self.lineEdit.setGeometry(QtCore.QRect(130, 220, 113, 20))
self.lineEdit.setObjectName("lineEdit")
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Welcome"))
self.label_2.setText(_translate("MainWindow", "Good Morning "))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I am new to python GUI i dont know how to fetch value from one form to another.
The way you are implementing the solution is not scalable, it will give you many headaches. Qt Designer recommends not to modify the classes or the files that it generates since for example you want to change the color of a window, add some widgets, you will have to rewrite all the logic and that is time and money wasted.
The classes generated by Qt Designer are not widgets, but are classes used to fill a widget.
What you should do is create another file that takes care of the logic and use the classes generated by Qt Designer.
In this case, you must eliminate the changes to the .py files generated using Qt Designer.
Recommendation: use appropriate names for the files, do not use -, since these files can be imported by another file and if you do not comply with the rules of python syntax you will have problems. So change the name of login-form.py to login_form.py.
You must create a new file that will call main.py and in it handle the logic, as it is a QDialog the login will use exec_() instead of show(), and verify that it is called to accept() by the result that returns.
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
from login_form import Ui_Dialog2
from welcome import Ui_MainWindow
class LoginDialog(QtWidgets.QDialog, Ui_Dialog2):
def __init__(self, *args, **kwargs):
QtWidgets.QDialog.__init__(self, *args, **kwargs)
self.setupUi(self)
self.login_button.clicked.connect(self.login_check)
def login_check(self):
uname = self.U_name_text.text()
passw = self.pass_text.text()
connection = sqlite3.connect("login.db")
result = connection.execute("SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?", (uname, passw))
if result.fetchall():
self.accept()
else:
print("invalid login")
class WelcomeWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
login = LoginDialog()
w = WelcomeWindow()
if login.exec_() == QtWidgets.QDialog.Accepted:
username = login.U_name_text.text()
w.lineEdit.setText(username)
w.show()
sys.exit(app.exec_())
The complete example can be found in the following link.
Related
LOGIN.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LOGIN_PAGE(object):
def setupUi(self, LOGIN_PAGE):
LOGIN_PAGE.setObjectName("LOGIN_PAGE")
LOGIN_PAGE.resize(1909, 995)
self.centralwidget = QtWidgets.QWidget(LOGIN_PAGE)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(690, 220, 501, 571))
font = QtGui.QFont()
font.setFamily("Cambria Math")
font.setPointSize(11)
self.frame.setFont(font)
self.frame.setStyleSheet("QFrame{\n""background:rgb(255, 250, 175);\n""border-radius:30px;\n"
"}\n""\n""QLineEdit{\n""border-radius:10px;\n""}\n""\n""QPushButton{\n""background:#03a9f4;\n"
"border-radius:10px;}")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(30, 360, 441, 81))
font = QtGui.QFont()
font.setFamily("Cambria")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(130, 480, 241, 41))
font = QtGui.QFont()
font.setFamily("Gill Sans MT")
font.setPointSize(10)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(30, 260, 441, 51))
font = QtGui.QFont()
font.setFamily("Cambria Math")
font.setPointSize(12)
self.lineEdit.setFont(font)
self.lineEdit.setText("")
self.lineEdit.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(30, 110, 441, 51))
font = QtGui.QFont()
font.setFamily("Cambria Math")
font.setPointSize(12)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(40, 60, 231, 41))
font = QtGui.QFont()
font.setFamily("Cambria Math")
font.setPointSize(11)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(40, 210, 231, 41))
font = QtGui.QFont()
font.setFamily("Cambria Math")
font.setPointSize(11)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
LOGIN_PAGE.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(LOGIN_PAGE)
self.statusbar.setObjectName("statusbar")
LOGIN_PAGE.setStatusBar(self.statusbar)
self.retranslateUi(LOGIN_PAGE)
QtCore.QMetaObject.connectSlotsByName(LOGIN_PAGE)
def retranslateUi(self, LOGIN_PAGE):
_translate = QtCore.QCoreApplication.translate
LOGIN_PAGE.setWindowTitle(_translate("LOGIN_PAGE", "MainWindow"))
self.pushButton.setText(_translate("LOGIN_PAGE", "LOGIN"))
self.pushButton_2.setText(_translate("LOGIN_PAGE", "Forget Password"))
self.lineEdit.setPlaceholderText(_translate("LOGIN_PAGE", "PASSWORD"))
self.lineEdit_2.setPlaceholderText(_translate("LOGIN_PAGE", "USERNAME"))
self.label.setText(_translate("LOGIN_PAGE", "USERNAME :"))
self.label_2.setText(_translate("LOGIN_PAGE", "PASSWORD :"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LOGIN_PAGE = QtWidgets.QMainWindow()
ui = Ui_LOGIN_PAGE()
ui.setupUi(LOGIN_PAGE)
LOGIN_PAGE.show()
sys.exit(app.exec_())
LOGINCLICK.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(505, 256)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(-1, -1, 501, 251))
self.frame.setStyleSheet("QFrame{\n""background:rgb(248, 255, 185);}\n""\n""QPushButton{\n"
"background:#03a9f4;\n""border-radius:10px;}")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.pushButton1 = QtWidgets.QPushButton(self.frame)
self.pushButton1.setGeometry(QtCore.QRect(70, 90, 151, 51))
self.pushButton1.setObjectName("pushButton1")
self.pushButton1_2 = QtWidgets.QPushButton(self.frame)
self.pushButton1_2.setGeometry(QtCore.QRect(280, 90, 151, 51))
self.pushButton1_2.setObjectName("pushButton1_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton1.setText(_translate("MainWindow", "LOGIN"))
self.pushButton1_2.setText(_translate("MainWindow", "REGISTRATION"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
How to switch from LOGINCLICK.py to LOGIN.py by clicking a button?
First of all, and this is more a matter of concept, you're not going to "switch" between those files, but between the instances of the classes that are in those files.
Second, you should create a file that acts as a "main" script, which is the file that you'll actually run from python; this is a general rule for programs that use multiple files as "modules", and is almost mandatory for PyQt when using files generated by pyuic; note that you should also never modify the contents of those files, but import them as python modules (read more about using Designer).
Recreate the files with pyuic, then create a new file that will look like this:
from PyQt5 import QtWidgets
from LOGINCLICK import Ui_MainWindow
from LOGIN import Ui_LOGIN_PAGE
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton1.clicked.connect(self.showLogin)
def showLogin(self):
self.loginWindow = LoginWindow()
self.loginWindow.show()
class LoginWindow(QtWidgets.QMainWindow, Ui_LOGIN_PAGE):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
Now, some suggestions:
avoid fixed geometries in any case, and use layout managers instead (like QGridLayout, QHBoxLayout or QVBoxLayout) so that the contents of the windows will always adjust to the available size, otherwise some of the widgets might become unaccessible if the user resizes the window to a smaller size than you decided (there are also other reasons for this, but this is the most important);
don't use uppercase names for files or variables; read more about naming conventions in the Style Guide for Python Code;
a login window should probably be "modal" (shown over the current windows and avoiding interaction with them until the login window is closed); you can use a QDialog for this: from Designer create a new empty dialog, copy the elements of your current login form and paste them to the new dialog (or select all of them and use ctrl+drag), save and generate the file with pyuic, then change the above code considering the following modifications:
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
#...
def showLogin(self):
self.loginWindow = LoginWindow(self)
self.loginWindow.exec_()
class LoginWindow(QtWidgets.QDialog, Ui_LOGIN_PAGE):
def __init__(self, parent=None):
super().__init__()
self.setupUi(self)
hi i wrote a python program in qt i want to call a function in pushbutton but its not work
my code is:
i write this code in pycharm and run this code but its not work
i tested it in idle , vs , other environment but this is not work. when i use jcopy function to pushbutton event its work and its copy lineEdit text to lineEdit_2 text but when i want to use the jtrans function for translate the text of lineEdit and and copy result to lineEdit2 program is not work. in pycharm and vs , idle dont sent any error but not work,so i write the same consol program that run in terminal that run currectly
from PyQt5 import QtCore, QtGui, QtWidgets
from googletrans import Translator
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(598, 456)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 30, 201, 16))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(150, 220, 131, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 220, 201, 16))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(480, 220, 75, 23))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(30, 60, 551, 131))
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(30, 250, 541, 141))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 598, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.jtrans)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Input Text:"))
self.comboBox.setItemText(0, _translate("MainWindow", "persian"))
self.comboBox.setItemText(1, _translate("MainWindow", "english"))
self.comboBox.setItemText(2, _translate("MainWindow", "arabic"))
self.comboBox.setItemText(3, _translate("MainWindow", "danish"))
self.comboBox.setItemText(4, _translate("MainWindow", "dutch"))
self.label_2.setText(_translate("MainWindow", "output Text:"))
self.pushButton.setText(_translate("MainWindow", "translate"))
# def jcopy(self):
# t1=self.lineEdit.text()
# self.lineEdit_2.setText(t1)
def jtrans(self, translator=None):
srcString = self.lineEdit.text()
srcLang = translator.detect(srcString)
dstLang = self.comboBox.currentText()
translated = translator.translate(srcString, src=srcLang, dest=dstLang)
self.lineEdit_2.setText(translated.text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
jcopy function only is a fuction that i know the pushbutton run the code
the problem is in sippet code:
def jtrans(self, translator=None):
srcString = self.lineEdit.text()
srcLang = translator.detect(srcString)
dstLang = self.comboBox.currentText()
translated = translator.translate(srcString, src=srcLang, dest=dstLang)
self.lineEdit_2.setText(translated.text)
anybody can solve this problem
tnx
You have a trivial error: Why do you set object translator to def jtrans(self, translator=None):? Do you think object translator was magically created? Well, no. If the error message is checked:
Traceback (most recent call last):
File "foo.py", line 74, in jtrans
srcLang = translator.detect(srcString)
AttributeError: 'bool' object has no attribute 'detect'
It is understandable that it is not magically created, but X takes the value of what the default clicked signal sends: a boolean.
Before proposing the PyQt solution, I recommend not modifying the code generated by Qt Designer, so you will have to use pyuic and regenerate the file, I will assume that the generated file is called gui.py.
Keep in mind that the translation process is very time consuming so it could crash the GUI so you will have to run it in another thread. Another problem in your code is that the detect method does not return the language but an object of the Detected class that has the language information.
Considering the above, the solution is:
main.py
import sys
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
from googletrans import Translator
from gui import Ui_MainWindow
class WorkerTranslator(QtCore.QObject):
translateSignal = QtCore.pyqtSignal(str)
def translate(self, text, dst_lang):
threading.Thread(
target=self._translate, args=(text, dst_lang), daemon=True
).start()
def _translate(self, text, dst_lang):
translator = Translator()
src_lang = translator.detect(text).lang
translated = translator.translate(text, src=src_lang, dest=dst_lang)
self.translateSignal.emit(translated.text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.translate)
self.translator = WorkerTranslator()
self.translator.translateSignal.connect(self.lineEdit_2.setText)
#QtCore.pyqtSlot()
def translate(self):
text = self.lineEdit.text()
dst_lang = self.comboBox.currentText()
self.translator.translate(text, dst_lang)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
├── gui.py
└── main.py
I want to create a PyQt5 window (Windows OS) which recognizes a button click with holding CTRL button. I successfully created a handler which recognizes CTRL key press but it couldn't find the pressing and releasing of a button which i need to call and dismiss button click event. I did a lot of search but the resources for PyQt5 seems pretty low. Any help is appreciated :)
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.show()
self.signals()
self.bleahOK=True
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 255)
self.unlockButton = QtWidgets.QPushButton(Dialog)
self.unlockButton.setGeometry(QtCore.QRect(10, 180, 171, 51))
self.unlockButton.setObjectName("unlockButton")
self.lockButton = QtWidgets.QPushButton(Dialog)
self.lockButton.setGeometry(QtCore.QRect(220, 180, 151, 51))
self.lockButton.setObjectName("lockButton")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(30, 30, 331, 71))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(30, 120, 261, 31))
font = QtGui.QFont()
font.setPointSize(18)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.unlockButton.setText(_translate("Dialog", "OK"))
self.lockButton.setText(_translate("Dialog", "Lock"))
self.label.setText(_translate("Dialog", ""))
self.lineEdit.setText(_translate("Dialog", ""))
def signals(self):
self.unlockButton.clicked.connect(self.unlock)
def unlock(self):
if 1: print('ff')
def keyPressEvent(self, event):
if type(event) == QtGui.QKeyEvent: #Unable to find when the key was released
print (event.key())
event.accept()
else:
event.ignore()
def lock(self):
print("Test")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
# ui.signals()
# Dialog.show()
sys.exit(app.exec_())
I recommend you not to modify the code generated by Qt Designer, instead create a class that inherits the appropriate widget and use that class as an interface as I recommend PyQt. Going to the problem, you have to use the keyReleaseEvent method to listen when a key is released:
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 255)
self.unlockButton = QtWidgets.QPushButton(Dialog)
self.unlockButton.setGeometry(QtCore.QRect(10, 180, 171, 51))
self.unlockButton.setObjectName("unlockButton")
self.lockButton = QtWidgets.QPushButton(Dialog)
self.lockButton.setGeometry(QtCore.QRect(220, 180, 151, 51))
self.lockButton.setObjectName("lockButton")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(30, 30, 331, 71))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(30, 120, 261, 31))
font = QtGui.QFont()
font.setPointSize(18)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.unlockButton.setText(_translate("Dialog", "OK"))
self.lockButton.setText(_translate("Dialog", "Lock"))
self.label.setText(_translate("Dialog", ""))
self.lineEdit.setText(_translate("Dialog", ""))
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.is_key_ctrl_pressed = False
self.unlockButton.clicked.connect(self.unlock)
#QtCore.pyqtSlot()
def unlock(self):
if self.is_key_ctrl_pressed:
print("unlock")
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Control:
self.is_key_ctrl_pressed = True
super(Dialog, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
if event.key() == QtCore.Qt.Key_Control:
self.is_key_ctrl_pressed = False
super(Dialog, self).keyReleaseEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())
I used Qt Designer to design a GUI and use the command in PyQt5 to convert the .ui file to .py. From that, I want to output a messagebox when a button is clicked. I have successfully done it using this line of code on the function of the button listener:
QtWidgets.QMessageBox.about(MainWindow, "Result", "Invalid number entered!")
I have searched around the internet but I don't understand why this one don't work.
QtWidgets.QMessageBox.about(self, "Result", "Invalid number entered!")
Does the self their refer to the MainWindow also?
Here is the full source code.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.WindowModal)
#MainWindow.resize(310, 185)
MainWindow.setFixedSize(310, 185)
MainWindow.setStyleSheet("#MainWindow\n"
"{\n"
" background-color:qradialgradient(spread:pad, cx:0.5, cy:0.5,
radius:1.696, fx:0.5, fy:0.505682, stop:0 rgba(0, 85, 255, 255), stop:1
rgba(255, 255, 255, 255))\n"
"}\n"
"\n"
"#firstNo_lineedit, #secondNo_lineedit\n"
"{\n"
" \n"
" background-color: rgb(28, 255, 123);\n"
"}\n"
"\n"
"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.firstNo_lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.firstNo_lineedit.setGeometry(QtCore.QRect(130, 40, 113, 20))
self.firstNo_lineedit.setObjectName("firstNo_lineedit")
self.secondNo_lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.secondNo_lineedit.setGeometry(QtCore.QRect(130, 70, 113, 20))
self.secondNo_lineedit.setObjectName("secondNo_lineedit")
self.calculate_button = QtWidgets.QPushButton(self.centralwidget)
self.calculate_button.setGeometry(QtCore.QRect(170, 100, 75, 23))
self.calculate_button.setObjectName("calculate_button")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 40, 81, 20))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(60, 70, 61, 20))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 310, 21))
self.menubar.setObjectName("menubar")
self.menuAbout = QtWidgets.QMenu(self.menubar)
self.menuAbout.setObjectName("menuAbout")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuAbout.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# ================= Listeners ===================
self.calculate_button.clicked.connect(self.basicArithmetic)
# ============================================
# ================= Events ===================
def basicArithmetic(self):
try:
firstNo=float(self.firstNo_lineedit.text())
secondNo=float(self.secondNo_lineedit.text())
resSum=firstNo+secondNo
resDiff=firstNo-secondNo
resMul=firstNo*secondNo
resDiv=round(firstNo/secondNo,2)
print("Sum: {}".format(resSum))
print("Difference: {}".format(resDiff))
print("Product: {}".format(resMul))
print("Quotient: {}".format(resDiv))
toDisplay="Sum: {}".format(resSum)
toDisplay+="\n"
toDisplay+="Difference: {}".format(resDiff)
toDisplay+="\n"
toDisplay+="Product: {}".format(resMul)
toDisplay+="\n"
toDisplay+="Quotient: {}".format(resDiv)
#QtWidgets.QMessageBox.about(self, "Result", toDisplay)
QtWidgets.QMessageBox.about(MainWindow, "Result", toDisplay)
except ValueError:
#QtWidgets.QMessageBox.about(self, "Result", toDisplay)
QtWidgets.QMessageBox.about(MainWindow, "Result", "Invalid number entered!")
# ============================================
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.calculate_button.setText(_translate("MainWindow", "Calculate"))
self.label.setText(_translate("MainWindow", "First No.:"))
self.label_2.setText(_translate("MainWindow", "Second No.:"))
self.menuAbout.setTitle(_translate("MainWindow", "About"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
self is the first parameter of the methods (the name is by convention, is not mandatory) of a class that refers to the instance of the same.
For another QMessageBox.about() requires as first parameter an object of some kind that inherits from QWidget or also None.
If we review the class generated by Qt Designer, we clearly see that it is not a widget, in fact the only objective of that class is to be an interface to fill in another widget:
MainWindow = QtWidgets.QMainWindow() # create widget
ui = Ui_MainWindow() # create interface
ui.setupUi(MainWindow) # fill widget
Therefore the self, the instance of Ui_MainWindow, you want to pass is not appropriate because it does not refer to the widget, but the MainWindow is a widget, so it works correctly with that change.
Also within the generated file you have the following message:
# WARNING! All changes made in this file will be lost!
That message is because if you want to change the design you will have to overwrite the logic that you wrote, such as your basicArithmetic method, which makes the project untenable.
PyQt I recommend to create another class that uses the class that generates QtDesigner, I assume that the generated file is called design.py
design.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'design.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.WindowModal)
#MainWindow.resize(310, 185)
MainWindow.setFixedSize(310, 185)
MainWindow.setStyleSheet("#MainWindow\n"
"{\n"
" background-color:qradialgradient(spread:pad, cx:0.5, cy:0.5,"
"radius:1.696, fx:0.5, fy:0.505682, stop:0 rgba(0, 85, 255, 255), stop:1"
"rgba(255, 255, 255, 255))\n"
"}\n"
"\n"
"#firstNo_lineedit, #secondNo_lineedit\n"
"{\n"
" \n"
" background-color: rgb(28, 255, 123);\n"
"}\n"
"\n"
"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.firstNo_lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.firstNo_lineedit.setGeometry(QtCore.QRect(130, 40, 113, 20))
self.firstNo_lineedit.setObjectName("firstNo_lineedit")
self.secondNo_lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.secondNo_lineedit.setGeometry(QtCore.QRect(130, 70, 113, 20))
self.secondNo_lineedit.setObjectName("secondNo_lineedit")
self.calculate_button = QtWidgets.QPushButton(self.centralwidget)
self.calculate_button.setGeometry(QtCore.QRect(170, 100, 75, 23))
self.calculate_button.setObjectName("calculate_button")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 40, 81, 20))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(60, 70, 61, 20))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 310, 21))
self.menubar.setObjectName("menubar")
self.menuAbout = QtWidgets.QMenu(self.menubar)
self.menuAbout.setObjectName("menuAbout")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuAbout.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.calculate_button.setText(_translate("MainWindow", "Calculate"))
self.label.setText(_translate("MainWindow", "First No.:"))
self.label_2.setText(_translate("MainWindow", "Second No.:"))
self.menuAbout.setTitle(_translate("MainWindow", "About"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This new class can have the following form:
main.py:
from PyQt5 import QtCore, QtGui, QtWidgets
from design import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
# ================= Listeners ===================
self.calculate_button.clicked.connect(self.basicArithmetic)
# ============================================
# ================= Events ===================
def basicArithmetic(self):
try:
firstNo=float(self.firstNo_lineedit.text())
secondNo=float(self.secondNo_lineedit.text())
resSum=firstNo+secondNo
resDiff=firstNo-secondNo
resMul=firstNo*secondNo
resDiv=round(firstNo/secondNo,2)
print("Sum: {}".format(resSum))
print("Difference: {}".format(resDiff))
print("Product: {}".format(resMul))
print("Quotient: {}".format(resDiv))
toDisplay="Sum: {}".format(resSum)
toDisplay+="\n"
toDisplay+="Difference: {}".format(resDiff)
toDisplay+="\n"
toDisplay+="Product: {}".format(resMul)
toDisplay+="\n"
toDisplay+="Quotient: {}".format(resDiv)
#QtWidgets.QMessageBox.about(self, "Result", toDisplay)
QtWidgets.QMessageBox.about(self, "Result", toDisplay)
except ValueError:
#QtWidgets.QMessageBox.about(self, "Result", toDisplay)
QtWidgets.QMessageBox.about(self, "Result", "Invalid number entered!")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
And that is the reason why, in many cases, self is seen as the first parameter.
You can find more information at the following link:
http://pyqt.sourceforge.net/Docs/PyQt5/designer.html
No self refers to Ui_MainWindowclass where MainWindow reffers to QtWidgets.QMainWindow()
So when you say QtWidgets.QMessageBox.about(self, "Result", toDisplay) it will treat Ui_MainWindow as a parent QWidget which is not correct.
when you call QtWidgets.QMessageBox.about(MainWindow, "Result", toDisplay) this time your code will run fine because MainWindow is a valid QWidget i.e QtWidgets.QMainWindow()
hope this helps.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(9576, 698)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(9)
MainWindow.setFont(font)
MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton.setFont(font)
self.RButton.setObjectName("RButton")
self.verticalLayout.addWidget(self.RButton)
self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton2.setFont(font)
self.RButton2.setObjectName("RButton2")
self.verticalLayout.addWidget(self.RButton2)
self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton3.setFont(font)
self.RButton3.setObjectName("RButton3")
self.verticalLayout.addWidget(self.RButton3)
self.Button = QtWidgets.QPushButton(self.centralwidget)
self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(24)
self.Button.setFont(font)
self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
self.Button.setAutoDefault(False)
self.Button.setObjectName("Button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.RButton.setText(_translate("MainWindow", "버튼1"))
self.RButton2.setText(_translate("MainWindow", "버튼2"))
self.RButton3.setText(_translate("MainWindow", "버튼3"))
self.Button.setText(_translate("MainWindow", "확인"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
when button clicked program is turn off...
I need help
The problem is caused because QMessageBox.about() requires as a first parameter a widget but self, that is Ui_MainWindow, is not a widget. Qt Designer does not generate a widget but creates a class that fills a widget. It is also not recommended to modify the class generated by Qt Designer, it is appropriate to create a class that inherits from the appropriate widget and use the design class
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())