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

UI ์ˆ˜์ •ํ•˜๊ธฐ

by hyeonha 2022. 11. 23.

๋ชฉ์ฐจ

    ์ค‘๊ฐ„๊ณผ์ •

     

    mport sys
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout =QVBoxLayout()
    
    
            ## #๋ ˆ์ด์•„์›ƒ ํ‹€ ์žก๊ธฐ ###
    
            layout_operation_solution = QFormLayout()
            layout_funct1 = QGridLayout()
            layout_funct2 =QGridLayout()
    
    
            ### ์ˆ˜์‹ ์ž…๋ ฅ & ๊ฒฐ๊ณผ ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” operation_solution ์ฐฝ ๊ตฌํ˜„ ###
            self.operation_solution = QLineEdit()
            layout_operation_solution.addRow("", self.operation_solution)
            self.setLayout(main_layout)
            self.resize(400, 400)
            self.show()
    
            ### ๊ธฐ๋Šฅ funct1 ๋ฒ„ํŠผ ๊ตฌํ˜„ ###
            ###๋‚˜๋จธ์ง€
            self.remainder = QPushButton("%")
            ##CE
            self.clear_entry = QPushButton("CE")
            ## C
            self.clear = QPushButton("C")
            self.backspace = QPushButton("์ง€์šฐ๊ธฐ")
            ### 1/x
            self.reciprocal =QPushButton("1/x")
            ### ์ œ๊ณฑ
            self.squre = QPushButton("์ œ๊ณฑ")
            ### ์ œ๊ณฑ๊ทผ
            self.square_root=QPushButton("์ œ๊ณฑ๊ทผ")
            ### ๋‚˜๋ˆ„๊ธฐ
            self.devision = QPushButton("÷")
    
            layout_funct1.addWidget(self.remainder,0 , 0)
            layout_funct1.addWidget(self.clear_entry,0 , 1)
            layout_funct1.addWidget(self.clear,0 ,2)
            layout_funct1.addWidget(self.backspace,0 ,3)
            layout_funct1.addWidget(self.reciprocal,1 , 0)
            layout_funct1.addWidget(self.squre,1,1)
            layout_funct1.addWidget(self.square_root,1 ,2)
            layout_funct1.addWidget(self.devision,1 , 3)
    
            ### layout_number ๊ตฌ์„ฑํ•˜๊ธฐ ###
            self.button_production = QPushButton("x")
            self.button_minus =QPushButton("-")
            self.button_plus = QPushButton("+")
            self.button_result = QPushButton("=")
            self.button_plus_minus = QPushButton("+/-")
            self.button_add_dot = QPushButton(".")
    
            layout_funct2.addWidget(self.button_production,0,3)
            layout_funct2.addWidget(self.button_minus,1,3)
            layout_funct2.addWidget(self.button_plus,2,3)
            layout_funct2.addWidget(self.button_result,3,3)
            layout_funct2.addWidget(self.button_plus_minus,3,0)
            layout_funct2.addWidget(self.button_add_dot,3,2)
    
    
            number_button_dicct = {}
            for number in range(0, 10):
                number_button_dicct[number] = QPushButton(str(number))
                number_button_dicct[number].clicked.connect(lambda state, num=number: self.number_button_click(num))
    
                if number > 0:
                    x, y = divmod(number - 1, 3)
                    # 4์œผ๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€์— ๋”ฐ๋ผ ๋ฐฐ์น˜๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค.
                    layout_funct2.addWidget(number_button_dicct[number], x, y)
    
                elif number == 0:
                    layout_funct2.addWidget(number_button_dicct[number], 3, 1)
    
            ###main_layout์— ์ถ”๊ฐ€ ###
            main_layout.addLayout(layout_operation_solution)
            main_layout.addLayout(layout_funct1)
            main_layout.addLayout(layout_funct2)
    
    
    
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())


    import sys
    from PyQt5.QtWidgets import *
    
    class Main(QDialog):
        def __init__(self):
            super().__init__()
            self.init_ui()
    
        def init_ui(self):
            main_layout =QVBoxLayout()
    
    
            ## #๋ ˆ์ด์•„์›ƒ ํ‹€ ์žก๊ธฐ ###
    
            layout_operation_solution = QFormLayout()
            layout_funct1 = QGridLayout()
            layout_funct2 =QGridLayout()
    
    
            ### ์ˆ˜์‹ ์ž…๋ ฅ & ๊ฒฐ๊ณผ ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” operation_solution ์ฐฝ ๊ตฌํ˜„ ###
            self.operation_solution = QLineEdit()
            layout_operation_solution.addRow("", self.operation_solution)
            self.setLayout(main_layout)
            self.resize(400, 400)
            self.show()
    
            ### ๊ธฐ๋Šฅ funct1 ๋ฒ„ํŠผ ๊ตฌํ˜„ ###
            ###๋‚˜๋จธ์ง€
            self.remainder = QPushButton("%")
            ##CE
            self.clear_entry = QPushButton("CE")
            ## C
            self.clear = QPushButton("C")
            self.backspace = QPushButton("์ง€์šฐ๊ธฐ")
            ### 1/x
            self.reciprocal =QPushButton("1/x")
            ### ์ œ๊ณฑ
            self.squre = QPushButton("์ œ๊ณฑ")
            ### ์ œ๊ณฑ๊ทผ
            self.square_root=QPushButton("์ œ๊ณฑ๊ทผ")
            ### ๋‚˜๋ˆ„๊ธฐ
            self.devision = QPushButton("÷")
    
            layout_funct1.addWidget(self.remainder,0 , 0)
            layout_funct1.addWidget(self.clear_entry,0 , 1)
            layout_funct1.addWidget(self.clear,0 ,2)
            layout_funct1.addWidget(self.backspace,0 ,3)
            layout_funct1.addWidget(self.reciprocal,1 , 0)
            layout_funct1.addWidget(self.squre,1,1)
            layout_funct1.addWidget(self.square_root,1 ,2)
            layout_funct1.addWidget(self.devision,1 , 3)
    
            ### layout_number ๊ตฌ์„ฑํ•˜๊ธฐ ###
            self.button_production = QPushButton("x")
            self.button_minus =QPushButton("-")
            self.button_plus = QPushButton("+")
            self.button_result = QPushButton("=")
            self.button_plus_minus = QPushButton("+/-")
            self.button_add_dot = QPushButton(".")
    
            layout_funct2.addWidget(self.button_production,0,3)
            layout_funct2.addWidget(self.button_minus,1,3)
            layout_funct2.addWidget(self.button_plus,2,3)
            layout_funct2.addWidget(self.button_result,3,3)
            layout_funct2.addWidget(self.button_plus_minus,3,0)
            layout_funct2.addWidget(self.button_add_dot,3,2)
    
    
            number_button_dicct = {}
            for number in range(0, 10):
                number_button_dicct[number] = QPushButton(str(number))
    
                if number > 0:
                    x, y = divmod(number - 1, 3)
                    # 3์œผ๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€์— ๋”ฐ๋ผ ๋ฐฐ์น˜๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค.
                    if x==0:
                        layout_funct2.addWidget(number_button_dicct[number], 2, y)
                    elif x==2:
                        layout_funct2.addWidget(number_button_dicct[number], 0, y)
                    else:
                        layout_funct2.addWidget(number_button_dicct[number], x, y)
    
                elif number == 0:
                    layout_funct2.addWidget(number_button_dicct[number], 3, 1)
    
            ###main_layout์— ์ถ”๊ฐ€ ###
            main_layout.addLayout(layout_operation_solution)
            main_layout.addLayout(layout_funct1)
            main_layout.addLayout(layout_funct2)
    
    
    
    if __name__ =='__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())


    LineEdit()๊ฐ€ ์žˆ๋Š” ์ € ์ž…๋ ฅ์ฐฝ ํฌ๊ธฐ ํ‚ค์šฐ๊ธฐ

    self.operation_solution.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    

    ์ž…๋ ฅ๋„ ์™ผ์ชฝ๋ถ€ํ„ฐ ๋˜๋„๋ก ํ•ด์•ผํ•˜๋‚˜์š”? 

     

     

    728x90