2017-09-09 14 views
-1

我想用一個按鈕的命令從我的列表框中選擇的值,所以沒有代碼執行,直到「轉到」按鈕被按下值按下按鈕的功能。呼叫使用從列表框中的Tkinter

到目前爲止我只看到一個列表框通過結合<>執行上的元件的選擇的代碼。這對我來說似乎是愚蠢的,當然,程序應該有兩種選擇,以確保用戶正確選擇他們想要的東西(切線,抱歉)。

到目前爲止我試圖創建一個變量用於使用.curselection()當前選擇[0]和。獲得,但我得到的索引差錯從這個(可能是因爲沒有選擇最初)。那麼如果我設置了一個初始選擇,我不能改變它,它會始終基於該選擇執行。

當然這應該是容易的,我失去了一些東西。

[編輯]添加代碼的建議轉儲:

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關鍵字。

我提出的group變量使用self關鍵字(self.group)一個實例變量,並創建了一個方法,select,其中更新該變量隨時列表框選擇的變化。還使功能generate_histogram成爲一種類方法。根據總體計劃目標,您可能還必須製作一些其他變量實例變量。下面

更新的代碼有註釋來解釋一些其他的變化/增加的。您可能需要根據您使用的Python版本更改導入語句。

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