【问题标题】:How to refresh the tkinter Listbox如何刷新 tkinter 列表框
【发布时间】:2016-02-17 01:42:04
【问题描述】:

目标:我在列表框中选择一个选项。我希望用黄色突出显示该选项。

问题:之前选择的所有选项也都以黄色突出显示。我希望将最新的选择保留为黄色,并将列表框中的其他所有内容保留为白色。

import tkinter as tk


root = tk.Tk()
fontfamily = tk.font.families()

def selectcolor(col=None):
    fontlist.config(bg='white') ##<--PROBLEM CODE
    option_selected = fontlist.curselection()
    fontlist.itemconfig(option_selected[0], bg='yellow')

fontlist = tk.Listbox (root, bg='white')
fontlist.grid()

for eachfont in fontfamily:
    fontlist.insert(tk.END, eachfont)

fontlist.bind('<<ListboxSelect>>', selectcolor)
##<<ListboxSelect>> is magic, this option is not shown in_
##ebook John Shipman tkinter 8.5 reference
##.curselection() doesn't work as expected

tk.mainloop()

【问题讨论】:

  • 我认为通过 fontlist.config(bg='white') 您正在尝试设置 Listbox 而不是其项目的背景颜色。例如,您可以跟踪最后一个选定的项目并将其着色为白色,然后再将新的选定项目着色为黄色。问题是,由于所选项目的颜色只有在项目失去焦点后才可见(至少在我的情况下),所以你永远不会看到黄色项目......我希望有人提出另一个聪明的解决方案...
  • 谢谢@nbro。我在 selectcolor() 中添加了如下代码,似乎仍然无法正常工作。 size=fontlist.size() while size: fontlist.itemconfig(size-1,bg='white') size-=1

标签: python-3.x tkinter


【解决方案1】:

您可以为ListBox 对象使用selectbackground 选项。您不需要使用fontlist.config(bg='white') 行。解决方案是这样的:

import tkinter as tk


root = tk.Tk()
fontfamily = tk.font.families()

def selectcolor(col=None):
    fontlist.config(bg='white') ##unnecessary
    option_selected = fontlist.curselection()
    fontlist.itemconfig(option_selected[0], selectbackground='yellow')

fontlist = tk.Listbox (root, bg='white')
fontlist.grid()

for eachfont in fontfamily:
    fontlist.insert(tk.END, eachfont)

fontlist.bind('<<ListboxSelect>>', selectcolor)
##<<ListboxSelect>> is magic, this option is not shown in_
##ebook John Shipman tkinter 8.5 reference
##.curselection() doesn't work as expected

tk.mainloop()

由于查询器的问题,我已经编辑了代码。

import tkinter as tk


root = tk.Tk()
fontfamily = tk.font.families()


#you don't need to define a function to change highlight color.


fontlist = tk.Listbox (root, bg='white', selectbackground='yellow') #add selectbackground='yellow' here.
fontlist.grid()

for eachfont in fontfamily:
    fontlist.insert(tk.END, eachfont)

#So it is necessary to bind a function now :)

tk.mainloop()

【讨论】:

  • 由于我的新手身份,我不能投票给你。我现在变得贪婪,在这里改变目标职位。我有三个列表框来选择字体系列、大小和重量。当焦点从当前列表框移动到另一个列表框时,您的解决方案会“取消突出显示”选择颜色(即丢失黄色选择)。有没有办法在移动到不同的列表框后保留黄色突出显示?
  • 您可以在上面找到您的新解决方案。我已经扩展了可能会回答。
【解决方案2】:

以前的项目保持黄色背景,因为您没有将它们改回(这需要记住最后标记的项目)。但是使用 selectbackground 选项要容易得多。出于您的目的,以下内容有什么问题吗?

import tkinter as tk
root = tk.Tk()
box = tk.Listbox(selectforeground='black', selectbackground='yellow')
box.pack()
box.insert('end', 'one', 'two', 'three')
#root.mainloop()

【讨论】:

  • 由于我的新手身份,我不能投票给你。我现在变得贪婪,在这里改变目标职位。我有三个列表框来选择字体系列、大小和重量。当焦点从当前列表框移动到另一个列表框时,您的解决方案会“取消突出显示”选择颜色(即丢失黄色选择)。有没有办法在移动到不同的列表框后保留黄色突出显示?
  • 可能性1:恢复使用itemconfig,加上恢复之前选择的背景,正如我上面建议的那样。这是 tk 默认所做的,除非您更改 'selectmode' 选项。
  • 可能性 2:将 Button、Label 或 Entry 与 ListBox 或下拉菜单组合。请注意,ttk.Combobox 结合了条目和下拉菜单。
  • 可能性 3. 模拟一个带有只读但可点击文本的列表框。这将是最难的,但我知道它会起作用,因为空闲编辑窗口在不活动时不会丢失颜色高光。甚至可以将所有三列放在一个文本框中。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多