-1

我在优秀的 Tkdocs 站点上使用的示例之一是关于复选框,我想修改它以显示哪些复选框被选中(在标签中)。

我定义了一个函数,然后重新做了它,认为第二个更清晰,因此更 Pythonic。

但是我相信还有更好的方法......

如果不明显 onevar twovar 和 threevar 是复选框,则 outvar 是我在标签中显示的变量...

欢迎评论!

    def checkvars(*args):
        if onevar.get():
            if twovar.get():
                if threevar.get():
                    outvar.set('All three are true')
                else:
                    outvar.set('one and two are set to true')
            elif threevar.get():
                outvar.set('one and three are set to true')
            else:
                outvar.set('one is set to true')
        elif twovar.get():
            if threevar.get():
                outvar.set('two and three are set to true')
            else:
                outvar.set('two is set to true')
        elif threevar.get():
            outvar.set('three is set to true')
        else:
            outvar.set('They are all false')

    def checkvars2(*args):
        if onevar.get() and twovar.get() and threevar.get():
            outvar.set('All three are true')
        elif onevar.get() and twovar.get():
            outvar.set('one and two are set to true')
        elif onevar.get() and threevar.get():
            outvar.set('one and three are set to true')
        elif onevar.get():
            outvar.set('one is set to true')
        elif twovar.get() and threevar.get():
            outvar.set('two and three are set to true')
        elif twovar.get():
            outvar.set('two is set to true')
        elif threevar.get():
            outvar.set('three is set to true')
        else:
            outvar.set('They are all false')
4

2 回答 2

1

怎么样?

def checkvars(*args):
    numbers = ['one', 'two', 'three']
    flags = [x.get() for x in (onevar, twovar, threevar)]
    numbers = filter(flags, numbers)
    if len(numbers) == 0:
        outvar.set('They are all false')
    elif len(numbers) == len(numbers):
        outvar.set('All three are true')
    else:
        is_are = {1 : 'is'}.get(l, 'are')
        comma_list = ''.join(('%s, ' % x for x in numbers[:-2]))
        and_list = ' and '.join(numbers[-2:])
        outvar.set(%s%s %s set to true' % (comma_list, and_list, is_are))

将最后一个 else 更改为 ', ' 当有 3 个或更多数字时分隔

于 2012-12-07T11:10:58.800 回答
1

俄佐立答案的小变化以确保完整性:

def checkvars(*args):
    flags = [x.get() for x in (onevar, twovar, threevar)]

    # Generate a list containing the corresponding string representation of
    # each checked flag value.
    # For example: (True, False, True) gives ('one', 'three')
    num_strings = ('one', 'two', 'three')
    val_strings = [s for f, s in zip(flags, num_strings) if f]

    # Number of checked values correspond to the number of strings.
    checked_count = len(val_strings)
    if checked_count == 0:
        outvar.set('They are all false')
    elif checked_count == len(flags):
        outvar.set('All three are true')
    else:
        verb = 'is' if len(val_strings) == 1 else 'are'
        outvar.set('%s %s set to true' % (' and '.join(val_strings), verb))

不管是什么语言,一个有这么多if/的函数elif是很少被接受的。

于 2012-12-07T13:11:10.400 回答