Access widget by widget / window name Tk
I use validatecommand
to observe and validate the input widget "dynamically".
Standard usage validatecommand
prevents invalid characters from being entered into the observed input widget. This is not the behavior I like, so I used validatecommand
to pass the record widget string to another function and return True
anyway. floatstr_to_float
checks a string using a regular expression preg
.
If the regex matches the entered value, everything is fine, and therefore executed print('approved')
. However, if the user has entered an invalid input, the regex does not match, is executed print('not approved')
, and the corresponding input widget should be filled with red (color change has not yet been properly implemented).
What I have done so far is changing the background of the very first input widget with a help <widget>.config(bg=<background>)
to check that I can access each widget by indexing the list of all the widgets I have created.
validatecommand
can pass multiple arguments to the function being executed (e.g. input / text string and widget / window name). So getting a link to an invalid widget is not a problem at all. However, the path passed validatecommand
is not python accessible. How can I get a reference (like a unique variable name) from this pathname to change the background by executing <widget>.config(bg=<background>)
in widgets containing (in) valid input?
- MWE -
#!/usr/bin/env python3
# -*- coding: <utf-8> -*-
# code adapted from:
# http://stackoverflow.com/questions/4140437/python-tkinter-interactively-validating-entry-widget-content
import tkinter as tk
import re
class MyApp():
def __init__(self):
self.root = tk.Tk()
self.parameternames = [
('a', 'U'), ('b', 'U'), ('c', 'U'), ('d', 'U'), ('e', 'U'),
('f', 'U'), ('g', 'U'), ('h', 'U'), ('i', 'U'), ('j', 'U'),
('k', 'U'), ('l', 'U'), ('m', 'U'), ('n', 'U'), ('o', 'U'),
('p', 'U'), ('q', 'U'), ('r', 'U'), ('s', 'U'), ('t', 'U')]
self.vcmd = (self.root.register(self.OnValidate), '%P', '%W')
self.create_widgets()
def create_widgets(self):
self.entries = []
for i in enumerate(self.parameternames):
entry = tk.Entry(self.root, validate="all", validatecommand=self.vcmd)
self.default_bg = entry.cget("bg")
entry.pack()
self.entries.append(entry)
self.root.mainloop()
def OnValidate(self, P, W):
# %P = value of the entry if the edit is allowed
# %W = the tk name of the widget (pathname)
print("OnValidate:")
print("P='%s'" % P )
print("W='%s'" % W )
self.floatstr_to_float(P, W)
# return True to display inserted character, validation is done by a re in 'floatstr_to_float()'
return True
def floatstr_to_float(self, fs, W):
preg = re.compile('^\s*(?P<int>\d*)\s*[\.,]?\s*(?P<dec>\d*)\s*$')
m = preg.match(fs)
if m:
print('approved')
intprt=m.group('int')
frcprt=m.group('dec')
f = 0. if (intprt == '' and frcprt == '') else float('%s.%s' %(intprt, frcprt)) # not needed yet
# currently: just changing the color of the first entry widget (proof of concept)
# aim: pass unique name of edited entry widget to self.change_bg() for changing bg of
# appropriate entry widget
self.change_bg(self.entries[0], 1)
else:
print('not approved')
# see comment in if-statement above
self.change_bg(self.entries[0], 0)
def change_bg(self, name, approved):
if approved == 1:
name.config(bg=self.default_bg)
else:
name.config(bg='#d9534f')
app=MyApp()
source to share