Issue
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:
- being able to enter 4 correct words
- getting input by a
QLineEdit
- enter input by clicking on a
QPushButton
- if the length of the word is 5, then print it, else: wait for next input
- 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?
Solution
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_())
Answered By - Shayan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.