从QLineEdit和QTextEdit获取输入并转换为字符串

时间:2015-04-28 09:51:01

标签: python python-2.7 pyside

我正在尝试使用Pyside创建一个简单的接口,它将接受并将文本写入csv文件。

下面的代码不会产生错误信息,但它只会写出像#03; PySide.QtGui.QLineEdit对象这样的内容,位于0x03A534B8"到csv文件。我一直在试图弄清楚如何将这些设置为字符串但是卡住了(我对python和pyside的经验很少)。我究竟做错了什么?

import sys
from PySide import QtGui, QtCore
import csv

class Form(QtGui.QWidget):
    def __init__(self):
        super(Form, self).__init__()
        self.initUI()

    def initUI(self):

        global itemText
        global descText

        item = QtGui.QLabel('Item')
        itemEdit = QtGui.QLineEdit()
        itemText = str(itemEdit)
        desc = QtGui.QLabel('Description (optional)')
        descEdit = QtGui.QTextEdit()
        descText = str(descEdit)
        add = QtGui.QPushButton("Add item")

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(item, 1, 0)
        grid.addWidget(itemEdit, 1, 1)

        grid.addWidget(desc, 2, 0)
        grid.addWidget(descEdit, 2, 1, 3, 1)

        grid.addWidget(add, 6, 1)

        add.clicked.connect(self.writeFile)

        self.setLayout(grid)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle("Add to list")
        self.show()

    def writeFile(self):
        csvfile = open('list.csv', 'ab')
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow([itemText, descText])
        print itemText

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Form()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

尝试更改此行

descText = str(descEdit)

为此:

descText = str(descEdit.text())