I have a sample code and I want to reload the text inside the QTextEdit by pushing a Find and Replace button.
QTextEdit text is already loaded from the dictionary and I want to change one of dictionary old key with dictionary new key and show new key in QTextEdit after changing the dictionary old key!
my means is new details of QTextEdit not shown and old information stays.
how did I do it?
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()