当按下按钮时,Pyside从QLineEdit打印文本

时间:2014-03-12 02:09:25

标签: python button pyside qlineedit

我试图这样做,以便当我单击按钮时,文本输入字段将打印到控制台。为什么我一直收到错误?我不太确定我做错了什么。

import sys
import os
from PySide import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        # EditText Field
        labelProjectName = QtGui.QLabel('Project Name:', self)
        labelProjectName.move(15, 10)

        etProjectName = QtGui.QLineEdit('', self)
        etProjectName.resize(etProjectName.sizeHint())
        etProjectName.move(90, 7)

        # Button UI
        btn = QtGui.QPushButton('Create Folder', self)
        btn.setToolTip('This creates the folders.')
        btn.resize(btn.sizeHint())
        btn.move(5, 30)       
        btn.clicked.connect(self.generateFolders)

        self.resize(250, 150)
        self.center()

        self.setWindowTitle('Folder Utility')    
        self.show()

    def center(self):

        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def generateFolders(self):
        var = self.etProjectName.text()
        print var

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':

    main()

1 个答案:

答案 0 :(得分:1)

您需要etProjectName Example类的属性:

    self.etProjectName = QtGui.QLineEdit('', self)
    self.etProjectName.resize(self.etProjectName.sizeHint())
    self.etProjectName.move(90, 7)