55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
import sys
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLineEdit, QHBoxLayout
|
|
|
|
|
|
class MyApp(QWidget):
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.initUI()
|
|
|
|
def initUI(self):
|
|
self.edit = QLineEdit("", self)
|
|
btnOK = QPushButton("OK", self)
|
|
btnOK.clicked.connect(self.on_click_btn_OK)
|
|
layout1 = QHBoxLayout()
|
|
layout1.addWidget(self.edit)
|
|
layout1.addWidget(btnOK)
|
|
|
|
# 버튼 생성
|
|
btn1 = QPushButton('Button 1', self)
|
|
btn2 = QPushButton('Button 2', self)
|
|
|
|
# 버튼 클릭 시 발생하는 이벤트 연결
|
|
btn1.clicked.connect(self.on_click_btn1)
|
|
btn2.clicked.connect(self.on_click_btn2)
|
|
|
|
# 레이아웃 설정
|
|
layout = QVBoxLayout()
|
|
layout.addLayout(layout1)
|
|
layout.addWidget(btn1)
|
|
layout.addWidget(btn2)
|
|
|
|
# 레이아웃을 윈도우에 적용
|
|
self.setLayout(layout)
|
|
|
|
self.setWindowTitle('My First Application')
|
|
self.move(300, 300)
|
|
self.resize(400, 200)
|
|
self.show()
|
|
|
|
# 버튼 1 클릭 시 호출되는 메서드
|
|
def on_click_btn1(self):
|
|
print('Button 1 clicked!')
|
|
|
|
# 버튼 2 클릭 시 호출되는 메서드
|
|
def on_click_btn2(self):
|
|
print('Button 2 clicked!')
|
|
|
|
def on_click_btn_OK(self):
|
|
print(self.edit.text())
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication(sys.argv)
|
|
ex = MyApp()
|
|
sys.exit(app.exec_()) |