通过 Tk 模块的部件/窗口路径名称访问小部件

4
我正在使用validatecommand来观察和验证输入的entry小部件的内容。 validatecommand的标准用法会防止无效字符输入到观察的entry小部件中。这不是我想要的行为,所以我使用validatecommand将entry小部件的字符串传递给另一个函数,并在任何情况下返回True。 floatstr_to_float使用称为preg的正则表达式验证字符串。如果正则表达式匹配,则输入有效,一切正常,因此将执行print('approved')。然而,如果用户输入无效输入,则正则表达式不匹配,将执行print('not approved'),并且应该将适当的entry小部件填充为红色(颜色更改尚未正确实现)。到目前为止,我所做的是使用<widget>.config(bg=<background>)更改第一个entry小部件的背景,以检查是否能够通过索引所有创建的entry小部件的列表来访问每个小部件。

validatecommand可以向执行函数传递多个参数(例如输入/文本字符串和小部件/窗口路径名)。因此,通常情况下获取无效小部件的引用并不是问题。但是,validatecommand传递的路径名似乎无法在Python中访问。如何从此路径名获取引用(例如唯一变量名)以通过在包含(无)效输入的小部件上执行<widget>.config(bg=<background>)来更改背景?

-- MWE --

#!/usr/bin/env python3
# -*- coding: <utf-8> -*-

# code adapted from:
# https://dev59.com/oG855IYBdhLWcg3wy3oc

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()
1个回答

3
您可以使用Tknametowidget方法, 根据名字查找小部件。因此,请使用以下代码:
self.change_bg(self.root.nametowidget(W), 1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接