我找不到任何原因,此綁定不起作用。我已經嘗試設置焦點,除了其他事情之外,還要在班級之外移動綁定。奇怪的是,它在程序開始的時候只有一次。這導致我相信綁定確實起作用,但並非如預期那樣。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()
上的任何選擇觸發一次......然後需要獲得所選擇的項目... –
不'self.check'做到這一點?目前,我只是想讓程序確認我點擊列表框。 @Joran –
你的例子中的縮進被搞砸了。 –