Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10263 > unrolled thread
| Started by | Saul Spatz <saul.spatz@gmail.com> |
|---|---|
| First post | 2011-07-25 05:12 -0700 |
| Last post | 2011-07-25 17:54 -0400 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.python
Re: Validating Entry in tkinter Saul Spatz <saul.spatz@gmail.com> - 2011-07-25 05:12 -0700
Re: Validating Entry in tkinter Peter Otten <__peter__@web.de> - 2011-07-25 14:31 +0200
Re: Validating Entry in tkinter Terry Reedy <tjreedy@udel.edu> - 2011-07-25 13:24 -0400
Re: Validating Entry in tkinter Peter Otten <__peter__@web.de> - 2011-07-25 21:08 +0200
Re: Validating Entry in tkinter rantingrick <rantingrick@gmail.com> - 2011-07-25 12:55 -0700
Re: Validating Entry in tkinter python@bdurham.com - 2011-07-25 16:26 -0400
Re: Validating Entry in tkinter Peter Otten <__peter__@web.de> - 2011-07-25 23:03 +0200
Re: Validating Entry in tkinter "Malcolm Greene" <mgreene@bdurham.com> - 2011-07-25 17:54 -0400
| From | Saul Spatz <saul.spatz@gmail.com> |
|---|---|
| Date | 2011-07-25 05:12 -0700 |
| Subject | Re: Validating Entry in tkinter |
| Message-ID | <745ebc09-7233-4718-8a01-d49d2075c4d9@glegroupsg2000goo.googlegroups.com> |
That doesn't work, I'm being stupid, The user might type anywhere in the string, not just at the end. I need return all([c in '1234567890abcdefABCDEF ' for c in after])
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-07-25 14:31 +0200 |
| Message-ID | <j0jnmc$r8j$1@solani.org> |
| In reply to | #10263 |
Saul Spatz wrote:
> That doesn't work, I'm being stupid, The user might type anywhere in the
> string, not just at the end. I need
>
> return all([c in '1234567890abcdefABCDEF ' for c in after])
Ah, you found out already. Here's what I've come up with in the meantime.
I also changed the command to a tuple as in the example pointed out by
Wolfgang.
import tkinter as tk
def is_valid_fromhex(s):
for pad in "", "0":
try:
bytes.fromhex(s + pad)
except ValueError:
pass
else:
return True
return False
def validate(before, after):
print(before, "-->", after)
return is_valid_fromhex(after)
if __name__ == "__main__":
root = tk.Tk()
cmd = (root.register(validate), "%s", "%P")
entry = tk.Entry(root, validate="all", validatecommand=cmd)
entry.pack()
entry.focus_set()
root.mainloop()
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-07-25 13:24 -0400 |
| Message-ID | <mailman.1465.1311614684.1164.python-list@python.org> |
| In reply to | #10265 |
On 7/25/2011 8:31 AM, Peter Otten wrote: > Saul Spatz wrote: > >> That doesn't work, I'm being stupid, The user might type anywhere in the >> string, not just at the end. I need >> >> return all([c in '1234567890abcdefABCDEF ' for c in after]) If one wants to validate keystrokes, rather than the entire field after the fact, is it possible to set an onkey handler, that will pass on valid keys? -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-07-25 21:08 +0200 |
| Message-ID | <mailman.1469.1311620897.1164.python-list@python.org> |
| In reply to | #10265 |
Terry Reedy wrote:
> On 7/25/2011 8:31 AM, Peter Otten wrote:
>> Saul Spatz wrote:
>>
>>> That doesn't work, I'm being stupid, The user might type anywhere in
>>> the
>>> string, not just at the end. I need
>>>
>>> return all([c in '1234567890abcdefABCDEF ' for c in after])
>
> If one wants to validate keystrokes, rather than the entire field after
> the fact,
It's not really after the fact as the user will not see the new contents
unless they are accepted by the validatecommand handler.
> is it possible to set an onkey handler, that will pass on
> valid keys?
With validatecommand you can have tkinter provide the string that is being
inserted:
import tkinter as tk
MESSAGE = ("about to insert {text!r} at position {index} "
"({resolution})")
def validate(action, index, text):
if action == "1":
accept = text.isdigit()
print(
MESSAGE.format(
resolution="OK" if accept else "rejected",
text=text,
index=index))
return accept
return True
if __name__ == "__main__":
root = tk.Tk()
cmd = (root.register(validate), "%d", "%i", "%S")
entry = tk.Entry(root, validate="all", validatecommand=cmd)
entry.pack()
entry.focus_set()
root.mainloop()
The available format codes are listed at
http://www.tcl.tk/man/tcl8.4/TkCmd/entry.htm#M16
If you need something more specific you'd probably have to bind the
<KeyPress> event to a custom handler:
import tkinter as tk
def keypress(event):
print(event.char)
if event.char:
if not event.char.isdigit() and event.char != "\b":
return "break"
else:
print("Don't know what to do with key #", event.keycode)
if __name__ == "__main__":
root = tk.Tk()
entry = tk.Entry(root)
entry.bind("<KeyPress>", keypress)
entry.pack()
entry.focus_set()
root.mainloop()
[toc] | [prev] | [next] | [standalone]
| From | rantingrick <rantingrick@gmail.com> |
|---|---|
| Date | 2011-07-25 12:55 -0700 |
| Message-ID | <29783711-00c3-4936-89ad-cdeb52b22b50@q15g2000yqk.googlegroups.com> |
| In reply to | #10298 |
On Jul 25, 2:08 pm, Peter Otten <__pete...@web.de> wrote: > Terry Reedy wrote: > > On 7/25/2011 8:31 AM, Peter Otten wrote: > >> Saul Spatz wrote: > > is it possible to set an onkey handler, that will pass on > > valid keys? > > With validatecommand you can have tkinter provide the string that is being > inserted: Yes but it's messy and requires knowledge of Tcl! We to keep our code bases as Pythonic as possible. > If you need something more specific you'd probably have to bind the > <KeyPress> event to a custom handler: Exactly! If you compare the code of the two approaches you'll see that the python approach is more readable and does not export any "magic" behind the scenes. By binding the KeyPress event and handling it in some derived class people who are not familiar with Tcl\Tk can read the code (and change it to suit their needs). The next best alternative would be to extend Tk.Entry with a pythonic wrapper for this functionality. MORAL: Sometimes you are forced to export these things but in this case i would argue that readability counts; so keep it in Python!
[toc] | [prev] | [next] | [standalone]
| From | python@bdurham.com |
|---|---|
| Date | 2011-07-25 16:26 -0400 |
| Message-ID | <mailman.1473.1311625597.1164.python-list@python.org> |
| In reply to | #10265 |
Peter, How would your examples work with text being inserted or deleted via the clipboard? Is there anything special that would have to happen for changes to a widget's value as the result of one of these events? Thank you, Malcolm (not the OP)
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-07-25 23:03 +0200 |
| Message-ID | <mailman.1475.1311627820.1164.python-list@python.org> |
| In reply to | #10265 |
python@bdurham.com wrote: > How would your examples work with text being inserted or deleted via the > clipboard? > > Is there anything special that would have to happen for changes to a > widget's value as the result of one of these events? I think it doesn't matter whether you type in text, or insert it with Ctrl+V or the middle mouse button. The validatecommand handler is always triggered. I suspect achieving the same effect with Button/KeyPress handlers would require significantly more work.
[toc] | [prev] | [next] | [standalone]
| From | "Malcolm Greene" <mgreene@bdurham.com> |
|---|---|
| Date | 2011-07-25 17:54 -0400 |
| Message-ID | <mailman.1479.1311630852.1164.python-list@python.org> |
| In reply to | #10265 |
Peter, > I think it doesn't matter whether you type in text, or insert it with Ctrl+V or the middle mouse button. The validatecommand handler is always triggered. I suspect achieving the same effect with Button/KeyPress handlers would require significantly more work. Thank you! Malcolm
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web