-1

私が優れた Tkdocs サイトで使用している例の 1 つは、チェック ボックスについて話しているもので、どのチェック ボックスが (ラベルで) チェックされているかを示すように変更したいと考えていました。

私は関数を定義し、2 番目の関数の方が明確であり、したがってより 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):
    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 に答える
1

次のようなものはどうですか: これはあなたが持っているものよりもはるかに短くはありませんが、「vars」をもう少しだけ取得すると、これははるかにうまくスケーリングされます

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))

数が3つ以上ある場合、最後のelseを「、」区切りに変更

于 2012-12-07T11:10:58.800 に答える