首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Tab键正确切换到ttk.TreeView

使用Tab键正确切换到ttk.TreeView
EN

Stack Overflow用户
提问于 2019-03-12 00:10:37
回答 1查看 160关注 0票数 0

我有一个Tkinter窗口,它有一个文本框和一个子类化的ttk.TreeView。当我运行该程序时,我在文本框中输入文本并切换到treeview。当我尝试使用Tab键执行此操作时,焦点似乎不是切换到表格,而是切换到滚动条,因此当按向上/向下键时,表格内容会滚动,但不会被选择。如何使用Tab键切换到表本身?

工作示例:

代码语言:javascript
复制
#!/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()
EN

回答 1

Stack Overflow用户

发布于 2019-03-12 03:37:09

对我自己和那些感兴趣的人负责。首先,我禁用了滚动条上的焦点,以避免按下额外的Tab键:

代码语言:javascript
复制
sc=tk.Scrollbar(root, takefocus=0)

然后我添加了一个获得焦点的处理程序,这要归功于this answer:

代码语言:javascript
复制
def on_get_focus(self, event):
    self.treeview.selection_set(self.treeview.get_children('')[0]) #set selection on the first item
    self.treeview.focus_set()
    self.treeview.focus(self.treeview.get_children('')[0])
    # here I added the code for OnClick event

# in CreateUI()
self.bind('<FocusIn>',self.on_get_focus)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55106046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档