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

GUI ๊ฐ•์˜3

by hyeonha 2022. 11. 21.

๋ชฉ์ฐจ


    Fornlayout์„ ์ด์šฉํ•˜์—ฌ ์ž๊ธฐ์†Œ๊ฐœ UI๋งŒ๋“ค๊ธฐ

    ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ๋งŽ์ด ์ด์šฉ๋˜๋Š” ํ˜•ํƒœ

    ํšŒ์›๊ฐ€์ž…, ์ œํ’ˆ ์ŠคํŽ™ ๋“ฑ์„ ํ‘œํ˜„ํ•˜๋Š”๋ฐ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

     

    PyQt์—์„œ๋Š” 

    QFormLayout์„ ์ œ๊ณตํ•œ๋‹ค.

    ํ•œ์ค„ ํ•œ์ค„๋งˆ๋‹ค Lable: field ํ˜•ํƒœ์˜ ๋ฐฐ์น˜ ์ œ๊ณต

     

    A : a

    B : b

    C : c

     

    Label : Field

    Widget : Widget

     

    Label, Field ๋ชจ๋‘ QLabel

    ๋ณดํ†ต Label์€ text, Field์—๋Š” widget

     


    [๊ธฐ๋ณธ ์ฝ”๋”ฉ] 

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

     

     

    def init_ui(self):
        main_layout =QFormLayout()
    
        #์ด๋ฆ„ text๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” widget
        name_widget = QLineEdit()
        #addRow ํ•œ์ค„ ํ•œ์ค„ ์ž…๋ ฅ
        #addRow(label Widget, field Widget)
        main_layout.addRow("Name: ",name_widget)
        self.setLayout(main_layout)
        self.resize(500,500)
        self.show()

     


    ์ƒ์ผ 

    def init_ui(self):
        main_layout =QFormLayout()
    
        #์ด๋ฆ„ text๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” widget
        name_widget = QLineEdit()
        #์ƒ์ผ ์ž…๋ ฅ
        #๋…„๋„ ๋‹ฌ ์ผ์„ ํ•ด์„œ ์ด๊ฒƒ์„ QHorizontalBox layout์œผ๋กœ ๋ฌถ์–ด์ค€๋‹ค.
    
        birthday_layout = QHBoxLayout()
        year_widget = QComboBox()
        month_widget= QComboBox()
        date_widget = QComboBox()
    
        birthday_layout.addWidget(year_widget)
        birthday_layout.addWidget(month_widget)
        birthday_layout.addWidget(date_widget)
    
        for year in range(1900, 2023):
            year_widget.addItem(str(year))
        for month in range(1,13):
            month_widget.addItem(str(month))
        for date in range(1,32):
            date_widget.addItem(str(date))
    
    
        #addRow ํ•œ์ค„ ํ•œ์ค„ ์ž…๋ ฅ
        #addRow(label Widget, field Widget)
        #label์€ Name, ์˜ค๋ฅธ์ชฝ์—๋Š” ์ด๋ฆ„์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” lineedit์ด ๋œฌ๋‹ค.
    
        main_layout.addRow("Name: ", name_widget)
        main_layout.addRow("Birthday: ", birthday_layout)
    
        self.setLayout(main_layout)
        self.resize(500,500)
        self.show()


    ์ฃผ์†Œ ์ž…๋ ฅ

    def init_ui(self):
        main_layout =QFormLayout()
    
        #์ด๋ฆ„ text๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” widget
        name_widget = QLineEdit()
        #์ƒ์ผ ์ž…๋ ฅ
        #๋…„๋„ ๋‹ฌ ์ผ์„ ํ•ด์„œ ์ด๊ฒƒ์„ QHorizontalBox layout์œผ๋กœ ๋ฌถ์–ด์ค€๋‹ค.
    
        birthday_layout = QHBoxLayout()
        year_widget = QComboBox()
        month_widget= QComboBox()
        date_widget = QComboBox()
    
        birthday_layout.addWidget(year_widget)
        birthday_layout.addWidget(month_widget)
        birthday_layout.addWidget(date_widget)
    
        for year in range(1900, 2023):
            year_widget.addItem(str(year))
        for month in range(1,13):
            month_widget.addItem(str(month))
        for date in range(1,32):
            date_widget.addItem(str(date))
    
        #์ฃผ์†Œ
        address_layout  = QVBoxLayout()
        address_1 =QComboBox()
        address_1.addItems( ["Seoul","Daejeon","Daegu","Busan"])
        address_2 = QLineEdit()
        address_layout.addWidget(address_1)
        address_layout.addWidget(address_2)
    
    
        #addRow ํ•œ์ค„ ํ•œ์ค„ ์ž…๋ ฅ
        #addRow(label Widget, field Widget)
        #label์€ Name, ์˜ค๋ฅธ์ชฝ์—๋Š” ์ด๋ฆ„์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” lineedit์ด ๋œฌ๋‹ค.
    
        main_layout.addRow("Name: ", name_widget)
        main_layout.addRow("Birthday: ", birthday_layout)
        main_layout.addRow("Address: ", address_layout)
    
        self.setLayout(main_layout)
        self.resize(500,500)
        self.show()
    


    ์ด๋ฉ”์ผ ์ž…๋ ฅ

    ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€ํ•˜๊ธฐ 

    #์ด๋ฉ”์ผ
    email_layout = QHBoxLayout()
    email_id = QLineEdit()
    email_company = QLineEdit()
    
    email_layout.addWidget(email_id)
    email_layout.addWidget( QLabel("@"))
    email_layout.addWidget(email_company)
    
    
    #addRow ํ•œ์ค„ ํ•œ์ค„ ์ž…๋ ฅ
    #addRow(label Widget, field Widget)
    #label์€ Name, ์˜ค๋ฅธ์ชฝ์—๋Š” ์ด๋ฆ„์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” lineedit์ด ๋œฌ๋‹ค.
    
    main_layout.addRow("Name: ", name_widget)
    main_layout.addRow("Birthday: ", birthday_layout)
    main_layout.addRow("Address: ", address_layout)
    main_layout.addRow("Email: " ,email_layout)
    
    self.setLayout(main_layout)
    self.resize(500,500)
    self.show()

    additems() ํ•˜๋ฉด ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ…์ŠคํŠธ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

    email_combobox = QComboBox()
    email_combobox.addItems(["google.com", "naver.com","daum.net"])


    ์ „ํ™”๋ฒˆํ˜ธ ์ž…๋ ฅ

    #์ „ํ™”๋ฒˆํ˜ธ
    phone_number_layout= QHBoxLayout()
    phone_number_layout.addWidget(QLineEdit())
    phone_number_layout.addWidget(QLabel("-"))
    phone_number_layout.addWidget(QLineEdit())
    phone_number_layout.addWidget(QLabel("-"))
    phone_number_layout.addWidget(QLineEdit())
    
    #addRow ํ•œ์ค„ ํ•œ์ค„ ์ž…๋ ฅ
    #addRow(label Widget, field Widget)
    #label์€ Name, ์˜ค๋ฅธ์ชฝ์—๋Š” ์ด๋ฆ„์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” lineedit์ด ๋œฌ๋‹ค.
    
    main_layout.addRow("Name: ", name_widget)
    main_layout.addRow("Birthday: ", birthday_layout)
    main_layout.addRow("Address: ", address_layout)
    main_layout.addRow("Email: " ,email_layout)
    main_layout.addRow("Phone number: " ,phone_number_layout)


    ํ‚ค 

    QSpinBox() ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์œ„์ ฏ์ด๋‹ค. 

    ๊ธฐ๋ณธ์ ์œผ๋กœ ์ตœ๋Œ€๊ฐ’์ด 99๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค.

    #ํ‚ค
    height_widget = QSpinBox()
    height_widget.setMaximum(250)
    main_layout.addRow("Height: ", height_widget)
    


    ๊ฐœ์ธ์ •๋ณด ์ด์šฉ ๋™์˜ ์—ฌ๋ถ€

    QCheckBox

    #๋™์˜
    personal_info = QCheckBox("Agree?")
    
    main_layout.addRow("Personal Information Share: ", personal_info)
    


    ์ž๊ธฐ์†Œ๊ฐœ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„

    ๊ทผ๋ฐ QLineEdit()์€ ํ•œ์ค„๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ œํ•œ์ด ์žˆ๋‹ค. ๊ทธ๋Ÿด ๋–„๋Š” QPlainTextEdit()์„ ์‚ฌ์šฉํ•ด๋ณด์ž.

    #์ž๊ธฐ์†Œ๊ฐœ
    self_intro = QPlainTextEdit()
    main_layout.addRow("Self Introduction", self_intro)
    


    ์ž‘์„ฑ ์™„๋ฃŒ ํ›„ ์ €์žฅ ์ทจ์†Œ ๋ฒ„ํŠผ 

    #์ €์žฅ๋ฒ„ํŠผ
    save_cancel_layout = QHBoxLayout()
    save_button = QPushButton("save")
    cancel_button = QPushButton("cancel")
    save_cancel_layout.addWidget(save_button)
    save_cancel_layout.addWidget(cancel_button)
    
    main_layout.addRow("",save_cancel_layout)
    

    ใ…“

    728x90

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

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