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

GUI ๊ฐ•์˜2: ๋ ˆ์ด์•„์›ƒ

by hyeonha 2022. 11. 20.

๋ชฉ์ฐจ

    import sys
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    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(500,500)
            self.show()
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_()

    ๋นˆ ์ฐฝ

    ์œ„์ ฏ ์ถ”๊ฐ€

     

    import sys
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout = QVBoxLayout()
            #vertical box layout ์ด๋ผ์„œ add์‹œ์ผœ์ค€ ์ˆœ์„œ๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋‹ด๊ธด๋‹ค. -> ์œ„์ ฏ์„ ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜
            #horizontal๋กœ ํ•ด์ฃผ๋ ค๋ฉด V๋ฅผ H๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. -> ๊ฐ€๋กœ๋ฐฉํ–ฅ ๋ฐฐ์น˜ 
    
            w_1 = QPushButton("widget_1")
            w_2 = QPushButton("widget_2")
            w_3 = QPushButton("widget_3")
    
            main_layout.addWidget(w_1)
            main_layout.addWidget(w_2)
            main_layout.addWidget(w_3)
    
            self.setLayout(main_layout)
            sel

     

    ํ™”๋ฉด์€ ๋Œ€๋ถ€๋ถ„ 2์ฐจ์›

    w_1 = QPushButton("widget_1")
    w_2 = QPushButton("widget_2")
    w_3 = QPushButton("widget_3")
    w_4 = QPushButton("widget_4")
    
    main_layout.addWidget(w_1, 0, 0) #ํ–‰ ์„ฑ๋ถ„๊ณผ ์—ด ์„ฑ๋ถ„์„ ์ ์–ด์ค€๋‹ค.
    main_layout.addWidget(w_2, 0, 1)
    main_layout.addWidget(w_3, 1, 0)
    main_layout.addWidget(w_4, 1, 1)

    QGridLayout์€ 2์ฐจ์› ๋ฐฐ์น˜์ด๋‹ค. 

    4๊ฐœ๋ฅผ 2*2 ํ–‰๋ ฌ์ฒ˜๋Ÿผ ๋ฐฐ์น˜

     

    ํ˜ผํ•ฉ

    import sys
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout =QVBoxLayout()
            #vertical box layout ์ด๋ผ์„œ add์‹œ์ผœ์ค€ ์ˆœ์„œ๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋‹ด๊ธด๋‹ค. -> ์œ„์ ฏ์„ ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜
            #horizontal๋กœ ํ•ด์ฃผ๋ ค๋ฉด V๋ฅผ H๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. -> ๊ฐ€๋กœ๋ฐฉํ–ฅ ๋ฐฐ์น˜
    
            w_1 = QPushButton("widget_1")
            w_2 = QPushButton("widget_2")
            w_3 = QPushButton("widget_3")
            w_4 = QPushButton("widget_4")
            w_5 = QPushButton("widget_5")
    
            layout_1 = QHBoxLayout()
            layout_2 = QHBoxLayout()
            layout_1.addWidget(w_1)
            layout_1.addWidget(w_2)
            layout_2.addWidget(w_4)
            layout_2.addWidget(w_5)
    
    
            main_layout.addLayout(layout_1)
            main_layout.addWidget(w_3)
            main_layout.addLayout(layout_2)
    
            self.setLayout(main_layout)
            self.resize(500,500)
            self.show()
    
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())

    ์กฐ๊ธˆ ๋” ๋ณต์žกํ•œ ๋ฐฐ์น˜๋ฅผ ํ•ด๋ณด์ž

    import sys
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout =QHBoxLayout()
            #vertical box layout ์ด๋ผ์„œ add์‹œ์ผœ์ค€ ์ˆœ์„œ๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋‹ด๊ธด๋‹ค. -> ์œ„์ ฏ์„ ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜
            #horizontal๋กœ ํ•ด์ฃผ๋ ค๋ฉด V๋ฅผ H๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. -> ๊ฐ€๋กœ๋ฐฉํ–ฅ ๋ฐฐ์น˜
    
            w_1 = QPushButton("widget_1")
            w_2 = QPushButton("widget_2")
            w_3 = QPushButton("widget_3")
            w_4 = QPushButton("widget_4")
            w_5 = QPushButton("widget_5")
    
            #๊ฐ€์žฅ ์ž‘์€ ๋ ˆ์ด์•„์›ƒ๋ถ€ํ„ฐ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค
            layout_3 = QVBoxLayout()
            layout_3.addWidget(w_4)
            layout_3.addWidget(w_5)
    
            layout_2=QHBoxLayout()
            layout_2.addWidget(w_3)
            layout_2.addLayout(layout_3)
    
            layout_1 = QVBoxLayout()
            layout_1.addWidget(w_2)
            layout_1.addLayout(layout_2)
    
            main_layout.addWidget(w_1)
            main_layout.addLayout(layout_1)
    
            self.setLayout(main_layout)
            self.resize(500,500)
            self.show()
    
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())

    ๋” ํ•ด๋ณด์ž~

    import sys
    
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout =QVBoxLayout()
            #vertical box layout ์ด๋ผ์„œ add์‹œ์ผœ์ค€ ์ˆœ์„œ๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋‹ด๊ธด๋‹ค. -> ์œ„์ ฏ์„ ์ˆ˜์ง์œผ๋กœ ๋ฐฐ์น˜
            #horizontal๋กœ ํ•ด์ฃผ๋ ค๋ฉด V๋ฅผ H๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. -> ๊ฐ€๋กœ๋ฐฉํ–ฅ ๋ฐฐ์น˜
    
            w_1 = QPushButton("widget_1")
            w_2 = QPushButton("widget_2")
            w_3 = QPushButton("widget_3")
            w_4 = QPushButton("widget_4")
            w_5 = QPushButton("widget_5")
            w_6 = QPushButton("widget_6")
            w_7 = QPushButton("widget_7")
            w_8 = QPushButton("widget_8")
            w_9 = QPushButton("widget_9")
            w_10 = QPushButton("widget_10")
            w_11 = QPushButton("widget_11")
    
            layout_attacker = QHBoxLayout()
            layout_midfielder = QHBoxLayout()
            layout_defender = QHBoxLayout()
    
            layout_attacker.addWidget(w_1)
            layout_attacker.addWidget(w_2)
            layout_attacker.addWidget(w_3)
    
            layout_midfielder.addWidget(w_4)
            layout_midfielder.addWidget(w_5)
            layout_midfielder.addWidget(w_6)
    
            layout_defender.addWidget(w_7)
            layout_defender.addWidget(w_8)
            layout_defender.addWidget(w_9)
            layout_defender.addWidget(w_10)
    
            main_layout.addLayout(layout_attacker)
            main_layout.addLayout(layout_midfielder)
            main_layout.addLayout(layout_defender)
            main_layout.addWidget(w_11)
    
    
            self.setLayout(main_layout)
            self.resize(500,500)
            self.show()
    
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())

     

    728x90

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

    QGridLayout  (0) 2022.11.23
    GUI ๊ฐ•์˜ 5  (0) 2022.11.23
    GUI ๊ฐ•์˜ 4  (0) 2022.11.22
    GUI ๊ฐ•์˜3  (0) 2022.11.21
    GUI ๊ฐ•์˜ 1  (0) 2022.11.20