我正在尝试在现有GUI上动态添加一个按钮。 我已将“按钮”连接到“ AddButton”功能,我试图动态添加第三个按钮,但在GUI上看不到它。
尽管,我可以通过编程方式访问以下代码,并返回按钮文本!
print(self.ui.pushButton_3.text())
我在这里做错了什么?
我有此代码:
import os
os.system("C:\Python37\python -m PyQt5.uic.pyuic -x beta.ui -o beta_gui.py")
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from beta_gui import Ui_MainWindow # importing our generated file
import sys
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.AddButton)
def AddButton(self,result):
try:
self.ui.pushButton_3 = QtWidgets.QPushButton(self.ui.centralwidget)
self.ui.pushButton_3.setGeometry(QtCore.QRect(0, 0, 75, 23))
self.ui.pushButton_3.setObjectName("pushButton_3")
self.ui.pushButton_3.setText("buton_3")
self.update()
print(self.ui.pushButton_3.text())
self.show()
except Exception as e:
print(e)
return
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())
beta_gui.py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'beta.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(344, 301)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(140, 90, 101, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(40, 90, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 344, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Button"))
self.pushButton_2.setText(_translate("MainWindow", "Button_2"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
谢谢!
答案 0 :(得分:0)
尝试一下:
#import os
#os.system("C:\Python37\python -m PyQt5.uic.pyuic -x beta.ui -o beta_gui.py")
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from beta_gui import Ui_MainWindow # importing our generated file
import sys
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.AddButton)
self.num = 3
self.layout = QtWidgets.QVBoxLayout(self.ui.centralwidget)
self.layout.addWidget(self.ui.pushButton)
self.layout.addWidget(self.ui.pushButton_2)
def AddButton(self,result):
button = QtWidgets.QPushButton("button {}".format(self.num))
button.clicked.connect(lambda: print(button.text()))
self.layout.addWidget(button)
self.num += 1
# try:
# self.ui.pushButton_3 = QtWidgets.QPushButton(self.ui.centralwidget)
# self.ui.pushButton_3.setGeometry(QtCore.QRect(0, 0, 75, 23))
# self.ui.pushButton_3.setObjectName("pushButton_3")
# self.ui.pushButton_3.setText("buton_3")
# self.update()
# print(self.ui.pushButton_3.text())
# self.show()
# except Exception as e:
# print(e)
# return
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())