【问题标题】:How to get input entered in QLineEdit by clicking QPushButton and then print result in QLabel?如何通过单击 QPushButton 在 QLineEdit 中输入输入,然后在 QLabel 中打印结果?
【发布时间】:2022-02-06 06:40:27
【问题描述】:

首先,我想完全解释我将要做什么。我有一个简单的脚本,我想将它转换为可执行文件或任何你称之为的文件,但首先我应该从接口的东西开始。

这是简单的python脚本:

for i in range(4):
    a = False
    while not a:
        text = input("enter a word")
        
        # a condition for accepting input. I mean input should satisfy this condition
        if len(text) == 5:
            a = True
    
    # just print the word, simple and easy
    print(text)

我想把这个过程放在 PyQt5 中,以创建一个极其简单的用户输入面。但我该怎么做呢?我想添加一个QLineEdit 以获得输入!输入是一个类似字符串的文本!我想通过点击QPushButton触发并输入单词!虽然输入不满足条件(len(text) == 5),但我应该能够在QLineEdit 中输入另一个输入并使用QPushButton 输入,直到输入满足条件!那么程序应该在print(text)QLabel(我认为QLabel 可以,因为我可以调整它的尺寸和大小......)。重要的是我应该能够在程序中编写和输入4个正确的单词(满足条件的单词)(因为我写了for i in range(4):)。所以这是我所期望的一步一步:

  1. 能够输入 4 个正确的单词
  2. 通过QLineEdit 获取输入
  3. 点击QPushButton输入输入
  4. 如果单词长度为5,则打印,否则:等待下一个输入
  5. 为 4 个有效输入执行此过程

我对pyqt5不太熟悉,但我已经尽力了。这是我为此目的而写的:

from PyQt5.QtWidgets import QApplication , QWidget , QPushButton , QLabel , QLineEdit
import sys

app = QApplication(sys.argv)

class APP(QWidget):
    def __init__(self):
        super().__init__()

        self.button = QPushButton("Enter" , self)
        self.button.setGeometry(500 , 400 , 100 , 50)
        self.button.clicked.connect(self.clicked)
        self.Line_edit()

        self.label = QLabel("place of ouput prints" , self)
        self.label.move(40 , 60)
        self.label.setGeometry(50 , 50 , 150 , 50)

    def Line_edit(self):
        self.input_text = QLineEdit(self)
        self.input_text.setPlaceholderText("Enter The Word")
    
    def clicked(self):
        # if QLineEdit contains a word
        if self.input_text.text():
            for i in range(4):
                a = False
                while not a:
                    text = self.input_text.text()

                    # a condition for accepting input. I mean input should satisfy this condition
                    if len(text) == 5:
                        a = True

                # just print the word in QLable, simple and easy
                self.label = QLabel(text , self)
                # then it should be waiting for new input untill i == 3 (since i wrote for i in range(4))


window = APP()
window.show()
sys.exit(app.exec_()) 

当我尝试运行此代码时,它甚至没有反应,也没有任何反应。我应该如何修改代码以获得我应该得到的?

【问题讨论】:

  • while 循环是错误的方法。您应该在 line-edit 上set a validator,并连接到 return-pressed/editing-finished 信号以在用户添加另一个输入时接收通知。保留输入计数,并在达到所需计数时更新标签。
  • @ekhumoro 感谢您的帮助!我没有使用验证器就做到了! (see here)。但是你能举例说明如何使用validator()吗?我检查了提到的链接,但那里没有任何示例
  • 正如我在评论中所说,您的代码的真正问题是 while 循环。我已经写了几个关于 SO 的验证器示例(例如here)。如果您搜索,您会发现许多其他人。

标签: python pyqt pyqt5


【解决方案1】:

我自己想出了解决方案。当您单击QPushButton 时,我对信号含义完全错误!我发现QLabel 可以使用.setText() 更新!所以这里是解决方案:

from PyQt5.QtWidgets import QApplication , QWidget , QPushButton , QLabel , QLineEdit
import sys

app = QApplication(sys.argv)

class APP(QWidget):
    def __init__(self):
        super().__init__()

        self.button = QPushButton("Enter" , self)
        self.button.setGeometry(500 , 400 , 100 , 50)
        self.button.clicked.connect(self.clicked)
        self._submit_counter = 0
        
        self.button2 = QPushButton("RESET" , self)
        self.button2.setGeometry(500 , 600 , 100 , 50)
        self.button2.clicked.connect(self.reset_clicked)
        
        self.Line_edit()

        self.label = QLabel("place of ouput prints" , self)
        self.label.move(40 , 60)
        self.label.setGeometry(50 , 50 , 150 , 50)

    def Line_edit(self):
        self.input_text = QLineEdit(self)
        self.input_text.setPlaceholderText("Enter The Word")

    def reset_clicked(self):
        self._submit_counter = 0
    
    def clicked(self):

        if self._submit_counter<4:

            # if QLineEdit contains a word
            if self.input_text.text():
                
                text = self.input_text.text()

                # a condition for accepting input. I mean input should satisfy this condition
                if len(text) == 5:
                    
                    self._submit_counter += 1
                    # just print the word in QLable, simple and easy
                    self.label.setText(text)


window = APP()
window.show()
sys.exit(app.exec_()) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2018-04-25
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2023-02-23
    相关资源
    最近更新 更多