how to detect a key release in pyqt - python

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_())

Related

I have two files LOGIN.py and LOGINCLICK.py. How to open LOGIN.py file from LOGINCLICK.py by clicking a button?

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)

How to popup window auomatically?

I want to automatically popup to another window,for test ,i add a buttom in my GUI,but there is nothing happened.What's more ,i do not want my thread killed,because it has happened before.How can I recorrecct my code?
main.py
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import time
from alert import Ui_Dialog
class Ui_Control_system(object):
def setupUi(self, Control_system):
Control_system.setObjectName("Control_system")
Control_system.resize(1004, 722)
Control_system.setFixedSize(1004, 722)
Control_system.setWindowIcon(QIcon('images/icon.png'))
Control_system.setStyleSheet("QMainWindow{border-image:url(images/background.jpg)}")
self.centralwidget = QtWidgets.QWidget(Control_system)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(130, 170, 752, 245))
self.tableWidget.setRowCount(8)
self.tableWidget.setColumnCount(6)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
self.tableWidget.horizontalHeader().setDefaultSectionSize(125)
self.tableWidget.horizontalHeader().setHighlightSections(True)
self.tableWidget.horizontalHeader().setSortIndicatorShown(False)
self.tableWidget.verticalHeader().setVisible(False)
Control_system.setCentralWidget(self.centralwidget)
self.tableWidget.setAutoFillBackground(True)
self.tableWidget.setItem(0,0,QTableWidgetItem("机器编号"))
self.tableWidget.setItem(0,1,QTableWidgetItem("烟雾值"))
self.tableWidget.setItem(0,2,QTableWidgetItem("火焰值"))
self.tableWidget.setItem(0,3, QTableWidgetItem("温度值"))
self.tableWidget.setItem(0,4, QTableWidgetItem("电流值"))
self.tableWidget.setItem(0,5, QTableWidgetItem("电压值"))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
self.tableWidget.item(0,0).setFont(font)
self.tableWidget.item(0,1).setFont(font)
self.tableWidget.item(0,2).setFont(font)
self.tableWidget.item(0,3).setFont(font)
self.tableWidget.item(0,4).setFont(font)
self.tableWidget.item(0,5).setFont(font)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 70, 300, 50))
self.label.setObjectName("label")
self.label.setAutoFillBackground(True)
self.label.setAlignment(Qt.AlignCenter)
self.label.setStyleSheet("border-image:url(images/title.png)")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(560, 480, 121, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(730, 480, 121, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(70, 60, 121, 41))
self.pushButton_3.setObjectName("pushButton_3")
Control_system.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(Control_system)
self.statusbar.setObjectName("statusbar")
Control_system.setStatusBar(self.statusbar)
self.retranslateUi(Control_system)
QtCore.QMetaObject.connectSlotsByName(Control_system)
def retranslateUi(self, Control_system):
_translate = QtCore.QCoreApplication.translate
Control_system.setWindowTitle(_translate("Control_system", "实时监控系统"))
self.pushButton.setText(_translate("Control_system", "开始运行"))
self.pushButton_2.setText(_translate("Control_system", "停止"))
self.pushButton_3.setText(_translate("Control_system", "跳转"))
class Control_system(QtWidgets.QMainWindow, Ui_Control_system):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pushButton_3.clicked.connect(self.jump_to_alert)
#QtCore.pyqtSlot()
def jump_to_alert(self):
self.popup_window_thread = PopupWindow(self)
self.popup_window_thread.start()
class Alert(QtWidgets.QDialog,Ui_Dialog):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.alert=Ui_Dialog()
self.alert.setupUi(self)
def closeEvent(self, event):
reply = QtWidgets.QMessageBox.question(self,
'警告',
"是否要退出警告?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
class PopupWindow(QtCore.QThread):
def __init__(self, parent=None):
super(PopupWindow, self).__init__()
self.index = 0
def run(self):
self.index+=1
while self.index <= 3:
alert = Alert()
alert.show()
alert.exec()
time.sleep(1)
def stop(self):
self.quit()
self.wait()
if __name__ == '__main__':
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
w = Control_system()
w.show()
app.exec()
this is another UI,which will popup to
alert.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(557, 444)
Dialog.setStyleSheet("QMainWindow{border-image:url(images/background.jpg)}")
Dialog.setWindowIcon(QIcon('images/icon.png'))
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(200, 80, 256, 41))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_2 = QtWidgets.QTextBrowser(Dialog)
self.textBrowser_2.setGeometry(QtCore.QRect(200, 170, 256, 41))
self.textBrowser_2.setObjectName("textBrowser_2")
self.textBrowser_3 = QtWidgets.QTextBrowser(Dialog)
self.textBrowser_3.setGeometry(QtCore.QRect(200, 260, 256, 41))
self.textBrowser_3.setObjectName("textBrowser_3")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(60, 80, 111, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(255, 0, 0);")
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(60, 170, 111, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setStyleSheet("color: rgb(255, 0, 0);")
self.label_2.setTextFormat(QtCore.Qt.AutoText)
self.label_2.setObjectName("label_2")
self.alert = QtWidgets.QLabel(Dialog)
self.alert.setGeometry(QtCore.QRect(60, 260, 111, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.alert.setFont(font)
self.alert.setStyleSheet("color: rgb(255, 0, 0);")
self.alert.setTextFormat(QtCore.Qt.AutoText)
self.alert.setObjectName("alert")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "警告"))
self.label.setText(_translate("Dialog", "报警车间"))
self.label_2.setText(_translate("Dialog", "报警机器"))
self.alert.setText(_translate("Dialog", "报警项目"))
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import time
from alert import Ui_Dialog
class Ui_Control_system(object):
def setupUi(self, Control_system):
Control_system.setObjectName("Control_system")
Control_system.resize(1004, 722)
Control_system.setFixedSize(1004, 722)
Control_system.setWindowIcon(QIcon('images/icon.png'))
Control_system.setStyleSheet("QMainWindow{border-image:url(images/background.jpg)}")
self.centralwidget = QtWidgets.QWidget(Control_system)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(130, 170, 752, 245))
self.tableWidget.setRowCount(8)
self.tableWidget.setColumnCount(6)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
self.tableWidget.horizontalHeader().setDefaultSectionSize(125)
self.tableWidget.horizontalHeader().setHighlightSections(True)
self.tableWidget.horizontalHeader().setSortIndicatorShown(False)
self.tableWidget.verticalHeader().setVisible(False)
Control_system.setCentralWidget(self.centralwidget)
self.tableWidget.setAutoFillBackground(True)
self.tableWidget.setItem(0,0,QTableWidgetItem("机器编号"))
self.tableWidget.setItem(0,1,QTableWidgetItem("烟雾值"))
self.tableWidget.setItem(0,2,QTableWidgetItem("火焰值"))
self.tableWidget.setItem(0,3, QTableWidgetItem("温度值"))
self.tableWidget.setItem(0,4, QTableWidgetItem("电流值"))
self.tableWidget.setItem(0,5, QTableWidgetItem("电压值"))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
self.tableWidget.item(0,0).setFont(font)
self.tableWidget.item(0,1).setFont(font)
self.tableWidget.item(0,2).setFont(font)
self.tableWidget.item(0,3).setFont(font)
self.tableWidget.item(0,4).setFont(font)
self.tableWidget.item(0,5).setFont(font)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 70, 300, 50))
self.label.setObjectName("label")
self.label.setAutoFillBackground(True)
self.label.setAlignment(Qt.AlignCenter)
self.label.setStyleSheet("border-image:url(images/title.png)")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(560, 480, 121, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(730, 480, 121, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(70, 60, 121, 41))
self.pushButton_3.setObjectName("pushButton_3")
Control_system.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(Control_system)
self.statusbar.setObjectName("statusbar")
Control_system.setStatusBar(self.statusbar)
self.retranslateUi(Control_system)
QtCore.QMetaObject.connectSlotsByName(Control_system)
def retranslateUi(self, Control_system):
_translate = QtCore.QCoreApplication.translate
Control_system.setWindowTitle(_translate("Control_system", "实时监控系统"))
self.pushButton.setText(_translate("Control_system", "开始运行"))
self.pushButton_2.setText(_translate("Control_system", "停止"))
self.pushButton_3.setText(_translate("Control_system", "跳转"))
class Control_system(QtWidgets.QMainWindow, Ui_Control_system):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pushButton_3.clicked.connect(self.jump_to_alert)
#QtCore.pyqtSlot()
def jump_to_alert(self):
self.popup_window_thread = PopupWindow(self)
self.popup_window_thread.popup.connect(self.goAlert) # +++
self.popup_window_thread.start()
def goAlert(self): # +++
self.alert = Alert()
self.alert.show()
class Alert(QtWidgets.QDialog,Ui_Dialog):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.alert=Ui_Dialog()
self.alert.setupUi(self)
def closeEvent(self, event):
reply = QtWidgets.QMessageBox.question(self,
'警告',
"是否要退出警告?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
class PopupWindow(QtCore.QThread):
popup = pyqtSignal() # +++
def __init__(self, parent=None):
super(PopupWindow, self).__init__()
self.index = 0
def run(self):
# self.index+=1
self.popup.emit() # +++
while self.index <= 3:
# alert = Alert()
# alert.show()
# alert.exec()
self.index += 1 # +++
time.sleep(1)
def stop(self):
self.quit()
self.wait()
if __name__ == '__main__':
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
w = Control_system()
w.show()
app.exec()

PyQt5 focusing on readonly QLineEdit widgets

I have 3 QLineEdit widgets that are readonly and listen to keyevents. Well I want to let the user to choose what QLineEdit he is going to use(because it's going to be a shortcut setting window). It appears when in readonly mode the widget is non-clickable. Is there a way to make it clickable or do the same thing without readonly?
I made the code in Qt Designer.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent1
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent2
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent3
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent1(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
def newOnkeyPressEvent2(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_2.setText(str(keyname))
def newOnkeyPressEvent3(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_3.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "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_())
You need to determine which lineedit is under focus with self.centralwidget.focusWidget(). Here is full working code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
lineEdit = self.centralwidget.focusWidget()
lineEdit.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "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_())

PyQt: How to refresh label on one window with push button on another one?

Hello dear Users of stackoverflow
Introduction:
I am kind of new to Python and want to build a touch GUI for my Raspberry Pi with PyQt5. Therefore, I use the QtDesigner to build up .ui files on Windows 7. After that, the files are translated to .py files using "pyuic5 -x file.ui -o file.py" in the LXTerminal of the Pi.
My GUI:
I need to build up one output window (MainWindow) with a label and a push button, which opens up another window (I chose Dialog) for input. The input window has a spin box to set the value and a horizontal slider for bigger value steps. At the bottom of the window is a push button, which sets the spin box value as global variable and closes the Input window again.
The problem:
I want the push button of the input window that closes this window to also refresh the output label of the MainWindow, so that it shows the new value.
Pictures:
I am not allowed to embed Pictures of my GUI yet, so please see the following links.
MainWindow
InputWindow
InputWindow with Connections between slider and spin box
My Code:
The following code is a simple example and has everything working except the refresh of the Label. Please help me to get this work, even if it might be very simple for advanced and professional developers. I spent days on trying and googling for this and got lots of more complicated things working.
Best wishes,
RaspiManu
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
value = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(890, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 100, 500, 250))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 370, 500, 100))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.OpenInput)
############
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", "Value"))
self.pushButton.setText(_translate("MainWindow", "Go to input window"))
##############################
# Show second window for input
def OpenInput(self, MainWindow):
Dialog.show()
##############################
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(889, 598)
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(210, 170, 471, 141))
font = QtGui.QFont()
font.setPointSize(33)
self.spinBox.setFont(font)
self.spinBox.setAlignment(QtCore.Qt.AlignCenter)
self.spinBox.setObjectName("spinBox")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(209, 360, 471, 61))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(310, 460, 271, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.CloseAndRefresh)
############
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 40, 471, 91))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Back to first Window"))
self.label.setText(_translate("Dialog", "Value"))
#######################################################
# Close second window and refresh label on first window
def CloseAndRefresh(self):
global value
value = self.spinBox.value()
print(value) #checking input
##################################################
# The refresh of the outputting label on the #
# MainWindow should be started at this position. #
##################################################
Dialog.close()
#######################################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Dialog = QtWidgets.QDialog()
dia = Ui_Dialog()
dia.setupUi(Dialog)
sys.exit(app.exec_())
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
value = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(890, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 100, 500, 250))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 370, 500, 100))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.OpenInput)
############
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", "Value"))
self.pushButton.setText(_translate("MainWindow", "Go to input window"))
##############################
# Show second window for input
def OpenInput(self, MainWindow):
Dialog.show()
##############################
# +++
def labelText(self, MainWindow, value):
self.label.setText(str(value))
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(889, 598)
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(210, 170, 471, 141))
font = QtGui.QFont()
font.setPointSize(33)
self.spinBox.setFont(font)
self.spinBox.setAlignment(QtCore.Qt.AlignCenter)
self.spinBox.setObjectName("spinBox")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(209, 360, 471, 61))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(310, 460, 271, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.CloseAndRefresh)
############
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 40, 471, 91))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Back to first Window"))
self.label.setText(_translate("Dialog", "Value"))
#######################################################
# Close second window and refresh label on first window
def CloseAndRefresh(self):
global value
value = self.spinBox.value()
print(value) #checking input
# +++
ui.labelText(MainWindow, value)
##################################################
# The refresh of the outputting label on the #
# MainWindow should be started at this position. #
##################################################
Dialog.close()
#######################################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Dialog = QtWidgets.QDialog()
dia = Ui_Dialog()
dia.setupUi(Dialog)
sys.exit(app.exec_())

How to display username after authentication from database in PYQT5

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.

Categories

Resources