Fetch data from qcalendar - python

How to fetch the data from Qcalendar. For example, when is select 21/10/2019, "Monday" will be fetched when I click the "ok" button
The following is my code:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QCalendarWidget
class Ui_Form(QMainWindow):
def __init__(self):
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.label = QtWidgets.QLabel(Form)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton_2 = QtWidgets.QPushButton(Form)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(700, 700)
self.calendarWidget.setGeometry(QtCore.QRect(100, 110, 510, 454))
self.calendarWidget.setMinimumSize(QtCore.QSize(200, 144))
self.calendarWidget.setObjectName("calendarWidget")
self.label.setGeometry(QtCore.QRect(100, 50, 101, 31))
self.label.setObjectName("label")
self.pushButton.setGeometry(QtCore.QRect(460, 600, 76, 37))
self.pushButton.setObjectName("backButton")
self.pushButton_2.setGeometry(QtCore.QRect(560, 600, 76, 37))
self.pushButton_2.setObjectName("okbutton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
selectionMode()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Select date"))
self.label.setText(_translate("Form", "Select date"))
self.pushButton.setText(_translate("Form", "Back"))
self.pushButton_2.setText(_translate("Form", "ok"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())

void QCalendarWidget::clicked(const QDate &date)
This signal is emitted when a mouse button is clicked.
The date the mouse was clicked on is specified by date.
The signal is only emitted when clicked on a valid date, e.g.,
dates are not outside the minimumDate() and maximumDate().
If the selection mode is NoSelection, this signal will not be emitted.
QString QDate::toString(const QString &format) const
Returns the date as a string. The format parameter determines the format of the result string.
https://doc.qt.io/qt-5/qdate.html#toString-2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QCalendarWidget, QWidget
# WARNING! All changes made in this file will be lost!
class Ui_Form(object):
'''
# WARNING! All changes made in this file will be lost !!!!!!
class Ui_Form(QMainWindow):
def __init__(self):
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.label = QtWidgets.QLabel(Form)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton_2 = QtWidgets.QPushButton(Form)
'''
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(700, 700)
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.calendarWidget.setGeometry(QtCore.QRect(100, 110, 510, 454))
self.calendarWidget.setMinimumSize(QtCore.QSize(200, 144))
self.calendarWidget.setObjectName("calendarWidget")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(100, 50, 101, 31))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(460, 600, 76, 37))
self.pushButton.setObjectName("backButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(560, 600, 76, 37))
self.pushButton_2.setObjectName("okbutton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
# ? selectionMode()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Select date"))
self.label.setText(_translate("Form", "Select date"))
self.pushButton.setText(_translate("Form", "Back"))
self.pushButton_2.setText(_translate("Form", "ok"))
class MyWindow(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
self.calendarWidget.setGridVisible(True)
self.calendarWidget.clicked[QtCore.QDate].connect(self.showDate)
self.date = self.calendarWidget.selectedDate()
self.label.setText(self.date.toString("dd-MM-yyyy dddd"))
self.pushButton_2.clicked.connect(self.clickedOk)
def showDate(self, date):
# self.label.setText(date.toString("dd-MM-yyyy dddd")) # ! Try to uncomment !
self.date = date
def clickedOk(self):
self.label.setText(self.date.toString("dd-MM-yyyy dddd"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# Form = QtWidgets.QWidget()
# ui = Ui_Form()
# ui.setupUi(Form)
# Form.show()
w = MyWindow()
w.show()
sys.exit(app.exec_())

Related

How to use class variable from pyqt5 script in another python file

I have a simple pyqt5 script that makes the following UI:
UI
The python file for this code is named 'Test.py' and is as follows:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 400)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(150, 90, 91, 31))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(240, 100, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(250, 150, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.function) #<<<<<<<< MY INPUT
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "Job Number"))
self.pushButton.setText(_translate("Form", "Press it"))
def function(self): #<<<<<<<< MY INPUT
self.text = self.lineEdit.text() #<<<<<<<< MY INPUT
return self.text #<<<<<<<< MY INPUT
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
I simply want to use the 'self.text' value that is generated on a different python file called 'Test1.py'.
I tried tried the following in the 'Test1.py' file:
from Test import Ui_Form
obj = Ui_Form()
obj.function()
But I keep getting the following error:
AttributeError: 'Ui_Form' object has no attribute 'lineEdit'

Change palette of PyQt5

Am attempting to change the GUI palette from dark to light.
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
from PyQt5.QtCore import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Dark"))
self.pushButton_2.setText(_translate("Form", "Light"))
self.pushButton.clicked.connect(self.changeSkinDark)
self.pushButton_2.clicked.connect(self.changeSkinLight)
def changeSkinDark(self):
darkpalette = QPalette()
darkpalette.setColor(QPalette.Window, QColor(41,44,51))
darkpalette.setColor(QPalette.WindowText, Qt.white)
darkpalette.setColor(QPalette.Base, QColor(15,15,15))
darkpalette.setColor(QPalette.AlternateBase, QColor(41,44,51))
darkpalette.setColor(QPalette.ToolTipBase, Qt.white)
darkpalette.setColor(QPalette.ToolTipText, Qt.white)
darkpalette.setColor(QPalette.Text, Qt.white)
darkpalette.setColor(QPalette.Button, QColor(41,44,51))
darkpalette.setColor(QPalette.ButtonText, Qt.white)
darkpalette.setColor(QPalette.BrightText, Qt.red)
darkpalette.setColor(QPalette.Highlight, QColor(100,100,225))
darkpalette.setColor(QPalette.HighlightedText, Qt.black)
app.setPalette(darkpalette)
def changeSkinLight(self):
palette = QGuiApplication.palette()
app.setPalette(palette)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setStyle('Fusion')
Form = QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
This is how PyQt5 says the palette can be set to default (changeSkinLight method).
But when I assign the functions to an action. It just runs the changeDarkSkin and does nothing on changeLightSkin. How can I set the palette to default like it was in QtDesigner?
You have to save the QPalette by default if you want to restore the application palette:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Dark"))
self.pushButton_2.setText(_translate("Form", "Light"))
class Form(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.changeSkinDark)
self.pushButton_2.clicked.connect(self.changeSkinLight)
self.default_palette = QtGui.QGuiApplication.palette()
#QtCore.pyqtSlot()
def changeSkinDark(self):
darkpalette = QtGui.QPalette()
darkpalette.setColor(QtGui.QPalette.Window, QtGui.QColor(41, 44, 51))
darkpalette.setColor(QtGui.QPalette.WindowText, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.Base, QtGui.QColor(15, 15, 15))
darkpalette.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor(41, 44, 51))
darkpalette.setColor(QtGui.QPalette.ToolTipBase, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.ToolTipText, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.Text, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.Button, QtGui.QColor(41, 44, 51))
darkpalette.setColor(QtGui.QPalette.ButtonText, QtCore.Qt.white)
darkpalette.setColor(QtGui.QPalette.BrightText, QtCore.Qt.red)
darkpalette.setColor(QtGui.QPalette.Highlight, QtGui.QColor(100, 100, 225))
darkpalette.setColor(QtGui.QPalette.HighlightedText, QtCore.Qt.black)
QtGui.QGuiApplication.setPalette(darkpalette)
#QtCore.pyqtSlot()
def changeSkinLight(self):
QtGui.QGuiApplication.setPalette(self.default_palette)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Form()
w.show()
sys.exit(app.exec_())
Note:
There seems to be a bug for certain versions of PyQt5 and python so a workaround is setting the palette to the window(thanks #S.Nick):
#QtCore.pyqtSlot()
def changeSkinDark(self):
# ...
self.setPalette(darkpalette)
#QtCore.pyqtSlot()
def changeSkinLight(self):
self.setPalette(self.default_palette)

how to detect a key release in pyqt

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

How to passing value from main to dialog

I'm newbie for using Pyqt5.
I use qtdesigner for building GUI.
I have MainWindow for passing value to dialogwindow
I want to LineEdit( in dialogwindow) show value after user input and click button (in MainWindow)
I try
self.ui = Ui_Dialog(self,data)
but it doesn't work
My code mainpage
MainWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
class Ui_MainWindow(object):
def openDialog(self):
data = self.lineEdit.text()
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog(self,data)
self.ui.setupUi(self.window)
# MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(505, 236)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(120, 40, 91, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(290, 40, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 110, 93, 28))
self.pushButton.clicked.connect(self.openDialog)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 505, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
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", "Passing Value"))
self.pushButton.setText(_translate("MainWindow", "Send"))
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_())
Dialog code.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(577, 253)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(260, 100, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(180, 100, 55, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Value"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
advice me plz
Thank you,
Mint
PyQt recommends not modifying the .py generated by pyuic and Qt Designer but creating another file that uses that class to fill a widget so I recommend regenerating the MainWindow.py and dialog.py files.
Now create a main.py where we inherit the appropriate class by setting a constructor with the requirement:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
from MainWindow import Ui_MainWindow
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, text, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText(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.openDialog)
def openDialog(self):
data = self.lineEdit.text()
w = Dialog(data)
w.exec_()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
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