Python Forum
Upload file data on Qwidget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upload file data on Qwidget
#1
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
Reply
#2
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()
maiya likes this post
Reply
#3
(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
Reply
#4
(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.
Reply
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Seeking guidance on PyQt Instantiation and correct way to use an instance of QWidget BrewBarred 3 540 Jan-23-2024, 07:21 AM
Last Post: deanhystad
Video [PyQt] Get a executable file (.exe) from a .py file add a promoted class in a QWidget MiguelonReyes 0 663 Oct-17-2023, 11:43 PM
Last Post: MiguelonReyes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020