Best practice for TextInput validation in kivy/kv - python

I'm designing a GUI for android with kivy. There are many TextInput-fields, most of them need some kind of validation. What I've found so far is, that on_text_validate would allow for plausibility checks, but only if the user leaves that field with ENTER, which is rarely the case in an android-environment. How can I achieve checks if the user sets the focus to another widget by tapping?
Doing this (allways) with on_text seems a bit weird to me, but maybe it's possible. A simple example: how would you make sure that the user is only able to insert integers and that the value is in a definite range?
The other question I would like to ask: how is it possible to deactivate a TextInput depending on the value of previous inputs or to set the focus appropriately?
I think these are standard tasks in GUI-programming and I'm a bit confused on how to manage these in kivy - help would be very appreciated!

Related

How to change the input language when an entry gets focused in Gtk Python

I want to change the input language when an entry gets focused (or dialog is shown) in Gtk Python. In my program, a dialog appears many times and the user must enter some texts. In systems which allow different keyboard layout for each window, the user must change the input language each time the dialog is appearing. I am programming on ubuntu ...
My first approach was be to look at Gtk+ input methods.
The Gtk.Entry has the property im_module, which can be used to set the Gtk.IMContext.
There also two blogpost (1, 2) and an extensive Stackoverflow Answer about Gtk+ input methods that may help you.
My second approach was to use xkb.
You could try something like
subprocess.run('sudo setxkbmap -layout ' + entry_lang_str)
And then I remembered that you could also use gsettings.
gsettings set /org/gnome/desktop/input-sources/ sources [('xkb', entr_lang_str)] (also via subprocess.run())
Using gsettings seems to be the easiest way for me.

How to allow only one checkbox checked at a time?

I am making a GUI for a script in Python, using PySide Qt. I have a couple of checkboxes in the main window and I was wondering if it is possible to make them dependant to each other. What I mean is that, if I check one of them, all the others should become unchecked. So only one can be checked at a time.
Is there a comfortable way to do this? Otherwise I would just write a function to uncheck the others.
use QButtonGroup to make them as a group and you might want to derive a class from this and override the basic check/uncheck depending on how you want the checkboxes to behave

Dynamically indenting and highlighting words in tkinter textbox

I recently made a simple notepad-like text editor but now I want to implement things like syntax highlighting certain words and automatic indentation as you type. How could I do this dynamically as the user types. So far everything I've done is event-based so I'm guessing I need to have some sort of loop that constantly checks the contents of the textbox as the user is typing? Is tkinter not suited for this? Appreciate it if you steer me in the right direction as to how I can implement this.
Tkinter is quite well suited to this sort of thing. It's possible to make a very smart text editor if you're willing to put in some effort.
This answer shows how to get the text widget to fire an event whenever something in the text widget changes. It's a little complicated, but fairly foolproof.
If you want something simpler, you can simply bind on <Any-KeyRelease> which will fire an event whenever the user releases a key. You can then use the information in the event object to decide what to do. It won't handle the case where you cut and paste with the mouse, for example, and your binding will fire for arrow keys and other non-inserting keys, which is why I recommend the more complicated solution.
This answer shows an example of using a binding on <space> to do do a simple spellcheck, and also shows a fairly simplistic implementation of a toolbar with a "bold" button.

How to react on actions (events) in real time in Tkinter?

I´d like to create a program which will react on actions by user in real time. For example there will be three Labels. And when user clicks on one, I want to recolor the border to a different color and the user should be able to "type" a (single) number in this Label. I know about the Entry widget, but Labels are suitable for the whole application.
Thank you for any answers
Your question is too vague to answer precisely, but to specifically address each individual point:
Yes, it's possible to "react .. in real time" -- whenever an event is detected it will be acted upon as soon as possible.
Yes, it's possible to color the border of a widget when an event is detected
Yes, it's possible to type into a label. Though, obviously, the behavior is unusual and may not be what you expect.
I suspect none of those help you solve your real problem, but I have no idea what you're actually trying to accomplish.

Python widget/cursor detection?

Beginner python learner here. I have a question that I have tried to Google but I just can't come up with the proper way to ask in just a few words (partly because I don't know the right terminology.)
How do I get python to detect other widgets? For example, if I wanted a script to check and see when I click my mouse if that click put focus on an entry widget on a (for example) website. I've been trying to get it to work in Tkinter and I can't figure out even where to begin.
I've seen this:
focus_displayof(self)
Return the widget which has currently the focus on the
display where this widget is located.
But the return value for that function seems to be some ambiguous long number I can't decipher, plus it only works in its own application.
Any direction would be much appreciated. :)
Do you mean inside your own GUI code, or some other application's/website's?
Sounds like you're looking for a GUI driver, or GUI test/automation driver. There are tons of these, some great, some awful, many abandoned. If you tell us more about what you want that will help narrow down the choices.
Is this for testing, or automation, or are you going to drive the mouse and button yourself and just want something to observe what is going on under the hood in the GUI?
>How do I get Python to detect other widgets?
On a machine, or in a browser? If in a machine, which platform: Linux/Windows (which)/Mac?
If in a browser, which browser (and major version)?
> But the return value for that function seems to be some ambiguous long number I can't decipher
Using longs as resource handles is par for the course, although good GUI drivers also work with string/regex matching on window and button names.
> plus it only works in its own application.
What do you mean, and what are you expecting it to return you? You should be able to look up that GUI object and access its title. Look for a GUI driver that works with window and button names.
Here is one list, read it through and see what sounds useful. I have used AutoIt under Win32, it's great, widely-used and actively-maintained; it can be called from Python (via subprocess).
Here are comparisons by the author of PyWinAuto on his and similar tools. Give a read to his criticisms of its structure from 2010. If none of these is what you want, at least you now have the vocabulary to tell us what would be...

Categories

Resources