I created the GUI using QTDesigner and save the file as .ui extension. Then convert the file to .py file using the following code
pyuic4 -x test.ui -o test.py
Now I want to integrate my project code to this test.py file. Since I am new to pyqt4, I dont know how to read text from text edit and save to file and viceversa. Following is my code.
from PyQt4 import QtCore, QtGui
from final_ar_gui import *
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_AnaphoraResolution(object):
def setupUi(self, AnaphoraResolution):
AnaphoraResolution.setObjectName(_fromUtf8("AnaphoraResolution"))
AnaphoraResolution.resize(608, 620)
self.textEdit = QtGui.QTextEdit(AnaphoraResolution)
self.textEdit.setGeometry(QtCore.QRect(0, 110, 271, 341))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.textEdit_2 = QtGui.QTextEdit(AnaphoraResolution)
self.textEdit_2.setGeometry(QtCore.QRect(310, 110, 271, 341))
self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
self.pushButton = QtGui.QPushButton(AnaphoraResolution)
self.pushButton.setGeometry(QtCore.QRect(40, 470, 91, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(AnaphoraResolution)
self.pushButton_2.setGeometry(QtCore.QRect(170, 470, 171, 27))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(AnaphoraResolution)
self.pushButton_3.setGeometry(QtCore.QRect(370, 470, 81, 27))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_4 = QtGui.QPushButton(AnaphoraResolution)
self.pushButton_4.setGeometry(QtCore.QRect(480, 470, 98, 27))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.label = QtGui.QLabel(AnaphoraResolution)
self.label.setGeometry(QtCore.QRect(180, 30, 241, 20))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(AnaphoraResolution)
self.connectActions()
QtCore.QMetaObject.connectSlotsByName(AnaphoraResolution)
def retranslateUi(self, AnaphoraResolution):
AnaphoraResolution.setWindowTitle(QtGui.QApplication.translate("AnaphoraResolution", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("AnaphoraResolution", "Enter", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("AnaphoraResolution", "Pronominal Resolution", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_3.setText(QtGui.QApplication.translate("AnaphoraResolution", "Clear", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_4.setText(QtGui.QApplication.translate("AnaphoraResolution", "Quit", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("AnaphoraResolution", "Anaphora Resolution in Malayalam", None, QtGui.QApplication.UnicodeUTF8))
def connectActions(self):
self.pushButton.clicked.connect(self.ent)
def ent(self):
%Dont know what code to write
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
AnaphoraResolution = QtGui.QDialog()
ui = Ui_AnaphoraResolution()
ui.setupUi(AnaphoraResolution)
AnaphoraResolution.show()
sys.exit(app.exec_())
When i click enter (push button) the input in the textEdit is read and save into a file named input.txt. When i click quit (pushbutton) the output in the outfile.txt is read and write into textEdit_2 . How to solve this ?
QTextEdit does not have any text() method, if you want to get the text you must use toPlainText() , if you want to clean the text it is better to use clear() since it makes it more readable.
The QPlainTextEdit class provides a widget that is used to edit and display plain text.
If all you need is the displayed text in your QTextEdit widget, you can access that by using the toPlainText()
method on the widget you need the text from.
Example:
mytext = self.textEdit.toPlainText()
At this point, you can do whatever you want with mytext
. You can write it to a file, you can manipulated it, etc.
If you need to (re)populate your QTextEdit
with the modified value of mytext
, you can do that by using setPlainText
self.textEdit.setPlainText(mytext)
To write the string to a file, you'll do something like this:
with open('somefile.txt', 'a') as f: f.write(mytext)
This will append mytext
to somefile.txt
Along with the answer posted above. Don't modify the actual .py that is converted when you use pyuic4. You want to incorporate a class to handle the setup so that you can modify the UI without erasing everything that you have written. Example in pyqt5
from MyConvertedUiFile import Ui_SomeName class MyWorkingCode(QtWidgets.QMainWindow, Ui_SomeName): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.pushButton.clicked.connect(self.ent) def ent(self): mytext = self.textEdit.toPlainText() with open('somefile.txt', 'a') as f: f.write(mytext)
This should be how you utilize the converted Ui files it will make it so you can re edit the file and you wont loose all of the work you have put into the Ui control structure. It will also allow you to use the same Ui for multiple purposes as you are using it as a template and changing what the buttons do in a different file. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With