PyQt5 튜토리얼-푸시 버튼
푸시 버튼 위젯 QPushButton 은 PyQt5의 명령 버튼입니다. ‘적용’, ‘취소’및 ‘저장’과 같은 특정 작업을 수행하도록 PC 에 명령하도록 사용자가 클릭합니다.
PyQt5 푸시 버튼-QPushButton
import sys
from PyQt5 import QtWidgets
def basicWindow():
app = QtWidgets.QApplication(sys.argv)
windowExample = QtWidgets.QWidget()
buttonA = QtWidgets.QPushButton(windowExample)
labelA = QtWidgets.QLabel(windowExample)
buttonA.setText("Click!")
labelA.setText("Show Label")
windowExample.setWindowTitle("Push Button Example")
buttonA.move(100, 50)
labelA.move(110, 100)
windowExample.setGeometry(100, 100, 300, 200)
windowExample.show()
sys.exit(app.exec_())
basicWindow()
어디,
buttonA = QtWidgets.QPushButton(windowExample)
buttonA 는 QtWidgets 의 QPushButton 이며 마지막 장에서 소개 된 레이블과 동일하게 windowExample 창에 추가되어야합니다.
buttonA.setText("Click!")
buttonA 의 텍스트를 Click!로 설정합니다.

실제로는 아무것도하지 않을 것입니다.
PyQt5 QLabel 라벨 위젯 세트 스타일
배경색, 글꼴 모음 및 글꼴 크기와 같은 PyQt5 QLabel 위젯의 스타일은 setStyleSheet 메소드로 설정할 수 있습니다. CSS 의 스타일 시트처럼 작동합니다.
buttonA.setStyleSheet(
"background-color: red;font-size:18px;font-family:Times New Roman;"
)
다음 스타일로 buttonA 를 설정합니다.
| 스타일 | 값 |
|---|---|
background-color |
red |
font-size |
18px |
font-family |
Times New Roman |
CSS 와 유사하기 때문에 PyQt5에서 스타일을 설정하는 것이 편리합니다.
import sys
from PyQt5 import QtWidgets
def basicWindow():
app = QtWidgets.QApplication(sys.argv)
windowExample = QtWidgets.QWidget()
buttonA = QtWidgets.QPushButton(windowExample)
labelA = QtWidgets.QLabel(windowExample)
buttonA.setStyleSheet(
"background-color: red;font-size:18px;font-family:Times New Roman;"
)
buttonA.setText("Click!")
labelA.setText("Show Label")
windowExample.setWindowTitle("Push Button Example")
buttonA.move(100, 50)
labelA.move(110, 100)
windowExample.setGeometry(100, 100, 300, 200)
windowExample.show()
sys.exit(app.exec_())
basicWindow()

PyQt5 QLabel 라벨 클릭 이벤트
버튼 클릭 이벤트는 QLabel.clicked.connect(func)메소드를 사용하여 특정 기능에 연결됩니다.
import sys
from PyQt5 import QtWidgets
class Test(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.buttonA = QtWidgets.QPushButton("Click!", self)
self.buttonA.clicked.connect(self.clickCallback)
self.buttonA.move(100, 50)
self.labelA = QtWidgets.QLabel(self)
self.labelA.move(110, 100)
self.setGeometry(100, 100, 300, 200)
def clickCallback(self):
self.labelA.setText("Button is clicked")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
test = Test()
test.show()
sys.exit(app.exec_())
QPushButton``buttonA 를 클릭하면 clickCallback 기능을 트리거하여 레이블 텍스트를 Button is clicked로 설정합니다.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook