简体   繁体   English

"如何通过单击 QPushButton 在 QLineEdit 中输入输入,然后在 QLabel 中打印结果?"

[英]How to get input entered in QLineEdit by clicking QPushButton and then print result in QLabel?

first of all, I want to completely explain what I'm going to do exactly.首先,我想完全解释我将要做什么。 I have a simple script and I want to convert it to an executable file or whatever you call it but first I should start with interface stuff.我有一个简单的脚本,我想将它转换为可执行文件或任何你称之为的文件,但首先我应该从接口的东西开始。

here is the simple python script:这是简单的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)

I figured out the solution myself.我自己想出了解决方案。 I was totally wrong with signal implication when you click a QPushButton<\/code> !当您单击QPushButton<\/code>时,我对信号含义完全错误! and I figured out that a QLabel<\/code> can be updated using .setText()<\/code> !我发现可以使用.setText()<\/code>更新QLabel<\/code> ! so here is the solution:所以这是解决方案:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击 QPushButton 打印 QTableWidget - How can I print a QTableWidget by clicking on QPushButton 使用QLineEdit,QPushButton捕获文本并使用QLabel pypt5显示该文本 - Capturing Text with QLineEdit, QPushButton and displaying that text with QLabel pypt5 如何在“ QSettings”中保存/更改“ QLineEdit”的文本并进行“ QPushButton的调用” - How to save/change text of 'QLineEdit' in 'QSettings' and get on 'QPushButton's' call 当在字符串中按下 QpushButton 时,如何在 QlineEdit 中获取文本? - How to get text in QlineEdit when QpushButton is pressed in a string? 如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色 - How to get the background color of a button or label (QPushButton, QLabel) in PyQt 如何打印QLineEdit的输入/存储在变量中? - How to print input of QLineEdit/ store in variable? 通过单击按钮,选中复选框时,打印在QLineEdit中输入的一些文本PyQt4 - By clicking a button, print some texts entered in QLineEdit when a checkbox is checked PyQt4 如何存储 QLineEdit 的字符串并能够使用 QPushButton 将存储的字符串添加到 QLineEdit? - How to store a string of QLineEdit and to be able to add the stored string to the QLineEdit with a QPushButton? 如何将QLineEdit和QPushButton移动到下一行 - how to move QLineEdit and QPushButton to next line 如何将 QLineEdit() 和 QPushButton 带到登录 window 的中心? - How to bring QLineEdit() & QPushButton to centre of Login window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM