只有在treeview小部件/tkinter中的click事件中执行第一个标记_configure

q7solyqu  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(181)

在过去的几个小时里,我一直在研究这个问题,似乎不明白为什么代码的行为会如此,因此请求任何帮助。基本上,我有一个tkinter python 3.6版本的treeview小部件,包含60000多个项目。我创建了一个函数ontripleclick,“理想情况下”应该突出显示具有特定样式的第一个单击的项(使用标记),当下一个项被三次单击时,以前的样式项应该返回到原始状态(使用另一个标记),而新单击的项应该采用样式状态。出于某种原因,只有第一个标记配置被执行-如果我注解掉第一个标记配置,那么第二个标记配置也会被执行-当第二个标记配置的if条件满足时,我需要它们都被执行。非常感谢您的建议!
编辑:根据要求-我已经包含了一个最小版本的工作代码,如上所述的想法是突出显示一个具有一种样式(trclicked标记)的三次左键单击行,当另一行被三次单击时,先前单击的项返回到原始状态(普通标记),而新的项采用该样式(trclicked标记)。按原样运行代码,然后注解掉第9行的第一个tree.tag_configure()位,然后重新运行。这次执行第二个tree.tag_configure(){line14}——我需要同时执行它们。非常感谢。

from tkinter import *
from tkinter import ttk

def onTripleClick(event):
    global selectedSigName, selectedSigPath, trClickedItemiid, toBeClearedItemiid
    itemSelection = tree.selection()[0] 

    trClickedItemiid = tree.focus()
    tree.tag_configure('trClicked'+str(trClickedItemiid), background='light green', foreground='black', font=( 'Helvetica' ,8, 'bold', 'italic')) # font=(family, size, weight, slant, underline, overstrike) 
    # tree.tag_bind('trClicked'+str(trClickedItemiid),'<1>', trClickedItemiid)
    prevTrClicked.append(trClickedItemiid)
    if len(prevTrClicked) > 1:
        toBeClearedItemiid = prevTrClicked.pop(0) # pop the 0th index and pass it to normal
        tree.tag_configure('normal'+str(toBeClearedItemiid), background='pink', foreground='black', font=( 'Courier' ,8, 'normal', 'roman'))
        # tree.tag_bind('normal'+str(toBeClearedItemiid),'<1>', toBeClearedItemiid)

# Create main root object of TK class

root = Tk()
root.title('MyTreeview')
root.geometry("700x500")

# create frame to house treeview AND scrollbar

frame = Frame(root)
frame.pack(pady=5)

tree = ttk.Treeview(frame, height=20, selectmode="browse")
tree.pack(side=LEFT)
tree['columns'] = ("Column1", "Column2", "Column3")

# Format Columns

tree.column("#0", width=10, minwidth=10) # this is where the plus icon will live
tree.column("Column1", anchor=W, width=150)
tree.column("Column2", anchor=W, width=300)
tree.column("Column3", anchor=W, width=120)

# Create headings

tree.heading("#0", text="", anchor=W)
tree.heading("Column1", text="Column1", anchor=W)
tree.heading("Column2", text="Column2", anchor=W)
tree.heading("Column3", text="Column3", anchor=W)

# to be used by TrClicked Function for helping clear older clicked items

global prevTrClicked
prevTrClicked = [] #

rows= [
    ['TopMostParent1', '2ndParent-ColE', 'ColF-3rdParent'],
    ['TopMostParent2', '5thParent-ColE', 'ColF-1stParent'],
    ['TopMostParent3', '4thParent-ColE', 'ColF-2ndParent'],
    ['TopMostParent4', '2ndParent-ColE', 'ColF-3rdParent'],
    ['TopMostParent4', '4thParent-ColE', 'ColF-1stParent'],
    ['TopMostParent6', '3rdParent-ColE', 'ColF-2ndParent'],
    ['TopMostParent5', '3rdParent-ColE', 'ColF-1stParent'],
    ['TopMostParent4', '3rdParent-ColE', 'ColF-3rdParent'],
    ['TopMostParent2', '3rdParent-ColE', 'ColF-3rdParent']
]
count=0
for row in rows:
    tree.insert(parent='', index='end', iid=count, text='', tags=('trClicked'+str(count), 'normal'+str(count)), values=(row[0], row[1], row[2]))
    count+=1

tree.bind("<Triple-1>", onTripleClick)

# tree.tag_bind('trClicked'+str(trClickedItemiid),'<1>', trClickedItemiid)

# tree.tag_bind('normal'+str(toBeClearedItemiid),'<1>', toBeClearedItemiid)

root.mainloop()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题