๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2022/์˜คํ”ˆ์†Œ์Šค ์†Œํ”„ํŠธ์›จ์–ด ํ”„๋กœ์ ํŠธ

GUI ๊ฐ•์˜ 4

by hyeonha 2022. 11. 22.

๋ชฉ์ฐจ

    Widget์— ์ด๋ฒคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ž‘๋™์‹œํ‚ค๊ธฐ 

     

    ๊ธฐ๋ณธ ์ฝ”๋“œ

    import sys
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout =QVBoxLayout()
    
            self.setLayout(main_layout)
            self.resize(400,400)
            self.show()
    
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())

    ๋ฒ„ํŠผ ์ถ”๊ฐ€ํ•˜๊ธฐ

    pushbutton = QPushButton("button1")
    
    main_layout.addWidget(pushbutton)

    ์ด๋ฒคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

    pushbutton.clicked.connect()
    

     

     

    ()์— ํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ๋œ๋‹ค.

    pushbutton.clicked.connect(self.button_clicked)
    
    def button_clicked(self):
        print("button clicked")

     

    self.lineedit=QLineEdit()
    main_layout.addWidget(self.lineedit)
    def button_clicked(self):
        self.lineedit.setText("button clicked")
    

    ์ฝค๋ณด๋ฐ•์Šค ์ด๋ฒคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ

    #์ฝค๋ณด๋ฐ•์Šค ์ด๋ฒคํŠธ
    combobox = QComboBox()
    combobox.addItems(["dog", "cat", "rabbit", "lion"])
    combobox.currentTextChanged.connect(self.combobox_changed)
    main_layout.addWidget(combobox)
    
    self.lineedit_combobox = QLineEdit()
    main_layout.addWidget(self.lineedit_combobox)
    def combobox_changed(self, item):
        self.lineedit_combobox.setText(item)
        #์ฝค๋ณด๋ฐ•์Šค์—์„œ ์–ด๋–ค ๋™๋ฌผ์ด ์„ ํƒ๋˜์—ˆ๋Š”์ง€ ์•Œ์•„์•ผํ•˜๊ณ (currentTextChanged ํ•จ์ˆ˜์— ์žˆ๋‹ค.)
        # ๊ทธ ๋™๋ฌผ์„ textํ˜•ํƒœ๋กœ ๋„์›Œ์ฃผ์•ผํ•œ๋‹ค.

    ์ฒดํฌ ๋ฐ•์Šค ์ด๋ฒคํŠธ ์ถ”๊ฐ€

    
    #์ฒดํฌ๋ฐ•์Šค ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
    checkbox = QCheckBox("item")
    checkbox.stateChanged.connect(self.checkbox_changed)
    main_layout.addWidget(checkbox)
    
    self.lineedit_checkbox = QLineEdit()
    main_layout.addWidget(self.lineedit_checkbox)
    
    def checkbox_changed(self,state):
        self.lineedit_checkbox.setText(str(state))

    ๋ผ๋””์˜ค ๋ฒ„ํŠผ

    #๋ผ๋””์˜ค ๋ฒ„ํŠผ
    radio1, radio2 = QRadioButton("A"), QRadioButton("B")
    #๋ผ๋””์˜ค ๋ฒ„ํŠผ ์ด๋ฒคํŠธ on/off์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒƒ -> toggled
    radio1.toggled.connect(self.radio1_toggled)
    radio2.toggled.connect(self.radio2_toggled)
    
    main_layout.addWidget(radio1)
    main_layout.addWidget(radio2)
    
    self.lineedit_radio1 =QLineEdit()
    self.lineedit_radio2 =QLineEdit()
    main_layout.addWidget(self.lineedit_radio1)
    main_layout.addWidget(self.lineedit_radio2)
    def radio1_toggled(self, state):
        self.lineedit_radio1.setText(str(state))
    def radio2_toggled(self, state):
        self.lineedit_radio2.setText(str(state))

     

    728x90

    '2022 > ์˜คํ”ˆ์†Œ์Šค ์†Œํ”„ํŠธ์›จ์–ด ํ”„๋กœ์ ํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    QGridLayout  (0) 2022.11.23
    GUI ๊ฐ•์˜ 5  (0) 2022.11.23
    GUI ๊ฐ•์˜3  (0) 2022.11.21
    GUI ๊ฐ•์˜2: ๋ ˆ์ด์•„์›ƒ  (0) 2022.11.20
    GUI ๊ฐ•์˜ 1  (0) 2022.11.20