[英]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.