#🔒 Encapsulating widgets in Pyside6

14 messages · Page 1 of 1 (latest)

mossy flicker
#

Im building a calculator with pyside6 but i can't use the widgets like variables so i have to repeat lines and it just looks a mess

class window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Calculator")
        self.setMinimumSize(350, 450)
        self.resize(self.minimumSize())

        layoutV = QVBoxLayout()
        
        layoutH1 = QHBoxLayout()
        layoutH2 = QHBoxLayout()
        layoutH3 = QHBoxLayout()
        layoutH4 = QHBoxLayout()

        button7 = QPushButton("7"); button8 = QPushButton("8"); button9 = QPushButton("9"); buttonD = QPushButton("/")
        button4 = QPushButton("4"); button5 = QPushButton("5"); button6 = QPushButton("6"); buttonX = QPushButton("x")
        button1 = QPushButton("1"); button2 = QPushButton("2"); button3 = QPushButton("3"); buttonM = QPushButton("-")
        button0 = QPushButton("0"); buttonO = QPushButton("."); buttonP = QPushButton("+"); buttonE = QPushButton("=")

        layoutH1.addWidget(button7); layoutH1.addWidget(button8); layoutH1.addWidget(button9); layoutH1.addWidget(buttonD)
        layoutH2.addWidget(button6); layoutH2.addWidget(button5); layoutH2.addWidget(button4); layoutH2.addWidget(buttonX)
        layoutH3.addWidget(button1); layoutH3.addWidget(button2); layoutH3.addWidget(button3); layoutH3.addWidget(buttonM)
        layoutH4.addWidget(button0); layoutH4.addWidget(buttonO); layoutH4.addWidget(buttonP); layoutH4.addWidget(buttonE)

        layoutV.addLayout(layoutH1)
        layoutV.addLayout(layoutH2)
        layoutV.addLayout(layoutH3)
        layoutV.addLayout(layoutH4)

Tried changing the size of a bunch of buttons at once but didn't work:

        buttons = button0, button1, button2, button3, button4, button5, button6, button7, button8, button9
        buttons.setFixedSize(40, 40)

Any tips on how i can make the code simpler in general?

summer pondBOT
#

@mossy flicker

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

upbeat wyvern
mossy flicker
#

thing is

#

how would i resize the buttons of that list?

upbeat wyvern
mossy flicker
#

interesting idea

mossy flicker
mossy flicker
#
        num = 0
        buttton = QPushButton("0")

        for i in range(1, 4):
            num += 1
            buttton = QPushButton(f"{num}")
            buttton.setFixedSize(40, 40)
            layoutH1.addWidget(buttton)

        num = 3

        for i in range(4, 7):
            num += 1
            buttton = QPushButton(f"{num}")
            buttton.setFixedSize(40, 40)
            layoutH2.addWidget(buttton)

        num = 7

        for i in range(7, 10):
            num += 1
            buttton = QPushButton(f"{num}")
            buttton.setFixedSize(40, 40)
            layoutH3.addWidget(buttton)
#

figured out kinda

#

thx for the help!

upbeat wyvern
summer pondBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.