str = self.search_window.lineEdit.text() str2 = self.search_window.lineEdit_2.text() cursor = self.textEdit.textCursor() cursor.select(QTextCursor.Document) format = QTextCharFormat() format.setBackground(QColor("white")) cursor.mergeCharFormat(format) self.textEdit.setReadOnly(False) self.textEdit.toPlainText().replace(str, str2) 替换不了
时间: 2024-02-23 14:58:15 浏览: 64
你可以尝试修改代码如下:
```
str = self.search_window.lineEdit.text()
str2 = self.search_window.lineEdit_2.text()
cursor = self.textEdit.textCursor()
cursor.select(QTextCursor.Document)
format = QTextCharFormat()
format.setBackground(QColor("white"))
cursor.mergeCharFormat(format)
self.textEdit.setReadOnly(False)
text = self.textEdit.toPlainText()
new_text = text.replace(str, str2)
self.textEdit.setPlainText(new_text)
```
原因是`toPlainText()`方法返回的是一个字符串的副本,而不是原始字符串。因此,你需要将修改后的字符串重新设置回文本编辑器中,才能实现替换的效果。
相关问题
def set_view_trunc(self, new_value): xr=[-5, 5] yr=[-5, 5] zr=[-5, 5] xr[0]=self.ui.horizontalSlider_x0.value()/10000 xr[1]=self.ui.horizontalSlider_x1.value()/10000 yr[0]=self.ui.horizontalSlider_y0.value()/10000 yr[1]=self.ui.horizontalSlider_y1.value()/10000 zr[0]=self.ui.horizontalSlider_z0.value()/10000 zr[1]=self.ui.horizontalSlider_z1.value()/10000 self.ui.lineEdit_horizontalSlider_x0.setText(str(xr[0])) self.ui.lineEdit_horizontalSlider_x1.setText(str(xr[1])) self.ui.lineEdit_horizontalSlider_y0.setText(str(yr[0])) self.ui.lineEdit_horizontalSlider_y1.setText(str(yr[1])) self.ui.lineEdit_horizontalSlider_z0.setText(str(zr[0])) self.ui.lineEdit_horizontalSlider_z1.setText(str(zr[1])) self.Truncate(self.vtk_widget.polydata,self.vtk_widget.polydata_show,xr,yr,zr) self.vtk_widget.SetPointsScalars(self.vtk_widget.polydata_show) self.vtk_widget.render_window.Render()
这段代码是一个函数,用来设置三维可视化界面的截断范围。具体来说,它从界面上的水平滑块控件获取三个方向的截断范围,然后更新界面上的文本框控件来显示当前的截断范围。接着,它调用名为 `Truncate` 的函数去截断三维场景中的数据,把截断后的结果保存在 `vtk_widget.polydata_show` 中。最后,它调用 `SetPointsScalars` 和 `render_window.Render` 函数来更新显示截断后的场景。
import sys import threading import time from PyQt5.QtWidgets import * from PyQt5 import uic import pandas as pd import random # import pyqtgraph as pg import matplotlib.pyplot as plt from PyQt5.QtWidgets import QGroupBox from PyQt5 import QtWidgets from login_4 import Ui_CK from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas # df = pd.read_excel('shu.xlsx') class MyWindow(QWidget and QMainWindow,Ui_CK): def __init__(self): super().__init__() self.init_ui() groupbox = QGroupBox('Title',self) # self.plot = pg.PlotWidget(enableAutoRange=True) # self.ui.verticalLayout.addWidget(self.plot) # self.curve = self.plot.plot() #self.ui = uic.loadUi("./login_4.ui") def init_ui(self): print('1.1') try: self.ui = uic.loadUi("./login_4.ui") #print(threading.current_thread()) #print(self.ui.__dict__) # print(self.ui.label) # print(self.ui.label.text()) # 查看ui文件中有哪些控件 # 提取要操作的控件 self.user_name_qwidget = self.ui.lineEdit # 单位输入框 self.password_qwidget = self.ui.lineEdit_2 # 二级单位输入框 self.zhicheng_qwidget = self.ui.lineEdit_3 # 职称输入框 self.jiaoyuan_qwidget = self.ui.lineEdit_4 # 教员输入框 self.login_btn = self.ui.pushButton # 登录抽课按钮 self.textBrowser = self.ui.textBrowser # 授课对象显示区域 # 绑定信号与槽函数 self.textBrowser_2 = self.ui.textBrowser_2 # 文本显示区域课程名称 self.textBrowser_3 = self.ui.textBrowser_3 # 文本显示区域课次 self.textBrowser_4 = self.ui.textBrowser_4 # 文本显示区域教研室 self.login_btn.clicked.connect(self.login) self.login_btna = self.ui.pushButton_2 self.login_btna.clicked.connect(lambda: self.plot_q()) except Exception as e: print(e.__class__.__name__, e) def login(self): print('1.2') """登录按钮的槽函数""" #print(self.user_name_qwidget.text()) a = self.user_name_qwidget.text() e = sel 为什么会报错
这是一段Python代码,通过使用import关键字导入了sys、threading、time等模块。同时,也导入了PyQt5.QtWidgets的部分类和PyQt5.uic模块。此外,还导入了pandas和random模块,用于处理数据和生成随机数等操作。
阅读全文