tkinter 列表框选择的样式透明
tkinter listbox selected style to transparent
我有一个包含动态 int 值的 tkinter Listbox
,例如 [1,2,3,4,5]
。我修改了我的代码以使其更简单。
self.edt_shots = ttk.Listbox(
self,
height=7,
exportselection=False,
selectforeground="purple",
activestyle=UNDERLINE,
#selectbackground="white", # TRANSPARENT needed here?
)
self.edt_shots.grid(row=3, column=3, rowspan=5)
我在每个项目的背景上做了一些条件格式设置。
因此,例如,所有偶数值都是红色,所有奇数值都是绿色。
列表框颜色为 [red,green, red, green, red]
。
效果不错。
lst=[1,2,3,4,5]
for i, name in enumerate(lst):
self.edt_shots.insert(i, str(name))
# conditionnal formatting
self.edt_shots.itemconfig(
i,
bg="green"
if i%2 == 1
else "red"
)
self.edt_shots.bind("<<ListboxSelect>>", self.on_edt_shots_change)
但我也在select购买商品。我想通过将前景设置为紫色来注意 select 项目。
还好。
但这也会将背景更改为蓝色,因此它会覆盖我不想要的条件格式的背景。
def on_edt_shots_change(self, event):
"""handle item selected event"""
if len(self.edt_shots.curselection()) <= 0:
return
index = self.edt_shots.curselection()[0] + 1
self.edt_shots.select_clear(0, "end")
self.edt_shots.selection_set(index)
self.edt_shots.see(index)
self.edt_shots.activate(index)
self.edt_shots.selection_anchor(index)
在for循环中设置bg
时也设置selectbackground
即可:
lst = [1, 2, 3, 4, 5]
for i, name in enumerate(lst):
self.edt_shots.insert(i, str(name))
# conditionnal formatting
bg = "green" if i%2 == 1 else "red"
self.edt_shots.itemconfig(i, bg=bg, selectbackground=bg)
我有一个包含动态 int 值的 tkinter Listbox
,例如 [1,2,3,4,5]
。我修改了我的代码以使其更简单。
self.edt_shots = ttk.Listbox(
self,
height=7,
exportselection=False,
selectforeground="purple",
activestyle=UNDERLINE,
#selectbackground="white", # TRANSPARENT needed here?
)
self.edt_shots.grid(row=3, column=3, rowspan=5)
我在每个项目的背景上做了一些条件格式设置。
因此,例如,所有偶数值都是红色,所有奇数值都是绿色。
列表框颜色为 [red,green, red, green, red]
。
效果不错。
lst=[1,2,3,4,5]
for i, name in enumerate(lst):
self.edt_shots.insert(i, str(name))
# conditionnal formatting
self.edt_shots.itemconfig(
i,
bg="green"
if i%2 == 1
else "red"
)
self.edt_shots.bind("<<ListboxSelect>>", self.on_edt_shots_change)
但我也在select购买商品。我想通过将前景设置为紫色来注意 select 项目。 还好。 但这也会将背景更改为蓝色,因此它会覆盖我不想要的条件格式的背景。
def on_edt_shots_change(self, event):
"""handle item selected event"""
if len(self.edt_shots.curselection()) <= 0:
return
index = self.edt_shots.curselection()[0] + 1
self.edt_shots.select_clear(0, "end")
self.edt_shots.selection_set(index)
self.edt_shots.see(index)
self.edt_shots.activate(index)
self.edt_shots.selection_anchor(index)
在for循环中设置bg
时也设置selectbackground
即可:
lst = [1, 2, 3, 4, 5]
for i, name in enumerate(lst):
self.edt_shots.insert(i, str(name))
# conditionnal formatting
bg = "green" if i%2 == 1 else "red"
self.edt_shots.itemconfig(i, bg=bg, selectbackground=bg)