This is the class where I'm trying to implement a simple expression evaluator:
class mainWindow(QtGui.QMainWindow, mainGui.Ui_MainWindow):
def __init__(*args, **kwargs)
super(mainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.connecting_range_displays()
def connecting_range_displays(self):
ranges = num_ranges()
first_range = num_ranges.ones_range
second_range = num_ranges.tens_range
print first_range
print second_range
self.top_lineEdit.setText(str(first_range))
self.bottom_lineEdit.setText(str(second_range))
ex_a = first_range + second_range
print "this is expressions answer", ex_a
self.answer_lineEdit.returnPressed.connect(self.ex_evaluator)
def ex_evaluator(self, right_answer):
answer = self.answer_lineEdit.text()
if ex_a == right_answer:
print "Correct answer bucko"
In the above code for a simple arithmetic practice application I use two lines to display random numbers in a certain range, and the third QlineEdit
i.e answer_lineEdit
I use it to get user input and then to check if its correct I use the Signal
returnPressed
and connect it to the ex_evaluator
which will then tell you if you have given a correct response, the problem seems to be that I cannot pass the ex_a
argument to the function to be able to evaluate it, could it be namespace issue, I'm a pretty new at coding and my philosophy is to only learn the least amount to get me to where I want so I'm wondering if I will have to learn how to subclass the QLineEdit
which I have no idea how to do, I saw that maybe decorators might help which is another bucket of trouble I don't want to get into.
You need to do one of the following:
For the second option, I wrote a tutorial that describes both ways of doing this: http://www.blog.pythonlibrary.org/2013/04/10/pyside-connecting-multiple-widgets-to-the-same-slot/
To use a lambda, do something like this:
self.answer_lineEdit.returnPressed.connect(lambda ans=ex_a: self.ex_evaluator(ans))
For the functools.partial, it would be something like this:
import functools
callback = partial(self.ex_evaluator, ex_a)
self.answer_lineEdit.returnPressed.connect(callback)
If you don't understand that last one, you might want to take a look at the documentation.