"Go"ボタンが押されるまでコードが実行されないように、ボタンのコマンドでリストボックスから選択した値を使用します。リストボックスからの値を使用してボタンプレスの関数を呼び出すTkinter
これまでのところ、リストボックスは要素の選択時に<にバインドすることでコードを実行することが分かりました。これは私には馬鹿に思えます。確かに、プログラムは、ユーザーが望むものを正確に選択していることを確認するために、2段階の選択が必要です(接線、残念)。
これまで、.curselection()[0]と.getを使用して現在の選択肢の変数を作成しようとしましたが、インデックスのエラーが表示されます。それから私が最初の選択を設定した場合、私はそれを変更することはできませんし、常にその選択に基づいて実行されます。
確かにこれは簡単に行うべきであり、私は何かを欠いている。
[EDIT]示唆したように、コードのダンプを追加:
class DataVisualiser:
def __init__(self, master):
master.minsize(width=600, height=400)
frame = Frame(master)
frame.grid()
# Histogram Generator
hist_options = ["Cat vs. Dog", "Cat Breed", "Dog Breed", "Cat Name", "Dog Name", "Cat Location",
"Dog Location", "Cat Registration", "Dog Registration"]
hist_dd = Listbox(master, bg="#cfcfcf", fg="black", font="Helvetica", height=5, selectmode=SINGLE)
for o in hist_options:
hist_dd.insert(0, o)
hist_dd.grid(row=0, column=0)
hist_dd.selection_set(first=0)
hist_scroll = Scrollbar(master, orient=VERTICAL)
hist_dd["yscrollcommand"] = hist_scroll.set
hist_scroll["command"] = hist_dd.yview
hist_scroll.grid(row=0, column=1, rowspan=7, sticky=N+S)
# scrollbar from: https://www.youtube.com/watch?v=xNLdB0jY1Rg
# HERE: I want to pass the value from the listbox to another function via this button
hist_gen = Button(master, text="Go!",
command=lambda group=hist_dd.get(hist_dd.curselection()[0]): generate_histogram(group))
# still need to fully implement this
hist_gen.grid(row=6, column=0, sticky=W+E)
def generate_histogram(group):
print(group)
return None
root = Tk()
dv = DataVisualiser(root)
root.mainloop()
コードを表示するとすべてが役に立ちます。 [MCVE]をお読みください。 – Lafexlos
** GO **ボタンのコールバック関数では、リストボックス項目を取得できますか? – wwii
何を使用していますか?私が 'command = lambda group = w.get(w.curselection()[0]):go(group))'と言ったように、インデックスエラーが発生します。 – 420fedoras