Python 如何获取 QLineEdit 文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42288320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:31:06  来源:igfitidea点击:

Python how to get QLineEdit Text?

pythonpyqt

提问by Tyrell

hello world i am trying to get a QLineEdit to work as a user Input witch they are suppose to type in a song name. after the song name is entered i am wanting that song to start playing after the click the play button everything is working fine other then the part where they can type in what ever song they want in that folder. the problem is im not sure on how to make the QlineEdit word and update everytime someone thing is entered into the text box here is my code hopefully someone can help me out Thanks in advance!

你好世界我正在尝试让 QLineEdit 作为用户输入女巫工作,他们假设他们输入歌曲名称。输入歌曲名称后,我希望该歌曲在单击播放按钮后开始播放,一切正常,除了他们可以在该文件夹中输入他们想要的任何歌曲的部分。问题是我不确定如何制作 QlineEdit 单词并在每次有人在文本框中输入内容时更新这里是我的代码希望有人可以帮助我提前谢谢!

import sys
import webbrowser
import random
import time
import os
import subprocess
from PyQt4.QtCore import QSize, QTimer, SIGNAL
from PyQt4.QtGui import QApplication,QScrollBar,QLineEdit , QDialog , QFormLayout ,QGraphicsRectItem , QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie 
from PyQt4 import QtGui
import vlc
#----|Imports End|----#
class UIWindow(QWidget):
    def __init__(self, parent=None):
        super(UIWindow, self).__init__(parent)

        self.resize(QSize(400, 450))

        self.Play = QPushButton('Play', self)
        self.Play.resize(100,40)
        self.Play.move(45, 100)#

        self.Pause = QPushButton('Pause', self)
        self.Pause.resize(100,40)
        self.Pause.move(260, 100)#



        self.Tbox = QLineEdit('Song name',self)
        self.Tbox.resize(400,25)
        self.Tbox.move(0,50)

        self.Play.clicked.connect(self.PlayB)
        self.Pause.clicked.connect(self.PauseB)
        self.Flask = vlc.MediaPlayer("C:\Users\Matt\Music\"+str(self.Tbox.text())+".mp3")

    def PlayB(self):
        self.Flask.play()

    def PauseB(self):
        self.Flask.stop()

class MainWindow(QMainWindow,):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(745 ,350 , 400, 450)
        self.setFixedSize(400, 450)
        self.startUIWindow()


    def startUIWindow(self):
        self.Window = UIWindow(self)
        self.setWindowTitle("HELP ME!")
        self.setCentralWidget(self.Window)
        self.show()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

回答by Erik ??astny

You can easily get text with QLineEdit.text()method. Or same way set text with QLineEdit.setText()method

您可以使用QLineEdit.text()方法轻松获取文本。或以同样的方式使用QLineEdit.setText()方法设置文本

If you want to connect it to QTextEditYou can connect it with .textChangedsignal which is emited from QTextEdit everytime text changes.

如果你想将它连接到QTextEdit你可以将它与.textChanged每次文本更改时从 QTextEdit 发出的信号连接起来。

The same way how you use .clickedsignal you can use this one as:

与使用.clicked信号的方式相同,您可以将其用作:

QTextEdit.textChanged.connect(your_method_to_put_text_somewhere_else)