편집 기록

편집 기록
  • 프로필 정영훈님의 편집
    날짜2020.01.06

    PyQt5 화면 간 date 전송 혹은 공유가 가능한지 궁금합니다.


    현재 테스팅 용으로 PyQt5를 사용하고 있습니다. 로그인 하고 로그인 정보 (ID)를 다음 화면에서 (여기 코드상에는 Plant 클래스 화면)도 사용하고자 하는데 로그인 정보를 다음 QDialog에서 사용 할 수 있는 방법이 궁금합니다. (말 그대로 사용자가 입력한 id값을 다음 실행 화면에서도 쓰고 싶은 것..)

    최대한 짧게 편집 한 코드 입니다.

    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) # 여기까지는 id가 들어온다.
    
            if self.id  == None:
                self.id = "없음"
            elif self.password == None:
                self.password = "없음"
    
            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', "계정이 없거나 잘못 입력하셨습니다. \n\n"
                                               "다시 시도해 주세요 ")
            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):
            # 2개의 타이머를 구분하기 위한 객체
            sender = self.sender();
            current_time = datetime.datetime.now()
    
            self.lineEdit.setText(str(current_time.strftime("%Y/%m/%d, %H:%M:%S"))) # 밀리초 까지 나타나지 않게 strftime으로 형태 고정.
    
        def pushButtonClicked(self):
            pass
    
    class Login(QWidget): # 이 부분이 처음 화면의 Sign In 실제 버튼 위치하는 곳.
        def __init__(self):
            super().__init__()
            self.setupUI()
    
        def setupUI(self):
    
            self.pushButton = QPushButton("Sign In")
            self.pushButton.clicked.connect(self.pushButtonClicked)
            self.label = QLabel("로그인이 필요합니다")
    
            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() # 상태 바
            self.statusBar().showMessage("현재 접속 중")
    
            login = Login() # login 클래스 불러옴.
            self.setCentralWidget(login)
    
            self.show()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Main()
        window.show()
        app.exec_()
    
    
  • 프로필 반시님의 편집
    날짜2020.01.06

    PyQt5 화면 간 date 전송 혹은 공유가 가능한지 궁금합니다.


    현재 테스팅 용으로 PyQt5를 사용하고 있습니다. 로그인 하고 로그인 정보 (ID)를 다음 화면에서 (여기 코드상에는 Plant 클래스 화면)도 사용하고자 하는데 로그인 정보를 다음 QDialog에서 사용 할 수 있는 방법이 궁금합니다. (말 그대로 사용자가 입력한 id값을 다음 실행 화면에서도 쓰고 싶은 것..)

    최대한 짧게 편집 한 코드 입니다.

    ```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) # 여기까지는 id가 들어온다.
    
        if self.id  == None:
            self.id = "없음"
        elif self.password == None:
            self.password = "없음"
    
        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', "계정이 없거나 잘못 입력하셨습니다. \n\n"
                                           "다시 시도해 주세요 ")
        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):
        # 2개의 타이머를 구분하기 위한 객체
        sender = self.sender();
        current_time = datetime.datetime.now()
    
        self.lineEdit.setText(str(current_time.strftime("%Y/%m/%d, %H:%M:%S"))) # 밀리초 까지 나타나지 않게 strftime으로 형태 고정.
    
    def pushButtonClicked(self):
        pass
    

    class Login(QWidget): # 이 부분이 처음 화면의 Sign In 실제 버튼 위치하는 곳. def init(self): super().init() self.setupUI()

    def setupUI(self):
    
        self.pushButton = QPushButton("Sign In")
        self.pushButton.clicked.connect(self.pushButtonClicked)
        self.label = QLabel("로그인이 필요합니다")
    
        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() # 상태 바
        self.statusBar().showMessage("현재 접속 중")
    
        login = Login() # login 클래스 불러옴.
        self.setCentralWidget(login)
    
        self.show()
    

    if name == "main": app = QApplication(sys.argv) window = Main() window.show() app.exec_()