I wonder if it is possible to send or share dates between PyQt5 screens.

Asked 1 years ago, Updated 1 years ago, 76 views

I am currently using PyQt5 for testing. I want to log in and use the login information (ID) on the next screen (Plant class screen on the code here) However, I would like to know how to use the login information in the following QDialog. (literally, I want to write the ID value entered by the user on the next execution screen.))

This code was edited as short as possible.

from PyQt5.QtCore import *
from PyQt5.QtCore import QTimer, QTime
import sys, pickle,  pymysql, datetime, time
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import pandas_datareader.data as web
import pandas as pd
from pandas import Series, DataFrame
from time import strftime

class LogInDialog(QDialog):
    def __init__(self):
        super().__init__()
        self.setupUI()

    def setupUI(self):
        self.id = None
        self.password = None

        self.setGeometry(150, 200, 450, 400)
        self.setWindowTitle("Sign In")

        label1 = QLabel("ID: ")
        label2 = QLabel("Password: ")

        self.lineEdit1 = QLineEdit()
        self.lineEdit2 = QLineEdit()
        self.lineEdit2.setEchoMode(QLineEdit.Password)
        self.pushButton1= QPushButton("Sign In")
        self.pushButton1.clicked.connect(self.pushButtonClicked)

        layout = QGridLayout()
        layout.addWidget(label1, 0, 0)
        layout.addWidget(self.lineEdit1, 0, 1)
        layout.addWidget(label2, 1, 0)
        layout.addWidget(self.lineEdit2, 1, 1)
        layout.addWidget(self.pushButton1, 1, 2)

        self.setLayout(layout)

    def pushButtonClicked(self):
        self.id = self.lineEdit1.text()
        self.password = self.lineEdit2.text()
        print(self.id) # Up to here the id comes in.

        if self.id  == None:
            self.id = "None"
        elif self.password == None:
            self.password = "None"

        conn =pymysql.connect(host='localhost', port=3306, user='root', password='cute1004!', db='student', charset='utf8')
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM user where id='" + self.id + "' and password = '" + self.password + "'")
        values = cursor.fetchall()

        if values is () :
             QMessageBox.warning(self, 'Error', "Account missing or incorrectly entered. \n\n"
                                           "Please try again ")
        else :
            self.close()
            Plantview = Plant()
            Plantview.exec_()

            print(self.id)

        self.close()
class Plant(QDialog): 
    def __init__(self):
        super().__init__()
        self.setupUIi()

    def setupUIi(self):

        self.setGeometry(150, 200, 450, 400)
        self.setWindowTitle("Plant overview")

        self.pushButton = QPushButton("Plant 1")
        self.pushButton.setStyleSheet('color:blue; background:yellow') 
        self.pushButton.clicked.connect(self.pushButtonClicked)


        self.label1 = QLabel('ID')
        self.lineEdit1 = QLineEdit('aidi')
        self.lineEdit1.setReadOnly(True)

        self.label2 = QLabel('DATE')
        date = QDate.currentDate()
        date = date.toString()
        self.lineEdit2 = QLineEdit(date)
        self.lineEdit2.setReadOnly(True)

        self.lineEdit = QLineEdit(self)
        self.lineEdit.setReadOnly(True)

        self.timer_one = QTimer(self)
        self.timer_one.start(1000)
        self.timer_one.timeout.connect(self.timeout_run)

        layoutingb1 = QHBoxLayout()
        layoutingb1.addStretch(4)
        layoutingb1.addWidget(self.label1)
        layoutingb1.addWidget(self.lineEdit1)

        layoutingb2 = QHBoxLayout()
        layoutingb2.addStretch(4)
        layoutingb2.addWidget(self.label2)
        layoutingb2.addWidget(self.lineEdit)

        inlayout = QVBoxLayout()
        inlayout.addLayout(layoutingb1)
        inlayout.addLayout(layoutingb2)
        inlayout.addStretch(4)

        gb_1 = QGroupBox(self)
        gb_1.setTitle('current value')
        gb_1.setGeometry(220,0,220,80)
        gb_1.setLayout(inlayout)

        layout = QVBoxLayout()
        layout.addWidget(self.pushButton)
        layout.addStretch(1)

        layout4 = QHBoxLayout()
        layout4.addStretch(1)
        layout4.addLayout(layout)
        layout4.addStretch(1)

        btnlayout = QVBoxLayout()
        btnlayout.addLayout(layout4)

        boxlayout = QHBoxLayout()
        boxlayout.addStretch(4)
        boxlayout.addWidget(gb_1)

        layout3 = QVBoxLayout()
        layout3.addLayout(boxlayout)
        layout3.addStretch(1)
        layout3.addLayout(btnlayout)
        layout3.addStretch(2)

        self.setLayout(layout3)

        self.show()

    @pyqtSlot()
    def timeout_run(self):
        # Object to distinguish between two timers
        sender = self.sender();
        current_time = datetime.datetime.now()

        self.lineEdit.setText (str(current_time.strftime("%Y/%m/%d, %H:%M:%S"))) # Fixed shape with strftime so it does not appear until milliseconds.

    def pushButtonClicked(self):
        pass
class Login (QWidget): # This is where the Sign In actual button on the first screen is located.
    def __init__(self):
        super().__init__()
        self.setupUI()

    def setupUI(self):

        self.pushButton = QPushButton("Sign In")
        self.pushButton.clicked.connect(self.pushButtonClicked)
        self.label = QLabel ("Login Required")

        layout = QVBoxLayout() 
        layout.addStretch(6)
        layout.addWidget(self.pushButton)
        layout.addStretch(1)
        layout.addWidget(self.label)

        self.setLayout(layout)

        self.show()

    def pushButtonClicked(self): 
        dlg = LogInDialog() 
        dlg.exec_() 
        id = dlg.id
        password = dlg.password
        self.label.setText("id: %s password: %s" % (id, password))

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.setGeometry(150, 200, 450, 400)
        self.setWindowTitle("MAIN")

        self.statusBar() # statusbar
        self.statusBar().showMessage ("Currently Connected")

        login = Login() # Recall login class.
        self.setCentralWidget(login)

        self.show()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Main()
    window.show()
    app.exec_()

pyqt5 qdialog

2022-09-22 19:30

1 Answers

Please revise it as below.

if values is () :
    QMessageBox.warning(self, 'Error', "Account missing or incorrectly entered. \n\n"
                                           "Please try again ")
    else :
        self.close()
        Plantview = Plant(self.id) #Put id into constructor
        Plantview.exec_()

class Plant(QDialog):
    def__init__(self, id): # Modify constructor
        super().__init__()
        self.id = id #id Value Instance Processing Variables
        self.setupUIi()

...
...
...
    self.label1 = QLabel('ID')
    self.lineEdit1 = QLineEdit (self.id) #idAdmission


2022-09-22 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.