2017-09-09 6 views
-1

"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() 
+2

コードを表示するとすべてが役に立ちます。 [MCVE]をお読みください。 – Lafexlos

+0

** GO **ボタンのコールバック関数では、リストボックス項目を取得できますか? – wwii

+0

何を使用していますか?私が 'command = lambda group = w.get(w.curselection()[0]):go(group))'と言ったように、インデックスエラーが発生します。 – 420fedoras

答えて

0

あなたDataVisualiserがクラスであるので、代わりに、クラスの他の機能のメソッドを作成する方がよいでしょう。 globalキーワードを使用しないように、変数(またはその一部)もクラスインスタンス変数にする必要があります。

Iはselfキーワード(self.group)を用いgroup可変インスタンス変数を作り、その可変いつでもリストボックスの選択の変更を更新する方法、selectを作成。また、関数generate_histogramをクラスメソッドにしました。全体的なプログラムの目標に基づいて、他の変数インスタンス変数のいくつかを作成する必要があるかもしれません。

下記の更新されたコードは、その他の変更/追加の一部について説明するコメントがあります。使用しているバージョンPythonに応じて、import文を変更する必要があります。

from Tkinter import * 

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 
     self.group = hist_dd.get(hist_dd.curselection()[0]) #make group a class variable 

     # don't use default keyword parameters i.e, group= ..... 
     hist_gen = Button(master, text="Go!", 
          command=lambda: self.generate_histogram(self.group)) 
     # still need to fully implement this 
     hist_gen.grid(row=6, column=0, sticky=W+E) 
     hist_dd.bind('<<ListboxSelect>>', self.select) # bind selecction event to listbox 


    #method to change the 'group' variable anytime a listbox selection changes 
    def select(self, event): 
     widget = event.widget 
     selection=widget.curselection() 
     self.group = widget.get(selection[0]) 


    #class method 
    def generate_histogram(self, group): 
     print(group) 
     return None 

root = Tk() 
dv = DataVisualiser(root) 
root.mainloop() 
関連する問題