2015-12-21 95 views
0

我找不到任何原因,此綁定不起作用。我已經嘗試設置焦點,除了其他事情之外,還要在班級之外移動綁定。奇怪的是,它在程序開始的時候只有一次。這導致我相信綁定確實起作用,但並非如預期那樣。Tkinter綁定只能工作一次

import tkinter as tk 
import os 
import Themes 

########################################################################## 
root = tk.Tk() 

topleft = tk.Frame(root, 
       bg = 'red', 
       ) 

topmid = tk.Frame(root, 
       bg = 'white', 
      ) 

topright = tk.Frame(root, 
       bg = 'blue', 
       ) 

bottomleft = tk.Frame(root, 
        bg = 'yellow', 
       ) 

bottommid = tk.Frame(root, 
        bg = 'green', 
       ) 

bottomright = tk.Frame(root, 
        bg = 'black', 
        ) 

tk.Grid.rowconfigure(root, 0, weight = 20) 
tk.Grid.columnconfigure(root, 0, weight = 30) 
topleft.grid(row = 0, column = 0, 
     sticky = tk.N+tk.E+tk.S+tk.W, 
     rowspan = 2, columnspan = 1) 

tk.Grid.columnconfigure(root, 1, weight = 20) 
topmid.grid(row = 0, column = 1, 
     sticky = tk.N+tk.E+tk.S+tk.W, 
     rowspan = 1, columnspan = 2) 

tk.Grid.columnconfigure(root, 3, weight = 85) 
topright.grid(row = 0, column = 3, 
      sticky = tk.N+tk.E+tk.S+tk.W, 
      rowspan = 3, columnspan = 1) 

tk.Grid.rowconfigure(root, 2, weight = 40) 
bottomleft.grid_propagate(False) 
bottomleft.grid(row = 2, column = 0, 
      sticky = tk.N+tk.E+tk.S+tk.W, 
      rowspan = 1, columnspan = 3) 

tk.Grid.rowconfigure(root, 1, weight = 10) 
tk.Grid.columnconfigure(root, 1, weight = 10) 
bottommid.grid(row = 1, column = 1, 
      sticky = tk.N+tk.E+tk.S+tk.W, 
      rowspan = 1, columnspan = 1) 

tk.Grid.rowconfigure(root, 1, weight = 10) 
tk.Grid.columnconfigure(root, 2, weight = 10) 
bottomright.grid(row = 1, column = 2, 
      sticky = tk.N+tk.E+tk.S+tk.W, 
      rowspan = 1, columnspan = 1) 
########################################################################## 

class File_selector: 
    def __init__(self): 
    self.scrollbar = tk.Scrollbar(bottomleft, 
            orient = 'vertical' 
           ) 
    self.listbox = tk.Listbox(bottomleft, 
           yscrollcommand = self.scrollbar.set 
          ) 
    self.check = print(self.listbox.curselection()) 

    self.scrollbar['command'] = self.listbox.yview 

    for file in os.listdir(os.curdir): 
     if file.endswith(".py"): 
      self.listbox.insert(tk.END, file) 

    self.listbox.bind('<<ListboxSelect>>', lambda e: self.check) 

    self.grid() 
    def grid(self):  
    self.scrollbar.grid(row = 0, column = 1, sticky = tk.N+tk.S) 

    tk.Grid.rowconfigure(bottomleft, 0, weight = 1) 
    tk.Grid.columnconfigure(bottomleft, 0, weight = 1) 
    self.listbox.grid(row = 0, column = 0, 
         sticky = tk.N+tk.E+tk.S+tk.W) 

File_selector() 
root.mainloop() 
+0

上的任何選擇觸發一次......然後需要獲得所選擇的項目... –

+0

不'self.check'做到這一點?目前,我只是想讓程序確認我點擊列表框。 @Joran –

+0

你的例子中的縮進被搞砸了。 –

回答

1

self.check = print(self.listbox.curselection()) 

不正確。

它運行在屏幕上時(File_selector對象被創建)print()並打印文本,然後將其分配Noneself.check(因爲print()回報None

您需要正常功能

def check(self, event): 
    print(self.listbox.curselection()) 

lambda功能

self.check = lambda event:print(self.listbox.curselection()) 

An d,那麼你可以綁定

self.listbox.bind('<<ListboxSelect>>', self.check) 
+0

這工作完美。非常感謝!對於未來,無論如何我都要檢查是否有任何內容返回'None'? –

+0

Python函數默認返回無。閱讀文檔或文檔字符串或使用'help(func)'來找出非默認返回。 –