PyQt5

star_is_mine·2022년 12월 10일
0
post-thumbnail

PyQt5

i learned by this vid
https://www.youtube.com/watch?v=MOItX2aKTGc

basic

from PyQt5.QtWidgets import *


def main():
    app = QApplication([])
    window = QWidget()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont


def main():
    app = QApplication([])
    window = QWidget()
    window.setGeometry(100, 100, 200, 300) # window size
    window.setWindowTitle("My Simple GUI")

    label = QLabel(window)
    label.setText("Press The Button Below!")
    label.setFont(QFont("Arial", 16)) # set font
    label.move(50, 100) # move the label

    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

버튼배치하고 기능(함수)연결하기

  • QVBoxLayout
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont


def main():
    app = QApplication([])
    window = QWidget()
    window.setGeometry(100, 100, 200, 300) # window size
    window.setWindowTitle("My Simple GUI")

    layout = QVBoxLayout(window) # vertical stack layout # 😍😘🥰
    
    lable = QLabel("Press The Button Below!") # element 1
    button = QPushButton("Press Me!") # element 2

    layout.addWidget(lable)
    layout.addWidget(button)

    window.setLayout(layout)

    window.show()
    app.exec_()

if __name__ == '__main__':
    main()
  • 위 button = QPushButton("Press Me!") 버튼에 함수추가하기. (코드 아래 참조)
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont


def main():
    app = QApplication([])
    window = QWidget()
    window.setGeometry(100, 100, 200, 300) # window size
    window.setWindowTitle("My Simple GUI")

    layout = QVBoxLayout(window) # vertical stack layout
    
    lable = QLabel("Press The  Button Below!") # element 1
    button = QPushButton("Press Me!") # element 2

    button.clicked.connect(on_clicked) # 😎😍😘🥰 함수 연결

    layout.addWidget(lable)
    layout.addWidget(button)

    window.setLayout(layout)

    window.show()
    app.exec_()

# 😎😍😘🥰 함수 정의
def on_clicked():
    print("Hello World")

if __name__ == '__main__':
    main()

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont


def main():
    app = QApplication([])
    window = QWidget()
    window.setGeometry(100, 100, 200, 300) # window size
    window.setWindowTitle("My Simple GUI")

    layout = QVBoxLayout(window) # vertical stack layout
    
    lable = QLabel("Press The  Button Below!") # element 1
    button = QPushButton("Press Me!") # element 2
    message_button = QPushButton("message_button") # 😁😂🤣 message 버튼 추가

    button.clicked.connect(on_clicked)
    message_button.clicked.connect(qt_message) # message 버튼에 기능(함수)연결

    layout.addWidget(lable)
    layout.addWidget(button)
    layout.addWidget(message_button) # message 버튼 화면에 배치 

    window.setLayout(layout)

    window.show()
    app.exec_()

def on_clicked():
    print("Hello World")

def qt_message(): # 😀😁😂 message 함수 정의
    message = QMessageBox()
    message.setText("Hello World")
    message.exec_()

if __name__ == '__main__':
    main()
  • 입력창을 통해서 함수에 매개변수 전달하기
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont


def main():
    app = QApplication([])
    window = QWidget()
    window.setGeometry(100, 100, 200, 300) # window size
    window.setWindowTitle("My Simple GUI")

    layout = QVBoxLayout(window) # vertical stack layout
    
    lable = QLabel("Press The  Button Below!") # element 1
    textbox = QTextEdit() # 😀😁😂
    message_button = QPushButton("message_button") # element 3
    message_button.clicked.connect(lambda : qt_message(textbox.toPlainText())) # 😀😁😂

    layout.addWidget(lable)
    layout.addWidget(textbox)
    layout.addWidget(button)
    layout.addWidget(message_button)

    window.setLayout(layout)

    window.show()
    app.exec_()

def qt_message(msg):
    message = QMessageBox()
    message.setText(msg)
    message.exec_()

if __name__ == '__main__':
    main()

마무리

이상 PyQt5 창을 생성하고, 함수를 만들고 버튼에 함수를 연결하며, 입력창을 이용해 함수에 매개변수를 전달하는 등의 기능을 모두 구현해 보았다. 이제 앞으로는 좀 더 다양한 프로그램을 시도해보면 실력이 향상될 것이다.

profile
i have a dream and I will make my dreams come true.

0개의 댓글