【发布时间】:2019-03-11 16:10:37
【问题描述】:
我有一个 Tkinter 窗口,它有一个文本框和子类 ttk.TreeView。当我运行程序时,我在文本框中输入文本并切换到树视图。当我尝试使用Tab 键执行此操作时,似乎焦点不是切换到表格,而是切换到滚动条,因此当按下向上/向下键时,表格内容会滚动,但不会被选中。如何使用Tab 键切换到表本身?
工作示例:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.ttk as ttk
class TV(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.CreateUI()
self.LoadTable()
self.grid(sticky = (tk.N,tk.S,tk.W,tk.E))
parent.grid_rowconfigure(0, weight = 1)
parent.grid_columnconfigure(0, weight = 1)
def CreateUI(self):
tv = ttk.Treeview(self,yscrollcommand=sc.set,height=30)
tv['columns'] = ('Col1', 'Col2', 'Col3')
tv.heading("#0", text='id')
tv.heading('Col1', text='Col1')
tv.heading('Col2', text='Col2')
tv.heading('Col3', text='Col3')
tv.grid(sticky = (tk.N,tk.S,tk.W,tk.E))
self.treeview = tv
self.treeview.bind('<1>',self.OnClick)
self.grid_rowconfigure(0, weight = 1)
self.grid_columnconfigure(0, weight = 1)
def LoadTable(self):
for i in range(100):
self.treeview.insert('', 'end', iid=str(i+1), text='1', values=(2, 3, 4))
def OnClick(self,event):
rowid=self.treeview.identify_row(event.y)
self.treeview.selection_set(rowid)
root=tk.Tk()
sv=tk.StringVar()
filt=tk.Entry(root,textvariable=sv)
filt.grid(row=0,column=0,sticky='nw')
sc=tk.Scrollbar(root)
sc.grid(row=1,column=1,sticky='ns')
ic_list=TV(root)
ic_list.grid(row=1,column=0,sticky='ns')
sc.config(command=ic_list.treeview.yview)
ic_list.treeview.selection_set('1')
filt.focus()
root.mainloop()
【问题讨论】:
-
您是否尝试过向 Tab 键添加显式绑定?