2009-11-24 73 views
0

閱讀QEditText箱從文件和顯示數據我想讀取數據從文本文件中的一行,並顯示在文本編輯框的數據如何在QT

回答

2

這很簡單,實際上是:

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_() 

一些注意事項:

  1. 將其另存爲'textedit_example.py'並運行。你會看到源的第一行的文本框中(import sys
  2. 它需要Python 2.6和最新的PyQt4的運行