ラベル tkinter の投稿を表示しています。 すべての投稿を表示
ラベル tkinter の投稿を表示しています。 すべての投稿を表示

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

2020年7月21日火曜日

Python3 tkinterでドラッグアンドドロップ

Pythonのtkinterでファイルをドロップしてそれを開くようなGUIをもったスクリプトを作るのは どうするのか調べてみた。 一つは、ctypes モジュールを用いて、WindowsのAPIを呼び出す方法がある。 もう一つは、TkDNDというTkの拡張ライブラリとそのPythonラッパーであるTkinterDnD2を用いる方法である。 ここでは、後者の方法を試してみた。 環境はWindows10、Python3.8.3、64bit環境である。

まずはこちらにいく。 TkinterDnD2のダウンロードができる。また、TkDND へのダウンロードリンクもある。 行うことは、Tkの拡張ライブラリであるTkDND2.8と、そのPythonラッパーであるTkinterDnD2をインストールすることである。

TkDNDのページへ行く。 64bit環境なら、 Windows Binaries/TkDND2.8の中にある tkdnd2.8-win32-x86_64.tar.gz をダウンロードする。

Python環境がインストールされているフォルダの中にtclというフォルダがある。 それぞれの環境ごとに場所が変わるので、 sys モジュールをインポートして、どこにpythonがインストールされているかを 調べてみるとよいだろう。 そのtklフォルダ中に、tkdnd2.8フォルダをそのまま配置する。

次に、こちらから TkinterDnD2-0.3.zipファイルをダウンロードする。 展開すると、TkinterDnD2フォルダといくつかのdemoファイルおよび 説明のhtmlファイルが得られる。 Pythonがインストールされているフォルダにある、 Lib\site-packagesの中にTkinterDnD2フォルダをそのまま配置すると完了である。

python からdemoを実行してみると動作を確認できる。 簡単なスクリプトは以下のものとなる。 エクスプローラからファイルを投げ込むと、そのファイルパスが表示される。 複数ファイルを投げ入れると、各ファイルパスがスペースで区切られたものとなる。

import sys
import os
import TkinterDnD2 as tkdnd
if sys.version > "3.0":
    import tkinter as tk
else:
    import Tkinter as tk

def drop_enter(event):
    event.widget.focus_force()
    return event.action

def drop_leave(event):
    event.widget._root().focus_force()
    return event.action

def drop_position(event):
    #print(event.x_root, event.y_root)
    return event.action

def drop(event):
    if event.data:
        print(event.data)
    return event.action

app = tkdnd.TkinterDnD.Tk()

app.drop_target_register(tkdnd.DND_FILES)
app.dnd_bind('<<DropEnter>>', drop_enter)
app.dnd_bind('<<DropLeave>>', drop_leave)
app.dnd_bind('<<DropPosition>>', drop_position)
app.dnd_bind('<<Drop>>', drop)

app.mainloop()