Есть кнопки: Button0
, Button1
, Button2
и Button3
и переменная f
.
При нажатии на кнопку Button0
:
global f
f = 0
def Button0_clicked(self):
f + 1
btn = "Button"
btnname = btn + str(f)
self.ui.btnname.show()
self.ui.btnname.settext('settext')
Выводит ошибку -
AttributeError: 'Ui_MainWindow' object has no attribute 'btnname'
Как сделать так, чтобы он считал btnname
переменной, а не именем кнопки
(или же есть другой вариант как это сделать)
весь код:
import sys
import os
import re
from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(747, 398)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Button1 = QtWidgets.QPushButton(self.centralwidget)
self.Button1.setGeometry(QtCore.QRect(260, 80, 75, 23))
self.Button1.setObjectName("Button1")
self.Button2 = QtWidgets.QPushButton(self.centralwidget)
self.Button2.setGeometry(QtCore.QRect(350, 80, 75, 23))
self.Button2.setObjectName("Button2")
self.Button3 = QtWidgets.QPushButton(self.centralwidget)
self.Button3.setGeometry(QtCore.QRect(440, 80, 75, 23))
self.Button3.setObjectName("Button3")
self.Button0 = QtWidgets.QPushButton(self.centralwidget)
self.Button0.setGeometry(QtCore.QRect(170, 40, 75, 23))
self.Button0.setObjectName("Button0")
self.Button1.hide()
self.Button2.hide()
self.Button3.hide()
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.action = QtWidgets.QAction(MainWindow)
self.action.setObjectName("action")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Pr"))
self.Button1.setText(_translate("MainWindow", "1"))
self.Button2.setText(_translate("MainWindow", "2"))
self.Button3.setText(_translate("MainWindow", "3"))
self.Button0.setText(_translate("MainWindow", "Добавить"))
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Button1.clicked.connect(self.Button1_clicked)
self.ui.Button2.clicked.connect(self.Button2_clicked)
self.ui.Button3.clicked.connect(self.Button3_clicked)
self.ui.Button0.clicked.connect(self.Button0_clicked)
global f
f = 0
def Button0_clicked(self):
f + 1
btn = "Button"
btnname = btn + str(f)
self.ui.btnname.show()
self.ui.btnname.settext('settext')
def Button1_clicked(self):
null()
def Button2_clicked(self):
null()
def Button3_clicked(self):
null()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())