본문 바로가기
IT, 프로그램, SW개발/Python,파이썬, AI

[Python/파이썬] PyQt5 리스트 박스 사용 (list box)

by RedBaDa 2023. 3. 17.

 

 

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import QIcon
import sys


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("List box")
        self.resize(600,400)
        
        self.lbox = QListWidget(self)
        self.lbox.resize(200,300)
        
        self.te=QTextEdit(self)
        self.te.move(220,0)
        self.te.resize(200,80)
        
        self.btn = QPushButton("추가",self)
        self.btn.move(440,0)
        self.btn.resize(100,80)
        self.btn.clicked.connect(self.AddItem)
        
        self.lb_sel = QLabel("[테스트]",self)
        self.lb_sel.move(220,100)
        self.lbox.currentItemChanged.connect(self.OnLBoxSelectChange)
        self.btn_remove = QPushButton("선택 항목 삭제",self)
        self.btn_remove.move(220,200)
        self.btn_remove.clicked.connect(self.RemoveItem)
        self.btn_clear = QPushButton("모두 지우기",self)
        self.btn_clear.move(220,300)
        self.btn_clear.clicked.connect(self.ClearAll)
    def ClearAll(self):
        self.lbox.clear()
    def RemoveItem(self):
        index = self.lbox.currentRow()
        self.lbox.takeItem(index)
    def AddItem(self):
        data = self.te.toPlainText()
        self.lbox.addItem(data)
        self.te.setText("")
    def OnLBoxSelectChange(self):
        item = self.lbox.currentItem()
        self.lb_sel.setText(item.text())



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = MyWidget()
    MainWindow.show()
    sys.exit(app.exec_())

 

반응형