Python如何获取QLineEdit Text?

Tyr*_*ell 3 python pyqt

你好,世界,我试图让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_())
Run Code Online (Sandbox Code Playgroud)

Eri*_*tný 7

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

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

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

QTextEdit.textChanged.connect(your_method_to_put_text_somewhere_else)
Run Code Online (Sandbox Code Playgroud)