python加载文件并显示文件内容到qtextedit上_python - PyQt4:使QTextEdit上的某些文本不可编辑 - 堆栈内存溢出...

这篇博客介绍了如何通过自定义的TextEdit类在Python的PyQt4应用中加载并显示文件内容,同时使QTextEdit部分文本不可编辑。示例代码展示了如何捕获用户输入,执行命令并显示输出,以及限制用户编辑特定文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的解决方案是创建一个从QTextEdit继承并覆盖并添加必要属性的类,如下所示:

class TextEdit(QTextEdit):

def __init__(self, *args, **kwargs):

QTextEdit.__init__(self, *args, **kwargs)

self.staticText = os.getcwd()

self.counter = 1

self.setReadOnly(True)

def append(self, text):

n_text = "{text} [{number}] > ".format(text=self.staticText, number=self.counter)

self.counter += 1

QTextEdit.append(self, n_text+text)

完整的代码:

import os

import sys

from PyQt4.QtCore import *

from PyQt4.QtGui import *

class TextEdit(QTextEdit):

def __init__(self, *args, **kwargs):

QTextEdit.__init__(self, *args, **kwargs)

self.staticText = os.getcwd()

self.counter = 1

self.setReadOnly(True)

def append(self, text):

n_text = "{text} [{number}] > ".format(text=self.staticText, number=self.counter)

self.counter += 1

QTextEdit.append(self, n_text+text)

class MyWindow(QWidget):

def __init__(self, *args, **kwargs):

QWidget.__init__(self, *args, **kwargs)

label = QLabel(self.tr("Enter command and press Return"), self)

self.le = QLineEdit(self)

self.te = TextEdit(self)

# layout

layout = QVBoxLayout(self)

layout.addWidget(label)

layout.addWidget(self.le)

layout.addWidget(self.te)

self.setLayout(layout)

self.connect(self.le, SIGNAL("returnPressed(void)"), self.display)

# self.le.returnPressed.connect(self.display)

def display(self):

command = str(self.le.text())

resp = str(os.popen(command).read())

self.te.append(resp)

self.le.clear()

def main():

app = QApplication(sys.argv)

w = MyWindow()

w.show()

sys.exit(app.exec_())

if __name__ == "__main__":

main()

要模拟QtConsole,我们必须覆盖QTextEdit的某些方法,捕获一些事件,并验证它不会消除前缀,如下所示:

import os

import sys

from PyQt4.QtCore import *

from PyQt4.QtGui import *

class TextEdit(QTextEdit):

def __init__(self, *args, **kwargs):

QTextEdit.__init__(self, *args, **kwargs)

self.staticText = os.getcwd()

self.counter = 1

self.prefix = ""

self.callPrefix()

self.setContextMenuPolicy(Qt.CustomContextMenu)

self.customContextMenuRequested.connect(self.onCustomContextMenuRequest)

def onCustomContextMenuRequest(self, point):

menu = self.createStandardContextMenu()

for action in menu.actions():

if "Delete" in action.text():

action.triggered.disconnect()

menu.removeAction(action)

elif "Cu&t" in action.text():

action.triggered.disconnect()

menu.removeAction(action)

elif "Paste" in action.text():

action.triggered.disconnect()

act = menu.exec_(point)

if act:

if "Paste" in act.text():

self.customPaste()

def customPaste(self):

self.moveCursor(QTextCursor.End)

self.insertPlainText(QApplication.clipboard().text())

self.moveCursor(QTextCursor.End)

def clearCurrentLine(self):

cs = self.textCursor()

cs.movePosition(QTextCursor.StartOfLine)

cs.movePosition(QTextCursor.EndOfLine)

cs.select(QTextCursor.LineUnderCursor)

text = cs.removeSelectedText()

def isPrefix(self, text):

return self.prefix == text

def getCurrentLine(self):

cs = self.textCursor()

cs.movePosition(QTextCursor.StartOfLine)

cs.movePosition(QTextCursor.EndOfLine)

cs.select(QTextCursor.LineUnderCursor)

text = cs.selectedText()

return text

def keyPressEvent(self, event):

if event.key() == Qt.Key_Return:

command = self.getCurrentLine()[len(self.prefix):]

self.execute(command)

self.callPrefix()

return

elif event.key() == Qt.Key_Backspace:

if self.prefix == self.getCurrentLine():

return

elif event.matches(QKeySequence.Delete):

return

if event.matches(QKeySequence.Paste):

self.customPaste()

return

elif self.textCursor().hasSelection():

t = self.toPlainText()

self.textCursor().clearSelection()

QTextEdit.keyPressEvent(self, event)

self.setPlainText(t)

self.moveCursor(QTextCursor.End)

return

QTextEdit.keyPressEvent(self, event)

def callPrefix(self):

self.prefix = "{text} [{number}] >".format(text=self.staticText, number=self.counter)

self.counter += 1

self.append(self.prefix)

def execute(self, command):

resp = os.popen(command).read()

self.append(resp)

class MyWindow(QWidget):

def __init__(self, *args, **kwargs):

QWidget.__init__(self, *args, **kwargs)

label = QLabel(self.tr("Enter command and press Return"), self)

self.te = TextEdit(self)

# layout

layout = QVBoxLayout(self)

layout.addWidget(label)

layout.addWidget(self.te)

self.setLayout(layout)

def main():

app = QApplication(sys.argv)

w = MyWindow()

w.show()

sys.exit(app.exec_())

if __name__ == "__main__":

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值