10

これは非常に単純なはずですが、私はそれを正しくするのに本当に苦労しています。必要なのは、選択の変更時に変数を更新する単純な ttk ComboBox だけです。

以下の例ではvalue_of_combo、新しい選択が行われるたびに変数の値を自動的に更新する必要があります。

from Tkinter import *
import ttk

class App:

    value_of_combo = 'X'


    def __init__(self, parent):
        self.parent = parent
        self.combo()

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value)
        self.box['values'] = ('X', 'Y', 'Z')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()
4

2 に答える 2

4

より一般的なケースとして、変数が更新されたときに変数の値を取得する必要がある場合は、変数に組み込まれているトレース機能を使用することをお勧めします。

var = StringVar()  # create a var object

# define the callback
def tracer(name, idontknow, mode):
    # I cannot find the arguments sent to the callback documented
    # anywhere, or how to really use them.  I simply ignore
    # the arguments, and use the invocation of the callback
    # as the only api to tracing
    print var.get()

var.trace('w', tracer)
# 'w' in this case, is the 'mode', one of 'r'
# for reading and 'w' for writing

var.set('Foo')  # manually update the var...

# 'Foo' is printed
于 2014-10-08T01:54:10.060 に答える