title: Python - Creating buttons in loop in PyQt5
body: <p>I've been trying to create an X number of buttons, based on the value of a variable in PyQt5, but my way is not working.</p>
  
  <p>I created a function with a loop, with the X value being the number of buttons.</p>
  
  <p>This code (function is commented) works:</p>
  
  <pre><code>class Ui_MainWindow(object):
  
  def setupUi(self, MainWindow):
  
      def createButtons(x):
          number_of_buttons = x
          table_set = 1
          distance_from_left = 5
  
          while table_set &lt;= number_of_buttons:
              print(distance_from_left) #Check if the loop is runnig
              "self.pushButton_{} = QtWidgets.QPushButton(self.groupBox)".format(table_set)
              "self.pushButton_{}.setGeometry(QtCore.QRect({}, 15, 40, 35))".format(table_set, distance_from_left)
              "self.pushButton_{}.setText(\"{}\")".format(table_set, str(table_set).zfill(2))
              "self.pushButton_{}.setObjectName(\"pushButton_{}\")".format(table_set, table_set)
              table_set += 1
              distance_from_left += 45
  
      MainWindow.setObjectName("MainWindow")
      MainWindow.resize(1366, 768)
      MainWindow.setAutoFillBackground(False)
      self.centralwidget = QtWidgets.QWidget(MainWindow)
      self.centralwidget.setObjectName("centralwidget")
      self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
      self.groupBox.setGeometry(QtCore.QRect(0, 80, 531, 56))
      self.groupBox.setAutoFillBackground(False)
      self.groupBox.setObjectName("groupBox")
      #createButtons(3)
      self.pushButton_1 = QtWidgets.QPushButton(self.groupBox)
      self.pushButton_1.setGeometry(QtCore.QRect(5, 15, 40, 35))
      self.pushButton_1.setText("01")
      self.pushButton_1.setObjectName("pushButton_1")
      self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
      self.pushButton_2.setGeometry(QtCore.QRect(50, 15, 40, 35))
      self.pushButton_2.setText("02")
      self.pushButton_2.setObjectName("pushButton_2")
      self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
      self.pushButton_3.setGeometry(QtCore.QRect(95, 15, 40, 35))
      self.pushButton_3.setText("03")
      self.pushButton_3.setObjectName("pushButton_3")
      MainWindow.setCentralWidget(self.centralwidget)
      self.centralwidget.show()
      QtCore.QMetaObject.connectSlotsByName(MainWindow)
  </code></pre>
  
  <p>This (function is running, direct commands are commented) doesn't work. To my eyes, they look the same.</p>
  
  <pre><code>from PyQt5 import QtCore, QtGui, QtWidgets
  
  class Ui_MainWindow(object):
  
      def setupUi(self, MainWindow):
  
          def createButtons(x):
              number_of_buttons = x
              table_set = 1
              distance_from_left = 5
  
              while table_set &lt;= number_of_buttons:
                  print(distance_from_left) #Check if the loop is runnig
                  "self.pushButton_{} = QtWidgets.QPushButton(self.groupBox)".format(table_set)
                  "self.pushButton_{}.setGeometry(QtCore.QRect({}, 15, 40, 35))".format(table_set, distance_from_left)
                  "self.pushButton_{}.setText(\"{}\")".format(table_set, str(table_set).zfill(2))
                  "self.pushButton_{}.setObjectName(\"pushButton_{}\")".format(table_set, table_set)
                  table_set += 1
                  distance_from_left += 45
  
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(1366, 768)
          MainWindow.setAutoFillBackground(False)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")
          self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
          self.groupBox.setGeometry(QtCore.QRect(0, 80, 531, 56))
          self.groupBox.setAutoFillBackground(False)
          self.groupBox.setObjectName("groupBox")
          createButtons(3)
          """
          self.pushButton_1 = QtWidgets.QPushButton(self.groupBox)
          self.pushButton_1.setGeometry(QtCore.QRect(5, 15, 40, 35))
          self.pushButton_1.setText("01")
          self.pushButton_1.setObjectName("pushButton_1")
          self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
          self.pushButton_2.setGeometry(QtCore.QRect(50, 15, 40, 35))
          self.pushButton_2.setText("02")
          self.pushButton_2.setObjectName("pushButton_2")
          self.pushButton_3 = QtWidgets.QPushButton(self.groupBox)
          self.pushButton_3.setGeometry(QtCore.QRect(95, 15, 40, 35))
          self.pushButton_3.setText("03")
          self.pushButton_3.setObjectName("pushButton_3")
          """
          MainWindow.setCentralWidget(self.centralwidget)
          self.centralwidget.show()
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  </code></pre>
  
  <p>None of the buttons show up. Can somebody please tell me what I'm doing wrong?</p>