tkinter TreeView:禁用选择突出显示

时间:2018-05-10 10:00:50

标签: python tkinter treeview tk

我有一个tkinter TreeView并使用标签来标记多个选定的项目,如下面黄色所示。不幸的是,无论标签如何,最后点击的项目都将始终显示为蓝色。如何阻止TreeView为最后单击的项着色?

以下是我用来选择项目的代码段:

Sub Summarize()
Dim ws As Worksheet: Set ws = Sheets("Sheet1") 'Sheet with data
Dim ws2 As Worksheet: Set ws2 = Sheets("Sheet2") 'Summarised Sheet
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

For i = 2 To LastRow 'loop through rows
    For col = 6 To 14 Step 4 'loop through columns
    'replace 14 with (LastCol - 4) if you wish to do all the months instead of just the first 3
        FreeRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1 'get the next free row to transfer data to
        ws.Range("A" & i & ":D" & i).Copy ws2.Range("A" & FreeRow) 'copy the first 4 columns into the free row
        ws2.Cells(FreeRow, 5).Value = "20" & Mid(ws.Cells(1, col).Value, 5, 2) 'get the year from the header
        ws2.Cells(FreeRow, 6).Value = Left(ws.Cells(1, col).Value, 3) ' get the month name from header
        ws2.Cells(FreeRow, 7).Value = ws.Cells(i, col).Value 'transfer values
        ws2.Cells(FreeRow, 8).Value = ws.Cells(i, col + 1).Value
        ws2.Cells(FreeRow, 9).Value = ws.Cells(i, col + 2).Value
        ws2.Cells(FreeRow, 10).Value = ws.Cells(i, col + 3).Value
    Next col
Next i
End Sub

以下是它的外观示例。请注意,您无法判断DNTP_iDL _...是否应该是白色的(b / c它是黄色的,我点击它以停用它):

All entries can be selected and deselected (yellow) with the code above, but the last clicked item stays blue

2 个答案:

答案 0 :(得分:1)

我知道它很旧,但是我没有找到真正的答案。

经过一番挖掘-可能(不明显,但可能):

很明显,treeview有单独的系统可以上色-通过样式和标签(使用tree.tag_bind可以更动态地添加基本的时尚商品)。但是,我的测试表明,如果您单击该项目,则标签无法从框中使用(此问题的主要问题)。

这是我想出的解决方案:

root = tk.Tk()
style = ttk.Style(root)
style.theme_use("clam")
style.map("Treeview",
          background=[
              ('selected', '!focus', 'yellow'),
          ])
# here is your treeview widgets and stuff
root.mainloop()

可能会有更优雅的解决方案...但是我没有找到。

希望它对某人有帮助!

更新

我发现当您单击该项目然后转到另一个应用程序(alt + Tab)时-您会看到黄色的选择。

我已经通过将背景参数更改为以下方式解决了此问题:

# ('selected', '!focus', 'yellow'),
('selected', 'invalid', 'yellow'),

答案 1 :(得分:0)

事实证明,使用标签选择多个项目时,我走错了路。

只需使用内置选择(使用shift-或cmd-select)然后  +----------+---------+-----------------+-------+----------------------------+---------------+------------------------------+--------+ |Session_ID|Device_ID| Channel_Time|Channel|row_number_by_session_device|channel_changed|channel_changed_filled_row_num|Group_ID| +----------+---------+-----------------+-------+----------------------------+---------------+------------------------------+--------+ | 1| 1|4/9/2018 15:00:00| A| 1| 1| 1| 1| | 1| 1|4/9/2018 15:01:00| A| 2| 0| 0| 1| | 1| 1|4/9/2018 15:02:00| B| 3| 1| 3| 3| | 1| 1|4/9/2018 15:03:00| B| 4| 0| 0| 3| | 1| 1|4/9/2018 15:04:00| B| 5| 0| 0| 3| | 1| 1|4/9/2018 15:05:00| C| 6| 1| 6| 6| | 1| 1|4/9/2018 15:06:00| C| 7| 0| 0| 6| | 1| 1|4/9/2018 15:07:00| A| 8| 1| 8| 8| | 1| 1|4/9/2018 15:08:00| A| 9| 0| 0| 8| | 1| 1|4/9/2018 15:09:00| B| 10| 1| 10| 10| | 1| 1|4/9/2018 15:10:00| B| 11| 0| 0| 10| +----------+---------+-----------------+-------+----------------------------+---------------+------------------------------+--------+ 获取所有选定项目的列表。