from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QInputDialog from PyQt5.QtCore import Qt class MyWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.button = QPushButton('点击我') self.button.clicked.connect(self.createNewButton) self.layout = QVBoxLayout() self.layout.addWidget(self.button) self.setLayout(self.layout) def createNewButton(self): new_button = QPushButton('新按钮') new_button.clicked.connect(self.newButtonClicked) new_button.setContextMenuPolicy(Qt.CustomContextMenu) new_button.customContextMenuRequested.connect(self.showRenameDialog) self.layout.addWidget(new_button) def newButtonClicked(self): print('新按钮被点击') def showRenameDialog(self, pos): text, ok = QInputDialog.getText(self, '重命名', '输入新名称:') if ok and text: new_button = self.sender() new_button.setText(text) if __name__ == '__main__': app = QApplication([]) widget = MyWidget() widget.show() app.exec_()
测试新功能01:PyQt5 每点击一次按钮后创建一个新按钮,并将新按钮并连接到一个信号上,右键给新按钮重命名
最新推荐文章于 2024-08-31 21:39:38 发布