2022.5.30 GUI

정성우·2022년 5월 30일
0

학습한 내용

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow,QAction,qApp, QFileDialog,QPushButton,QLabel

from PyQt5.QtCore import QCoreApplication

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

    def initUI(self):
        self.setWindowIcon(QIcon("./web.png"))
        # 타이틀바에 나타나는 창의 제목
        self.setWindowTitle("My First Application !!")
        self.move(300,300)
        self.resize(400,200)
        # 푸쉬버튼 만들기
        self.statusBar().showMessage("준비중")
        self.label=QLabel('0000000',self)
        self.move(40,40)
        self.pb=QPushButton('start',self)
        self.pb.clicked.connect(self.count_number)
        self.pb.move(150,40)





        # 메뉴바 만들기
        exitAction=QAction('&Exit',self)
        # 단축기
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip("Exit application")
        exitAction.triggered.connect(qApp.quit)

        menubar=self.menuBar()

        loadfile=QAction('load file ...',self)
        savefile=QAction('save file...',self)
        loadfile.triggered.connect(self.add_open)
        savefile.triggered.connect(self.add_save)


        fileMenu=menubar.addMenu('&File')

        fileMenu.addAction(exitAction)
        fileMenu.addAction(loadfile)
        fileMenu.addAction(savefile)



        self.show()

    def add_open(self):
        FileOpen=QFileDialog.getOpenFileName(self,'openfile','./')

    def add_save(self):
        FileSave=QFileDialog.getSaveFileName(self,'savefile','./')
    
    def count_number(self):
        """
        상태바 테스트 함수
        """
        self.statusBar().showMessage("작업중....")
        """
        PyQt5를 쓰면서 데이터가 계속 업데이트 되지 않을때 반드시 repaint() 부분을 추가 해주세요
        """
        self.statusBar().repaint()
        for i in range(1,100000):
            print(i)
            self.label.setText(str(i))
            self.label.repaint()
        
        self.statusBar().showMessage("준비중")



if __name__=="__main__":
    app=QApplication(sys.argv)
    ex=MyApp()
    sys.exit(app.exec_())

실행결과


학습한 내용

import sys
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QHBoxLayout,QVBoxLayout

"""
구성
창의 가운데 아래에 두 개의 버튼을 배치
두개의 버튼은 창의 크기를 변화시켜도 같은 자리에 위치합니다.
"""


class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        #두개의 버튼 만들기
        okButton=QPushButton('ok')
        cancelButton=QPushButton('Cancel')

        """
        수평 박스를 하나 만들고 두개의 버튼과 양쪽에 빈 공간 추가
        addStretch() 메서드는 신축성 있는 빈공간을 제공
        두 버튼 양쪽의 stretch factor 1로 같기 때문에 이 두 빈공간의 크기는 창의 크기가 변화해도 항상 동일
        """
        hbox=QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        hbox.addStretch(1)

        vbox=QVBoxLayout()
        vbox.addStretch(3)
        vbox.addLayout(hbox)
        vbox.addStretch(1)

        #최종적으로 수직박스를 창의 메인 레이어아웃으로 설정

        self.setLayout(vbox)
        self.setWindowTitle("box layout")
        self.setGeometry(300,300,300,200)



        self.show()

if __name__=='__main__':
    app=QApplication(sys.argv)
    ex=MyApp()
    sys.exit(app.exec_())    


실행결과

from PyQt5.QtWidgets import QWidget, QSlider, QHBoxLayout, QLabel, QApplication
from PyQt5.QtCore import Qt

import sys

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        hbox=QHBoxLayout()

        sld=QSlider(Qt.Vertical,self)
        sld.setFocusPolicy(Qt.NoFocus)

        sld.setRange(0,100)
        sld.setPageStep(5)
        sld.valueChanged.connect(self.changeValue)

        self.label=QLabel("0",self)
        self.label.setStyleSheet(
            "Qlabel { backgroudn: #007AA5; border-radius: 3px;}")
        self.label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
        self.label.setMinimumWidth(80)
        hbox.addStretch()
        hbox.addWidget(sld)
        hbox.addSpacing(15)
        hbox.addWidget(self.label)
        hbox.addStretch(1)

        self.setLayout(hbox)
        self.setGeometry(300,300,350,250)
        self.setWindowTitle("qslider")
        self.show()
    
    def changeValue(self,value):
        self.label.setText(str(value))


        


def main():
    app=QApplication(sys.argv)
    ex=Example()
    sys.exit(app.exec_())


if __name__=="__main__":
    main()

실행결과

학습한 내용 중 어려웠던 점 또는 해결못한 것들
과제로 새로운 함수를 만들어야하는데 slider 값을 어디에 넣어야할지 모르겠다

해결방법 작성
구글링

학습 소감
아직 간단하지만 윈도우창에 뭔가를 넣는게 재미있다.

0개의 댓글