56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
![]() |
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
|
Created on Mon Sep 23 09:34:24 2024
|
||
|
|
||
|
@author: WANGXIBAO
|
||
|
"""
|
||
|
|
||
|
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QVBoxLayout, QDialog, QDialogButtonBox, QMessageBox
|
||
|
|
||
|
class RenameDialog(QDialog):
|
||
|
def __init__(self, old_name, parent=None):
|
||
|
super().__init__(parent)
|
||
|
self.old_name = old_name
|
||
|
self.init_ui()
|
||
|
|
||
|
def init_ui(self):
|
||
|
layout = QVBoxLayout(self)
|
||
|
self.lineEdit = QLineEdit(self.old_name)
|
||
|
layout.addWidget(self.lineEdit)
|
||
|
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
||
|
self.buttonBox.accepted.connect(self.accept)
|
||
|
self.buttonBox.rejected.connect(self.reject)
|
||
|
layout.addWidget(self.buttonBox)
|
||
|
|
||
|
def get_new_name(self):
|
||
|
return self.lineEdit.text()
|
||
|
|
||
|
class MainWindow(QWidget):
|
||
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.init_ui()
|
||
|
|
||
|
def init_ui(self):
|
||
|
layout = QVBoxLayout(self)
|
||
|
self.pushButton = QPushButton('Click or Double Click to Rename')
|
||
|
self.pushButton.setFixedSize(200, 50)
|
||
|
self.pushButton.clicked.connect(self.rename_button)
|
||
|
layout.addWidget(self.pushButton)
|
||
|
self.setLayout(layout)
|
||
|
|
||
|
def rename_button(self):
|
||
|
clicked_button = self.sender()
|
||
|
if clicked_button:
|
||
|
dialog = RenameDialog(clicked_button.text(), self)
|
||
|
if dialog.exec_():
|
||
|
new_name = dialog.get_new_name()
|
||
|
if new_name: # 确保新名称不为空
|
||
|
clicked_button.setText(new_name)
|
||
|
else:
|
||
|
QMessageBox.warning(self, "Rename Error", "The new name cannot be empty.")
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app = QApplication([])
|
||
|
window = MainWindow()
|
||
|
window.show()
|
||
|
app.exec_()
|