0

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:

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 want to put this process in PyQt5 to create an extremely simple user enter face. but how should I do that? I want to add a QLineEdit to get input! input is a string-like text! and I want to trigger and enter the word by clicking QPushButton! and while the input doesn't satisfy the condition (len(text) == 5) I should be able to type another input in QLineEdit and enter it using QPushButton till the input satisfies the condition! then the program should print(text) in a QLabel(i think QLabel is okay since I can adjust its dimension and size...). and the important thing is I should be able to write and enter 4 correct words(words that satisfy the condition) in the program (since I wrote for i in range(4):). so here is what I expect step by step:

  1. being able to enter 4 correct words
  2. getting input by a QLineEdit
  3. enter input by clicking on a QPushButton
  4. if the length of the word is 5, then print it, else: wait for next input
  5. do this process for 4 valid inputs

I'm not much familiar with pyqt5, but I'm doing my best. here is what I wrote for this purpose:

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

when I try to run this code, it doesn't even react and nothing happens. how should I modify the code to get what I am supposed to?

Shayan
  • 5,165
  • 4
  • 16
  • 45
  • A while-loop is the wrong approach. You should [set a validator](https://doc.qt.io/qt-5/qlineedit.html#setValidator) on the line-edit, and connect to the return-pressed/editing-finished signal to receive notification when the user has added another input. Keep a count of the inputs, and update the label when the required count is reached. – ekhumoro Feb 05 '22 at 21:08
  • @ekhumoro Thanks for your help! I did it without using a validator! ([see here](https://stackoverflow.com/a/71002162/11747148)). but can you please make an example of how to use `validator()`? I checked mentioned link but there wasn't any example there – Shayan Feb 05 '22 at 21:16
  • 1
    As I said in my comment, the real problem with your code was the while-loop. I have already written several validator examples on SO (e.g. [here](https://stackoverflow.com/q/58278508/984421)). If you search you will find many others. – ekhumoro Feb 06 '22 at 02:02

1 Answers1

0

I figured out the solution by myself. I was totally wrong with signal implication when you click a QPushButton! and I figured out that a QLabel can be updated using .setText()! 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_()) 
Shayan
  • 5,165
  • 4
  • 16
  • 45