我想更改tkinter.treeview中选定单元格的前景色或背景色。我怎样才能做到这一点?
此链接显示了更改树视图中所有单元格颜色的命令,但我无法在单个单元格中使用它。
ttk.Style().configure("Treeview", background="#383838",
foreground="white", fieldbackground="red")
我以前写过一个测试代码。请使用此代码得出您的解决方案/建议。谢谢。
该链接显示了如何使用标记来更改数据行(即所选项目)而不是单元格的颜色。
需要改进:
我还没有弄清楚为什么我的算法无法在“图标/树”列和“值”列中所选Treeview单元中的值上准确覆盖“画布文本框”。为此,我求助于使用通过反复试验确定的软糖值。但是,这不是理想的。
有人可以分享我如何在不使用软糖值的情况下实现canvas_textbox覆盖层与Treeview单元格值的精确对齐吗?
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkFont
class App(tk.Frame):
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
#1. Create Treeview with binding
self.tree = ttk.Treeview(parent, columns=("size", "modified"))
self.tree["columns"] = ("date", "time", "loc")
self.tree.column("#0", width=100, anchor='center')
self.tree.column("date", width=100, anchor='center')
self.tree.column("time", width=100, anchor='center')
self.tree.column("loc", width=100, anchor='center')
self.tree.heading("#0", text="Name")
self.tree.heading("date", text="Date")
self.tree.heading("time", text="Time")
self.tree.heading("loc", text="Location")
self.tree.insert("","end", text = "Grace",
values = ("2010-09-23","03:44:53","Garden"))
self.tree.insert("","end", text = "John" ,
values = ("2017-02-05","11:30:23","Airport"))
self.tree.insert("","end", text = "Betty",
values = ("2014-06-25","18:00:00",""))
self.tree.grid()
self.tree.bind('<ButtonRelease-1>', self.selectItem)
#2. Create a Canvas Overlay to show selected Treeview cell
sel_bg = '#ecffc4'
sel_fg = '#05640e'
self.setup_selection(sel_bg, sel_fg)
def setup_selection(self, sel_bg, sel_fg):
self._font = tkFont.Font()
self._canvas = tk.Canvas(self.tree,
background=sel_bg,
borderwidth=0,
highlightthickness=0)
self._canvas.text = self._canvas.create_text(0, 0,
fill=sel_fg,
anchor='w')
def selectItem(self, event):
# Remove Canvas overlay from GUI
self._canvas.place_forget()
# Local Parameters
x, y, widget = event.x, event.y, event.widget
item = widget.item(widget.focus())
itemText = item['text']
itemValues = item['values']
iid = widget.identify_row(y)
column = event.widget.identify_column(x)
print ('\n&&&&&&&& def selectItem(self, event):')
print ('item = ', item)
print ('itemText = ', itemText)
print('itemValues = ',itemValues)
print ('iid = ', iid)
print ('column = ', column)
#Leave method if mouse pointer clicks on Treeview area without data
if not column or not iid:
return
#Leave method if selected item's value is empty
if not len(itemValues):
return
#Get value of selected Treeview cell
if column == '#0':
self.cell_value = itemText
else:
self.cell_value = itemValues[int(column[1]) - 1]
print('column[1] = ',column[1])
print('self.cell_value = ',self.cell_value)
#Leave method if selected Treeview cell is empty
if not self.cell_value: # date is empty
return
#Get the bounding box of selected cell, a tuple (x, y, w, h), where
# x, y are coordinates of the upper left corner of that cell relative
# to the widget, and
# w, h are width and height of the cell in pixels.
# If the item is not visible, the method returns an empty string.
bbox = widget.bbox(iid, column)
print('bbox = ', bbox)
if not bbox: # item is not visible
return
# Update and show selection in Canvas Overlay
self.show_selection(widget, bbox, column)
print('Selected Cell Value = ', self.cell_value)
def show_selection(self, parent, bbox, column):
"""Configure canvas and canvas-textbox for a new selection."""
print('@@@@ def show_selection(self, parent, bbox, column):')
x, y, width, height = bbox
fudgeTreeColumnx = 19 #Determined by trial & error
fudgeColumnx = 15 #Determined by trial & error
# Number of pixels of cell value in horizontal direction
textw = self._font.measure(self.cell_value)
print('textw = ',textw)
# Make Canvas size to fit selected cell
self._canvas.configure(width=width, height=height)
# Position canvas-textbox in Canvas
print('self._canvas.coords(self._canvas.text) = ',
self._canvas.coords(self._canvas.text))
if column == '#0':
self._canvas.coords(self._canvas.text,
fudgeTreeColumnx,
height/2)
else:
self._canvas.coords(self._canvas.text,
(width-(textw-fudgeColumnx))/2.0,
height/2)
# Update value of canvas-textbox with the value of the selected cell.
self._canvas.itemconfigure(self._canvas.text, text=self.cell_value)
# Overlay Canvas over Treeview cell
self._canvas.place(in_=parent, x=x, y=y)
if __name__ == "__main__":
window = tk.Tk()
app = App(window)
window.mainloop()
我在更改JavaFX8中表格单元格的背景色时遇到了问题。 用 setStyle(“-fx-Alignment:centre-right;-fx-background-color:#ffd9d9;”)
有人知道如何更改ZK网格中单元格的背景色吗?在网上搜索了几个小时,找不到太多。静态单元格不是问题,但这是动态渲染的网格。 计划是将某些细胞涂成红色或黄色,因为我想突出显示特定的值。 我的Zul: 我的虚拟机:
我找到了在点击JTable标题时选择特定列的代码。对于我的模块,如果有人选择一个JTable单元格,则必须删除之前选择的所有列。我成功地更改了
我想改变JTable的单元格背景颜色,想从MySQL数据库中获取数据。 我在MySQL中使用一个数据表,它有一个状态字段。如果状态为1,则单元格背景颜色应为红色;如果状态为0,则应更改为红色。
我正在使用Apache Poi将数据写入Excel表格。我需要合并单元格并为合并的单元格设置背景。我有合并单元格的代码,但我不知道如何为单元格设置背景或前景。 //此代码是我用于合并单元格和设置单元格边框的代码
问题内容: 我试图更改选项菜单的默认颜色为白色:我希望选项菜单上的每个项目都为黑色背景。 我已经尝试过在菜单元素中的item元素上进行类似android:itemBackground =“#000000”的拍摄,但是没有用。 我该怎么做? 问题答案: 在花费了大量时间尝试所有选项之后,我能够使用更改溢出菜单背景的应用程序的唯一方法是使用属性: ``` 从API 4.2到5.0进行了测试。