简体   繁体   中英

Taking input from QLineEdit and QTextEdit and converting to string

I am trying to make a simple interface using Pyside that will accept and write text to a csv file.

The code below doesn't produce an error message but it will only write things like "PySide.QtGui.QLineEdit object at 0x03A534B8" to the csv file. I have been trying to work out how to set these as strings but am stuck (I have minimal experience with python and pyside). What am I doing wrong?

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

Try changing this line

descText = str(descEdit)

for this:

descText = str(descEdit.text())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM