PDA

View Full Version : While loop problem in pyqt



neverlander
16th December 2007, 16:11
I am new here, so I hope it is not a wrong place to ask for pyqt.

I wrote a while loop in pyqt but I want it to stop to take input from user.I do this in python with raw_input but it doesn't work in pyqt.How can I do this in pyqt.



cev = str(ui.lineEdit.text()).lower()

guess = 0
f = 0

while cev != answer:
guess += 1

ui.label_6.setText(QtGui.QApplication.translate("MainWindow", "Wrong answer!", None, QtGui.QApplication.UnicodeUTF8))

f += 1

ui.label_4.setText(answer[0:f])

if answer[0:f] == answer[0:]:
ui.label_4.setText(answer[0:f])
ui.label_6.setText(QtGui.QApplication.translate("MainWindow"," Answer is revealed", None, QtGui.QApplication.UnicodeUTF8))

ui.label_9.setText(QtGui.QApplication.translate("MainWindow", soz[sor1], None, QtGui.QApplication.UnicodeUTF8))
liste.pop(s)
return self.sor()
HERE ?
"cev = raw_input("..................:")"

I need a code here to take input from user like "raw_input"

jacek
16th December 2007, 16:25
Where should that input come from? Do you want to retrieve the text that user has entered into some widget?

In general it is a bad idea to have long loop in event-based application.

neverlander
16th December 2007, 16:51
It is not a long loop.I revised the codes above.I ask a word to the user.If he makes a mistake the first letter of the answer reveals.But here because ı couldnot take another input from user the loop continues and reveals all the letters.I want it to reveal the letters one by one after the user enters his answer.

I take the input from user by line edit.

jacek
16th December 2007, 17:16
I take the input from user by line edit.
In that case you have to connect to the QLineEdit::textChanged() or some other QLineEdit signal and get rid of that loop.

neverlander
16th December 2007, 18:06
I am not so good at pyqt.Can you give me an example on how to use it?

jacek
17th December 2007, 11:25
See this: http://zetcode.com/tutorials/pyqt4/widgets. It shows the typical way of doing things in Qt. In the Checkbox example you have a changeTitle() slot that is invoked every time the user clicks the check box. You don't need any loops to read data from the user -- the data will be delivered to you.

If you make a loop in your application, the event loop inside Qt won't run and Qt won't be able to redraw widgets and receive input from the user. So basically your application should only react to signals and events, instead of enforcing the program flow. Of course you can have loops in your code, but they should be short or the application will become unresponsive*.

The whole tutorial is here: http://zetcode.com/tutorials/pyqt4/


* You can use QCoreApplication::processEvents() to avoid this, but you should do that only as the last resort.

neverlander
17th December 2007, 20:03
Thank you very much...