简体   繁体   English

按下按钮时从QLineEdit进行Pyside打印文本

[英]Pyside print text from QLineEdit when button pressed

I'm trying to make it so that when I click the button, the text input field is printed to the console. 我试图做到这一点,以便当我单击按钮时,文本输入字段被打印到控制台。 Why do I keep getting an error? 为什么我总是出错? I'm not quite sure what I'm doing wrong. 我不太确定自己在做什么错。

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

You need to make etProjectName an attribute of your Example class: 您需要将etProjectNameExample类的属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pyside:使用按钮将文本从qlineEdit复制到标签和字符串var - Pyside: Use button to copy text from qlineEdit to a label and to a string var QLineEdit中的PySide文本不可编辑 - PySide text in QLineEdit not editable Pyside - 当 QLineEdit 获得焦点时选择所有文本 - Pyside - Select all text when QLineEdit gets focus PySide6 应用程序中 function 重复和全局变量的问题,按下按钮时打印日期和月份的 function 重复 - Problem with function duplication and global variables in PySide6 app, a function to print day and month is duplicated when button pressed 当在字符串中按下 QpushButton 时,如何在 QlineEdit 中获取文本? - How to get text in QlineEdit when QpushButton is pressed in a string? 将QLineEdit文本转换为浮点-PySide / pyQt4 - Converting QLineEdit text to float - PySide/pyQt4 按下选项卡时防止特定的QLineEdit聚焦 - Prevent a specific QLineEdit from focusing when tab is pressed Pyside从QLineEdit检索用户输入 - Pyside retrieving a user input from QLineEdit QlineEdit :: text()不返回单击按钮且该特定QlineEdit的对象名称为lineEdit时输入的文本 - QlineEdit::text() not returning the text entered when push button is clicked and the object name of that particular QlineEdit is lineEdit 来自 QLineEdit 的文本不显示 - Text from QLineEdit not displaying
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM