开发者

How to read data from file and display in QEditText box in QT

开发者 https://www.devze.com 2022-12-12 21:25 出处:网络
i would like to read a line ofdata from text file 开发者_StackOverflow社区and display that data in Text Edit boxIt\'s quite simple, actually:

i would like to read a line of data from text file 开发者_StackOverflow社区and display that data in Text Edit box


It's quite simple, actually:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


FILENAME = 'textedit_example.py'


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.edit = QTextEdit()
        layout = QVBoxLayout()
        layout.addWidget(self.edit)
        self.setLayout(layout)

        self.edit.setText("No file found")

        with open(FILENAME) as f:
            self.edit.setText(f.readline())


app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

Some notes:

  1. Save it as 'textedit_example.py' and run. You'll see the first line of the source in the text box (import sys)
  2. It requires Python 2.6 and latest PyQt4 to run


You may interest to look at this tutorial

Simple Text Viewer Example

0

精彩评论

暂无评论...
验证码 换一张
取 消