Frage

I am trying to build a GUI in wxPython. I have two TextCtrls. Let's say they are first_index_input and last_index_input, and they control model.f_i and model.l_i, respectively. There's a condition: For proper model execution, model.f_i must be <= model.l_i. Therefore the content of first_index_input should be <= last_index_input.

So what I want to do is fix it so that: 1) if the user enters a value into first_index_input that is greater than the value in last_index_input (established in a prior use case or something) then last_index_input and model.l_i will both be set equal to the new first_index_input. 2) If the user enters a last_index_input that is less than first_index_input, then last_index_input will be corrected again and set equal to first_index_input.

So what's so hard about that? It's a question of what event to key off of. Let's say that first_index_input has content "145" and I (correctly) want to give last_index_input an input of 10324. If this widget keys off of a wx.EVT_TEXT event, then it won't wait for me to type in "10324". As soon as it sees the "1" (i.e. first digit) it freaks out and says, "NO! That's less than first_index_input" and "corrects" my typing. On the other hand, I can get rid of this by keying off of a wx.EVT_TEXT_ENTER command, and everything works fine as long as I remember to hit return after entering my value and I never remember to hit return so I am sure my users won't either.

Is there another way to do this? Some other event that is available, perhaps? Thanks.

War es hilfreich?

Lösung

I would use EVT_TEXT together with a wx.Timer. The idea here is to monitor how long it's been since the user last typed something in that second control. So you'll have a start value of zero (self.start_value) and each time EVT_TEXT fires, you'll reset that value and restart the timer. When the EVT_TIMER event gets fired, it will increment the start value. You'll have to put an "if" statement in timer's event handler that checks if the start value is greater than X seconds. If it is, then do the check to see if the first input if less then the second and act accordingly. Make sure you stop the timer if it is as a running timer object can hang the app when you go to close it.

Here's a link on timers: http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top