簡體   English   中英

"如何通過單擊 QPushButton 在 QLineEdit 中輸入輸入,然后在 QLabel 中打印結果?"

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

首先,我想完全解釋我將要做什么。 我有一個簡單的腳本,我想將它轉換為可執行文件或任何你稱之為的文件,但首先我應該從接口的東西開始。

這是簡單的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)

我自己想出了解決方案。 當您單擊QPushButton<\/code>時,我對信號含義完全錯誤! 我發現可以使用.setText()<\/code>更新QLabel<\/code> ! 所以這是解決方案:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM