本帖最后由 hrpzcf 于 2022-9-21 10:15 编辑
getnew里你只创建了self.btn1,并没有把放到界面上,怎么会有反应呢
view.pyfrom PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QMainWindow, QPushButton, QVBoxLayout
class View(QMainWindow):
def __init__(self):
super(View, self).__init__()
self.setUI()
def setUI(self):
self.widgetWin = QWidget()
self.btn = QPushButton(self.widgetWin)
self.btn.setText("我是旧按钮")
self.layout1 = QVBoxLayout(self.widgetWin)
self.layout1.addWidget(self.btn)
self.setCentralWidget(self.widgetWin)
def getnew(self, str):
"""添加新按钮方法"""
print("调用了getnew")
self.btn1 = QtWidgets.QPushButton(str)
self.btn1.setText("我是新按钮")
self.layout1.addWidget(self.btn1)
|