import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
self.user_action()
def setup_ui(self):
self.setWindowTitle("Qt桌面应用程序")
self.resize(300, 300)
self.push_button = QPushButton(self)
self.push_button.setText("按钮")
self.push_button.move(50, 10)
self.text_edit = QTextEdit(self)
self.text_edit.resize(200, 200)
self.text_edit.move(50, 50)
def user_action(self):
def push_button_clicked():
text_edit_text_cursor = self.text_edit.textCursor()
text_edit_text_cursor_text_char_format = QTextCharFormat()
text_edit_text_cursor_text_char_format.setFontFamily("幼圆")
text_edit_text_cursor_text_char_format.setFontPointSize(30)
text_edit_text_cursor.setCharFormat(text_edit_text_cursor_text_char_format)
text_edit_text_cursor.setBlockCharFormat(text_edit_text_cursor_text_char_format)
text_edit_text_cursor_text_block_format = QTextBlockFormat()
text_edit_text_cursor_text_block_format.setTextIndent(20)
text_edit_text_cursor.setBlockFormat(text_edit_text_cursor_text_block_format)
#第一个块输入时块字体格式无效,将光标移动到块开头即可
text_edit_text_cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor, 1)
self.text_edit.setTextCursor(text_edit_text_cursor)
self.text_edit.setFocus()
self.push_button.clicked.connect(push_button_clicked)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())