Posts: 50
Threads: 23
Joined: Apr 2020
Hi All,
I am working on QWidget part where I wanted to browse a file and upload the file data on widget window (that should be editable too). It is a yaml file (consists of dictionary). Same widget should also have the 'submit' button. I have written a code where I able to browse the file and read the data, however am printing it on console, instead I wanted to display it on another widget/window, once it is displayed on the widget, it should allow to modify the data and have the submit button to click as well. when submit button clicked it should connect to remote system and execute the predefined script and result of that script should display it back on the same widget as an output as well.
This is my code, I know it is not complete yet
from PyQt5 import QtGui, QtCore, QtWidgets
import os, sys
'''
class YMLWidget(QtWidgets.QWidget):
def __init__(self) -> None:
super().__init__()
self.setWindowTitle('YML Display Widget')
self.setFixedWidth(500)
mainLayout = QtWidgets.QVBoxLayout()
'''
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600, 300, 450, 300)
self.setWindowTitle('YAML Uploader Button')
button = QtWidgets.QPushButton('upload a yaml', self)
button.resize(button.sizeHint())
button.clicked.connect(self.YamlUpload)
button.move(150, 150)
self.show()
def YamlUpload(self):
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', r'C:\Users\maiya\Desktop\compare', '*.yml')
with open(filePath, 'r', encoding='mbcs') as file_pointer:
self.lines = file_pointer.readlines()
def main():
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
app.exec_()
if __name__ == '__main__':
main() Any help on this would really appreciated, thanks a lot
Regards,
maiya
Posts: 1,034
Threads: 16
Joined: Dec 2016
unknown encoding: mbcs
You can display and edit in QPlainTextEdit
from PyQt5 import QtGui, QtCore, QtWidgets
import os, sys
class MyWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600, 300, 450, 300)
self.setWindowTitle('YAML Uploader Button')
self.tool_bar = self.addToolBar("File")
button = QtWidgets.QPushButton('upload a yaml', self)
button.resize(button.sizeHint())
button.clicked.connect(self.YamlUpload)
self.tool_bar.addWidget(button)
self.editor = QtWidgets.QPlainTextEdit()
self.setCentralWidget(self.editor)
self.show()
def YamlUpload(self):
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', r'C:\Users\maiya\Desktop\compare', '*.yml')
with open(filePath, 'r', encoding='utf-8') as file_pointer:
self.lines = file_pointer.read()
self.editor.setPlainText(self.lines)
def main():
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
app.exec_()
if __name__ == '__main__':
main()
Posts: 50
Threads: 23
Joined: Apr 2020
Jul-08-2022, 05:24 AM
(This post was last modified: Jul-08-2022, 05:24 AM by maiya.)
(Jul-07-2022, 01:19 PM)Axel_Erfurt Wrote: unknown encoding: mbcs
You can display and edit in QPlainTextEdit
from PyQt5 import QtGui, QtCore, QtWidgets
import os, sys
class MyWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600, 300, 450, 300)
self.setWindowTitle('YAML Uploader Button')
self.tool_bar = self.addToolBar("File")
button = QtWidgets.QPushButton('upload a yaml', self)
button.resize(button.sizeHint())
button.clicked.connect(self.YamlUpload)
self.tool_bar.addWidget(button)
self.editor = QtWidgets.QPlainTextEdit()
self.setCentralWidget(self.editor)
self.show()
def YamlUpload(self):
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', r'C:\Users\maiya\Desktop\compare', '*.yml')
with open(filePath, 'r', encoding='utf-8') as file_pointer:
self.lines = file_pointer.read()
self.editor.setPlainText(self.lines)
def main():
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
app.exec_()
if __name__ == '__main__':
main()
Hi Axel,
Thanks a lot for your suggestion and modified code as well.
One quick question?
self.editor.setPlainText() takes the string parameter not list type parameter, here I wanted to display the entire file data on that editable screen (plain text screen)
any suggestion on this?
Regards,
maiya
Posts: 50
Threads: 23
Joined: Apr 2020
(Jul-08-2022, 05:24 AM)maiya Wrote: (Jul-07-2022, 01:19 PM)Axel_Erfurt Wrote: unknown encoding: mbcs
You can display and edit in QPlainTextEdit
from PyQt5 import QtGui, QtCore, QtWidgets
import os, sys
class MyWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600, 300, 450, 300)
self.setWindowTitle('YAML Uploader Button')
self.tool_bar = self.addToolBar("File")
button = QtWidgets.QPushButton('upload a yaml', self)
button.resize(button.sizeHint())
button.clicked.connect(self.YamlUpload)
self.tool_bar.addWidget(button)
self.editor = QtWidgets.QPlainTextEdit()
self.setCentralWidget(self.editor)
self.show()
def YamlUpload(self):
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', r'C:\Users\maiya\Desktop\compare', '*.yml')
with open(filePath, 'r', encoding='utf-8') as file_pointer:
self.lines = file_pointer.read()
self.editor.setPlainText(self.lines)
def main():
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
app.exec_()
if __name__ == '__main__':
main()
Hi Axel,
Thanks a lot for your suggestion and modified code as well.
One quick question?
self.editor.setPlainText() takes the string parameter not list type parameter, here I wanted to display the entire file data on that editable screen (plain text screen)
any suggestion on this?
Regards,
maiya
Okay, I got it.. how to display the entire file data on screen.. Thanks a lot!.
from PyQt5 import QtGui, QtCore, QtWidgets
import os, sys
'''
class YMLWidget(QtWidgets.QWidget):
def __init__(self) -> None:
super().__init__()
self.setWindowTitle('YML Display Widget')
self.setFixedWidth(500)
mainLayout = QtWidgets.QVBoxLayout()
'''
class MyWidget(QtWidgets.QMainWindow):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600, 300, 450, 300)
self.setWindowTitle('YAML Uploader Widget')
self.tool_bar = self.addToolBar('File')
upload_button = QtWidgets.QPushButton('upload a yaml', self)
upload_button.resize(upload_button.sizeHint())
upload_button.clicked.connect(self.YamlUpload)
self.tool_bar.addWidget(upload_button)
submit_button = QtWidgets.QPushButton('submit a yaml', self)
submit_button.resize(submit_button.sizeHint())
#submit_button.clicked.connect(self.YamlUpload)
self.tool_bar.addWidget(submit_button)
self.editor = QtWidgets.QPlainTextEdit()
self.setCentralWidget(self.editor)
#button.move(150, 150)
self.show()
def YamlUpload(self):
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', r'C:\Users\maiyapr\Desktop\compare', '*.yml')
with open(filePath, 'r', encoding='utf-8') as file_pointer:
self.lines = file_pointer.readlines()
self.editor.setPlainText(''.join(self.lines))
def main():
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
app.exec_()
if __name__ == '__main__':
main() Here I used join method to do the same.
Posts: 1,034
Threads: 16
Joined: Dec 2016
yml files are plain text files, there is no need to first make a list out of it with readlines() and then convert it back to a string with join()
|