PyQt(6) - QButtonGroup, QMessageBox

Gyeomiiยท2022๋…„ 6์›” 24์ผ
0

DDITPython

๋ชฉ๋ก ๋ณด๊ธฐ
11/18
post-thumbnail

๐Ÿ“Œ ์ „ํ™”๊ธฐ ๋งŒ๋“ค๊ธฐ

์ฝ”๋“œ

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic

# UIํŒŒ์ผ ์—ฐ๊ฒฐ
form_class = uic.loadUiType("myQt09.ui")[0]

# ํ™”๋ฉด์„ ๋„์šฐ๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” Class ์„ ์–ธ
class WindowClass(QMainWindow, form_class):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        # ๋ฒ„ํŠผ์— ๊ธฐ๋Šฅ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ฝ”๋“œ
				#QButton์„ ๊ทธ๋ฃนํ™” ํ•˜๋Š” ์ฝ”๋“œ
        self.pbGroup = QButtonGroup()
        self.pbGroup.setExclusive(False)
        self.pbGroup.buttonClicked[int].connect(self.dial)
        self.pb_call.clicked.connect(self.call)
        # ๊ทธ๋ฃน์— ๋ฒ„ํŠผ ๋‹ด๊ธฐ
        self.pbGroup.addButton(self.pb1,1)
        self.pbGroup.addButton(self.pb2,2)
        self.pbGroup.addButton(self.pb3,3)
        self.pbGroup.addButton(self.pb4,4)
        self.pbGroup.addButton(self.pb5,5)
        self.pbGroup.addButton(self.pb6,6)
        self.pbGroup.addButton(self.pb7,7)
        self.pbGroup.addButton(self.pb8,8)
        self.pbGroup.addButton(self.pb9,9)
        self.pbGroup.addButton(self.pb0,0)
        
    # pb๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด ์ž‘๋™ํ•  ํ•จ์ˆ˜
    def dial(self, id):
        for pb in self.pbGroup.buttons():
            if pb is self.pbGroup.button(id):
                txt = self.qle.text()
                txt += pb.text()
                self.qle.setText(txt)
                
    def call(self):
        QMessageBox.about(self, 'MyQt09 Calling alert', "Calling to %s"%(self.qle.text()))

if __name__ == "__main__":
    app = QApplication(sys.argv) 
    myWindow = WindowClass() 
    myWindow.show()
    app.exec_()

์„ค๋ช…

				#QButton์„ ๊ทธ๋ฃนํ™” ํ•˜๋Š” ์ฝ”๋“œ
        self.pbGroup = QButtonGroup()
        self.pbGroup.setExclusive(False) # ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค
        self.pbGroup.buttonClicked[int].connect(self.dial)
        self.pb_call.clicked.connect(self.call)
  • self.๊ทธ๋ฃน๋ช… = QButtonGroup()
    • ๊ทธ๋ฃน๋ช…์— QButton์„ ๋‹ด๋Š”๋‹ค.
  • self.๊ทธ๋ฃน๋ช….buttonClicked[int].connect(self.๋ฉ”์†Œ๋“œ๋ช…)
				# ๊ทธ๋ฃน์— ๋ฒ„ํŠผ ๋‹ด๊ธฐ
        self.pbGroup.addButton(self.pb1,1)
        self.pbGroup.addButton(self.pb2,2)
        self.pbGroup.addButton(self.pb3,3)
        self.pbGroup.addButton(self.pb4,4)
        self.pbGroup.addButton(self.pb5,5)
        self.pbGroup.addButton(self.pb6,6)
        self.pbGroup.addButton(self.pb7,7)
        self.pbGroup.addButton(self.pb8,8)
        self.pbGroup.addButton(self.pb9,9)
        self.pbGroup.addButton(self.pb0,0)
  • self.pbGroup.addButton(self.๋ฒ„ํŠผ๋ช…, Id๊ฐ’)
# pb๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด ์ž‘๋™ํ•  ํ•จ์ˆ˜
    def dial(self, id):
        for pb in self.pbGroup.buttons():
            if pb is self.pbGroup.button(id):
                txt = self.qle.text()
                txt += pb.text()
                self.qle.setText(txt)
  • ๋ฒ„ํŠผ๊ทธ๋ฃน์•ˆ์— pb๊ฐ€ ๋ˆŒ๋ฆฌ๋ฉด ๋ˆŒ๋ฆฐ pb์˜ ์•„์ด๋””์™€ ์กด์žฌํ•˜๋Š” pb์˜ ์•„์ด๋””๊ฐ€ ๊ฐ™์„ ๋•Œ ๊ทธ ํ…์ŠคํŠธ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ

profile
๊น€์„ฑ๊ฒธ

0๊ฐœ์˜ ๋Œ“๊ธ€