2024年7月18日木曜日

tkinterのoptionmenuを使う

tkinterでGUIを作るとき、選択肢から選ばせるようなときにつかうウィジットで、 tkinter.OptionMenuがあります。wxPythonなら、wx.Choiceを使うところです。 普通に選択肢を用意しておいて、使うならシンプルなのですが、 選択肢をユーザーの操作に応じて変化させたいときがあります。 そのような場合のメモを残しておきます。

tkinterでは、ウィジットと結び付けて、その値を保持したり変更を監視したりできるオブジェクトがあります。文字列ならtkinter.StringVar()というものです。ここでは、それを用いてOptionMenuの値を監視します。trace()メソッドを使って、値が設定されたときにon_selected()が呼び出されるようにそておきます。

選択肢を変更するのが、OptionMenuSetChoices()という名前で作った関数です。選ばれたときの値の設定を通して、on_selected()が呼び出されます。OptionMenuの選択状態と、値を保持するオブジェクトが結びついているため、それを変更するとOptionMenuの表示も変更されます。

動作を試すのに、選択肢を100個増やすボタン、最初に移動するボタン、最後に移動するボタンを配置っしておきました。 Windows11、Python 3.8.3の環境で試しています。

import tkinter as tk

def OptionMenuSetChoices(opmenu, var, choices):
    opmenu['menu'].delete(0, "end")
    for i, choice in enumerate(choices):
        opmenu['menu'].add_command(label=choice, command=tk._setit(var, choice))

class APPFRAME(tk.Frame):
    def __init__(self, master, **kwargs):
        super(APPFRAME, self).__init__(master=master, **kwargs)

        self.var = tk.StringVar(self, "default")
        self.var.trace('w', self.on_selected)

        self.choices = [self.var.get()]
        self.opmenu = tk.OptionMenu(self, self.var, *self.choices)
        self.opmenu.pack()
        OptionMenuSetChoices(self.opmenu, self.var, self.choices)

        btn = tk.Button(self, text='Set Choices', command=self.on_set_choices)
        btn.pack()
        btn_first = tk.Button(self, text='Go First', command=self.on_go_first)
        btn_first.pack()
        btn_last = tk.Button(self, text='Go Last', command=self.on_go_last)
        btn_last.pack()

    def on_selected(self, *args):
        print(self.var.get())

    def on_set_choices(self, *args):
        self.choices = ["choice: %02d" % (i+1) for i in range(100)] 
        OptionMenuSetChoices(self.opmenu, self.var, self.choices)
        self.on_go_first()

    def on_go_first(self):
        self.var.set(self.choices[0])
    
    def on_go_last(self):
        self.var.set(self.choices[-1])

def main():
    root = tk.Tk()
    app = APPFRAME(root)
    app.pack()
    root.mainloop()

if __name__ == '__main__':
    main()

0 件のコメント: