重新加载QTextEdit文本

时间:2019-04-08 18:39:11

标签: python python-3.x pyqt pyqt5

我有一个示例代码,我想通过按查找和替换按钮在 QTextEdit 中重新加载文本。 已经从 dictionary 中加载了 QTextEdit 文本,我想用 dictionary新密钥更改 dictionary旧密钥之一并在更改字典旧键后在 QTextEdit 中显示新键!

我的意思是没有显示 QTextEdit 的新详细信息,而旧信息仍然保留。

我是怎么做到的?

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QMainWindow):
    my_dict = {'key1': 'value1', 'key2': 'value2'}
    def __init__(self):
        super(Window, self).__init__()
        V = QtWidgets.QApplication.desktop().screenGeometry()
        h, w, x, y = V.height(), V.width(), 1000, 600
        self.setGeometry(h/4, w/20, x, y)
        self.setFixedSize(x, y)
        self.setWindowTitle('Main Window')
        centralWidget = QtWidgets.QWidget()
        self.setCentralWidget(centralWidget)
        self.grid = QtWidgets.QGridLayout(centralWidget)
        self.home()

    def home(self):
        self.tools_in_home()
        self.show()

    def tools_in_home(self):
        global find_entry
        global replace_entry
        global textBox
        self.groupBox0 = QtWidgets.QGroupBox('Test') 
        self.groupBox0.setFixedSize(400, 150)
        hBoxLayout = QtWidgets.QHBoxLayout()
        textBox = QtWidgets.QTextEdit()
        textBox.resize(400, 200)
        textBox.setReadOnly(True)
        list_keys = list(Window.my_dict.keys())
        list_values = list(Window.my_dict.values())
        if len(list_keys) == 0:
            textBox.append('--EMPTY--')
        else:
            for num, dict_key in enumerate(list_keys):
                res = '{:50s} | {:50s}'.format(dict_key, list_values[num])
                textBox.append(res)

        hBoxLayout.addWidget(textBox)
        self.groupBox0.setLayout(hBoxLayout)

        find_entry = QtWidgets.QLineEdit('Enter the value you want to find...')
        find_entry.setFixedSize(380,25)

        replace_entry = QtWidgets.QLineEdit('Enter the value you want to replace...')
        replace_entry.setFixedSize(380,25)

        replace_btn = QtWidgets.QPushButton('Find and Replace')
        replace_btn.setFixedSize(125, 25)
        replace_btn.clicked.connect(self.replace_in_dict)

        self.grid.addWidget(self.groupBox0, 0, 0)
        self.grid.addWidget(find_entry, 1, 0)
        self.grid.addWidget(replace_entry, 2, 0)
        self.grid.addWidget(replace_btn, 3, 0)

    def replace_in_dict(self):
        findwt = find_entry.text()
        ans_of_findwt = Window.my_dict.get(findwt, 'Not Exist')
        if ans_of_findwt == 'Not Exist':
            QtWidgets.QMessageBox.warning(self, ans_of_findwt, 'There is no result', QtWidgets.QMessageBox.Ok)

        else:
            old_key, old_value = findwt, Window.my_dict.pop(findwt)
            new_key = replace_entry.text()
            Window.my_dict.update({new_key: old_value})
            QtWidgets.QMessageBox.information(self, 'Success','[{0}] has replaced successfully with [{1}]\n\nResult:\n\t{1} : {2}'.format(old_key, new_key, old_value), QtWidgets.QMessageBox.Ok)

def run():
    app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

if __name__ == '__main__':
    run()

1 个答案:

答案 0 :(得分:0)

您的代码具有以下错误:

  • 您假设当您更改字典时,文本将被重新加载,但不会重新加载,因此您必须实现一个替换文本的函数。
  • 假设您要保留订单,则必须使用OrderedDict,因为默认字典不会保留订单。
  • 要验证词典中是否有键,请不要使用get,因为默认情况下您使用的单词(“不存在”)可能属于词典,因此最好使用in

考虑到上述情况,解决方案是:

import sys
from collections import OrderedDict
from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        groupBox = QtWidgets.QGroupBox("Test")
        self._textBox = QtWidgets.QTextEdit(readOnly=True)
        hBoxLayout = QtWidgets.QHBoxLayout()
        hBoxLayout.addWidget(self._textBox)
        groupBox.setLayout(hBoxLayout)

        self._find_entry = QtWidgets.QLineEdit(
            placeholderText="Enter the value you want to find..."
        )
        self._replace_entry = QtWidgets.QLineEdit(
            placeholderText="Enter the value you want to replace..."
        )
        replace_btn = QtWidgets.QPushButton(
            text="Find and Replace", clicked=self.on_clicked
        )

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(groupBox)
        lay.addWidget(self._find_entry)
        lay.addWidget(self._replace_entry)
        lay.addWidget(replace_btn)
        self.resize(640, 480)

        self._my_dict = OrderedDict({"key1": "value1", "key2": "value2"})
        self.load_dictionary(self._my_dict)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        old_key = self._find_entry.text()
        new_key = self._replace_entry.text()
        self.replace_key(old_key, new_key)

    def load_dictionary(self, d):
        self._textBox.clear()
        for key, value in d.items():
            res = "{:50s} | {:50s}".format(key, value)
            self._textBox.append(res)

    def replace_key(self, old_key, new_key):
        # https://stackoverflow.com/a/1602964/6622587
        if old_key in self._my_dict:
            # https://stackoverflow.com/a/12150917/6622587
            self._my_dict = OrderedDict(
                [
                    (new_key, v) if k == old_key else (k, v)
                    for k, v in self._my_dict.items()
                ]
            )
            self.load_dictionary(self._my_dict)
        else:
            QtWidgets.QMessageBox.warning(
                self,
                "Not Exist",
                "There is no result",
                QtWidgets.QMessageBox.Ok,
            )


def run():
    app = QtWidgets.QApplication(sys.argv)
    gui = MainWindow()
    gui.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    run()