【问题标题】:Treeview mouse position pythonTreeview鼠标位置python
【发布时间】:2016-04-23 21:25:38
【问题描述】:

我不太确定以前是否有人问过这个问题,因为它似乎太微不足道而不能成为新问题,但是我已经四处寻找了一个小时,但没有找到任何东西

我正在使用 ttk Treeview 来显示和分析表格。对于给定的函数,我希望能够识别已双击的确切单元格(行和列)。虽然很容易找到行,但我找不到列,因为鼠标位置总是相对于屏幕显示,而不是相对于带有 winfo_pointerx() 的 Treeview。我也无法让 event.x 工作......

from Tkinter import *
import ttk
import utils

class AudiDataList():

    def __init__(self, i_root, i_header, i_data, i_types):
        self.root = i_root

        f1 = ttk.Frame(root)
        f1.pack()
        t1 = ttk.Treeview(f1, columns=i_header, show="headings")
        t1.pack()

        for col in i_header:
            t1.heading(col, text=col.title())
        for item in i_data:
            t1.insert('', 'end', values=item, tags = ('item',))
        t1.tag_bind('item', "<Double-Button-1>", callback=lambda c=col: self.filter(t1, c, 0, i_types, i_header))

    def filter(self, tree, col, descending, i_types, i_header):
        print "Row: ", tree.focus()
        print "X-position", tree.winfo_pointerx()
        print "Column: ??? ",tree.identify_column(tree.winfo_pointerx())


header = ['col1', 'col2', 'col3']
datentypen = ['text', 'money', 'int']
data = [
('a', '1,00', 1) ,
('a', '2,00', 2) ,
('b', '12,10', 3) ,
('c', '2,10', 4) ,
('b', '3,00', 5)]

root = Tk()
Auditable = AudiDataList(root, header, data, datentypen)

root.mainloop()

非常感谢您的帮助。

【问题讨论】:

    标签: python treeview python-2.x ttk


    【解决方案1】:

    您需要结合一些技巧来解决您的问题。

    首先,您需要使用winfo_pointerxy()。这将返回一个元组 (x , y),其中包含鼠标指针相对于 tree 的根窗口的坐标。

    然后你需要使用这个元组的第一个元素 winfo_pointerxy()[0] 来调用你减去 winfo_rootx()identify_column() 方法(以解决移动窗口的问题)。

    但是你需要小心,因为winfo_pointerxy() 返回的元组是&lt;type 'unicode'&gt; 类型。因此,例如,您将需要import re 才能使用re.sub(),以便可以将#1 形式的数据替换为整数1

    这意味着您的 filter() 方法现在看起来像这样:

         def filter(self, tree, col, descending, i_types, i_header):               
             print 'Row: {} & Column: {} '.format(re.sub('I00','',str(tree.identify_row(tree.winfo_pointerxy()[1]-tree.winfo_rooty()))),re.sub(r'#','',str(tree.identify_column(tree.winfo_pointerxy()[0]-tree.winfo_rootx()))))
    

    完整程序

    所以这是完整的程序:

    '''
    Created on Apr 24, 2016
    
    @author: billal begueradj
    '''
    from Tkinter import *
    import ttk
    import re
    
    class AudiDataList():
    
        def __init__(self, i_root, i_header, i_data, i_types):
            self.root = i_root
    
            f1 = ttk.Frame(root)
            f1.pack()
            t1 = ttk.Treeview(f1, columns=i_header, show="headings")
            t1.pack()
    
            for col in i_header:
                t1.heading(col, text=col.title())
            for item in i_data:
                t1.insert('', 'end', values=item, tags = ('item',))
            t1.tag_bind('item', "<Double-Button-1>", callback=lambda c=col: self.filter(t1, c, 0, i_types, i_header))
    
             def filter(self, tree, col, descending, i_types, i_header):               
                 print 'Row: {} & Column: {} '.format(re.sub('I00','',str(tree.identify_row(tree.winfo_pointerxy()[1]-tree.winfo_rooty()))),re.sub(r'#','',str(tree.identify_column(tree.winfo_pointerxy()[0]-tree.winfo_rootx())))) 
    
    header = ['col1', 'col2', 'col3']
    datentypen = ['text', 'money', 'int']
    data = [
    ('a', '1,00', 1) ,
    ('a', '2,00', 2) ,
    ('b', '12,10', 3) ,
    ('c', '2,10', 4) ,
    ('b', '3,00', 5)]
    
    root = Tk()
    Auditable = AudiDataList(root, header, data, datentypen)
    
    root.mainloop()
    

    演示

    下面是运行程序的截图:

    【讨论】:

    • 非常感谢比拉尔!我试过了,它有效。但是,我现在需要花一些钱来找出原因……
    • 糟糕。那太快了……和以前一样的问题。如果我将窗口移到屏幕右侧,则 Col1 变为 Col3 并且 Col3 变为 ,即 winpointer_xy() 就像 winfo_pointerx() 似乎返回相对于屏幕而不是相对于窗口的鼠标位置。
    • 你是绝对正确的。我没有移动屏幕上的窗口,所以我没有注意到你提到的问题。我进行了相应的编辑。
    【解决方案2】:

    我猜我找到了。 问题是 tree.winfo_pointerx() 不返回相对于 Treeview 的位置,而是相对于屏幕。 此外,tree.winfo_x() 始终返回零。 但是,从 tree.winfo_pointerx() 中减去 self.root.winfo_x() 就可以了。

    在任何情况下,坐标转换都需要 re。

    现在得走了,如果你需要sn-p,我稍后会发布。

    【讨论】:

    • 最后竟然是:
    • x = tree.winfo_pointerxy()[] - self.root.winfo_rootx() - f1.winfo_x()。这是因为最终实现中的框架 f1 位于窗格窗口的右侧。在实现这一点的过程中真正让我感到困惑的是,找到 x-Position 的方法并不适用于所有对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多