How do you know what kind of button was PyQt5



  • I want to write a browser. I'm trying to figure out how to make any deposits, remove them, and generally make any other changes.

    How am I supposed to know which button was hit, that's her number?

    class Button(QPushButton):
        def __init__(self):
            QPushButton.__init__(self)
            self.setStyleSheet("background-color: rgb(255,0,0);")
    

    class Window(QWidget):
    def init(self, parent = None):
    QWidget.init(self, parent)
    self.layout = QGridLayout()
    self.buttons = [Button() for i in range(100)]
    i = -1
    for button in self.buttons:
    i+=1
    button.clicked.connect(lambda: self.printer(i)) #Так все время i = 99, я бы хотел, чтобы i был равен номеру нажатой кнопки
    self.layout.addWidget(button,i//30,i%30)
    self.setLayout(self.layout)

    def printer(self):
        self.buttons[num].setText(f"{num}")
    



  • Try this:

    import sys
    from PyQt5.Qt import *
    

    class Button(QPushButton):
    def init(self):
    QPushButton.init(self)
    self.setStyleSheet("background-color: rgb(255,0,0);")
    self.setMinimumSize(30, 30)
    self.setMaximumSize(30, 30)

    class Window(QWidget):
    def init(self, parent = None):
    QWidget.init(self, parent)

        self.layout = QGridLayout()
        self.buttons = [Button() for i in range(100)]
    

    i = -1

        for i, button in enumerate(self.buttons):
    

    i+=1

            button.clicked.connect(lambda ch, num=i: self.printer(num))   
            self.layout.addWidget(button, i//30, i%30)
        self.setLayout(self.layout)
    
    def printer(self, num):
        self.buttons[num].setText(f"{num}")
    

    if name == "main":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

    введите сюда описание изображения



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2