简体   繁体   中英

Python how to get QLineEdit Text?

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!

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

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

If you want to connect it to QTextEdit You can connect it with .textChanged signal which is emited from QTextEdit everytime text changes.

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

QTextEdit.textChanged.connect(your_method_to_put_text_somewhere_else)

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