如何通过将文本小部件传递到函数中,在pyqt5中动态更改标签文本?

huangapple go评论65阅读模式
英文:

How to dynamically change label texts in pyqt5 by passing a text widget into a function?

问题

I want to dynamically edit the content of a QLineEdit text, by passing the Widget to a function that initiates a file dialog. The text of the passed widget shall then be set to the selected file. Obviously I do not want to create a function for each QLineEdit Widget.

This is the current state (which is obviously not working).

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class UserInterface(QWidget):

    def __init__(self, *args, **kwargs):
        super(UserInterface, self).__init__(*args, **kwargs)
        self.layout = QFormLayout()

        self.button_test_1 = QPushButton("Select File")
        self.line_test_1 = QLineEdit("Select File")
        self.button_test_1.clicked.connect(lambda: self.select_file(self.line_test_1))      
        self.layout.addRow(self.button_test_1, self.line_test_1)

        self.button_test_2 = QPushButton("Select File")
        self.line_test_2 = QLineEdit("Select File")
        self.button_test_2.clicked.connect(lambda: self.select_file(self.line_test_2))      
        self.layout.addRow(self.button_test_2, self.line_test_2)

        self.setLayout(self.layout)

    def select_file(self, line):
        fileName = QFileDialog.getOpenFileName(self, "Choose File", directory="D:\\", filter="*.json *.csv")
        line.setText(fileName)

def main():
    app  = QApplication(sys.argv)

    controller = UserInterface()
    controller.show()

    sys.exit(app.exec_())

main()

Currently, it results in an error as "self.line" does not exist.

In the end, I want to be able to pass self.line_test_[n] (or any other name) to my select_file function and set the text of the passed widget within the function.

英文:

I want to dynamically edit the content of a QLineEdit text, by passing the Widget to a function that initiates a filedialoge. The text of the passed widget shall then be set to the selected file.
Obviously I do not want to create a function for each QLineEdit Widget.

This is the current state (which is obviously not working).

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class UserInterface(QWidget):

    def __init__(self, *args, **kwargs):
        super(UserInterface, self).__init__(*args, **kwargs)
        self.layout = QFormLayout()

        self.button_test_1 = QPushButton("Select File")
        self.line_test_1 = QLineEdit("Select File")
        self.button_test_1.clicked.connect(lambda: self.select_file(self.line_test_1))      
        self.layout.addRow(self.button_test_1,self.line_test_1)

        self.button_test_2 = QPushButton("Select File")
        self.line_test_2 = QLineEdit("Select File")
        self.button_test_2.clicked.connect(lambda: self.select_file(self.line_test_2))      
        self.layout.addRow(self.button_test_2,self.line_test_2)

        self.setLayout(self.layout)

    def select_file(self,line):
        fileName = QFileDialog.getOpenFileName(self,"Choose File",directory="D:\\",filter="*.json *.csv")
        self.line.setText(fileName)

def main():
    app  = QApplication(sys.argv)

    controller = UserInterface()
    controller.show()

    sys.exit(app.exec_())

main()

Currently it results in an error als "self.line" does not exist.

In the end I want to be able to pass self.line_test_[n](or any other name) to my select_file function and set the text of the passed Wigdet within the function.

答案1

得分: -1

You just need to remove self keyword for it to work:

    def select_file(self, line):
        fileName = QFileDialog.getOpenFileName(self, "Choose File", directory="D:\\", filter="*.json *.csv")[0] # Make sure to use [0] here to get the file name.
        line.setText(fileName) # Remove self here

However, the setText will break right away since the value of fileName is not string but a tuple. Therefore you need to put [0] after "*.json *.csv".

英文:

You just need to remove self keyword for it to work:

    def select_file(self, line):
        fileName = QFileDialog.getOpenFileName(self, ,"Choose File",directory="D:\\",filter="*.json *.csv")[0] # Make sure to use [0] here to get the file name.
        line.setText(fileName) # Remove self here

However, the setText will break right away since the value of fileName is not string but a tuple. Therefore you need to put [0] after ..."*.json *.csv")

huangapple
  • 本文由 发表于 2023年5月10日 17:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定