PyQt5 QTextEdit按钮功能实现:删除空行与文本前后缀添加
Hey there, I've put together a complete working example that implements both of your requested features in the btn_func function. Let's dive in:
Full Code Implementation
import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit, QPushButton, QVBoxLayout, QWidget, QRadioButton, QHBoxLayout) class TextEditorWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("Text Editor Tool") self.setGeometry(100, 100, 600, 400) # Central widget and main layout central_widget = QWidget() self.setCentralWidget(central_widget) main_layout = QVBoxLayout(central_widget) # Text edit area self.text_edit = QTextEdit() main_layout.addWidget(self.text_edit) # Radio buttons for prefix/suffix choice radio_layout = QHBoxLayout() self.radio_prefix = QRadioButton("Add prefix '12'") self.radio_suffix = QRadioButton("Add suffix '12'") # Default to suffix option self.radio_suffix.setChecked(True) radio_layout.addWidget(self.radio_prefix) radio_layout.addWidget(self.radio_suffix) main_layout.addLayout(radio_layout) # Process button self.process_btn = QPushButton("Process Text") self.process_btn.clicked.connect(self.btn_func) main_layout.addWidget(self.process_btn) def btn_func(self): # Get current text from QTextEdit original_text = self.text_edit.toPlainText() if not original_text: return # Step 1: Remove all empty lines (including lines with only whitespace) lines = original_text.split('\n') filtered_lines = [line for line in lines if line.strip() != ''] # Step 2: Add prefix or suffix '12' to each line processed_lines = [] for line in filtered_lines: if self.radio_prefix.isChecked(): processed_line = f"12{line}" else: processed_line = f"{line}12" processed_lines.append(processed_line) # Combine lines back into a single string and set back to QTextEdit final_text = '\n'.join(processed_lines) self.text_edit.setPlainText(final_text) if __name__ == "__main__": app = QApplication(sys.argv) window = TextEditorWindow() window.show() sys.exit(app.exec_())
How It Works
1. Removing Empty Lines
- First, we pull the plain text from the QTextEdit using
toPlainText() - Split the text into individual lines with
split('\n') - Use a list comprehension to filter out lines that are empty (or only contain spaces/tabs) — checking
line.strip() != ''covers both truly blank lines and lines with invisible whitespace
2. Adding Prefix/Suffix '12'
- I added two radio buttons to let the user pick between prefix or suffix (you can hardcode this to one option if you don't need user choice)
- Loop through the filtered lines (after removing empty ones)
- Depending on which radio button is selected, we either prepend '12' to the line or append it
- Finally, we join all processed lines back into a single string with
'\n'.join()and update the QTextEdit with the result
Quick Notes
- The function handles empty lines first, then applies the prefix/suffix — this avoids wasting time modifying lines we're just going to delete anyway
- If the QTextEdit is empty, the function exits early to skip unnecessary processing
内容的提问来源于stack exchange,提问作者Jack Parten
相关产品推荐
相关产品推荐

