如何在WxPython中查找和更改选定文本的DataViewListCtrl BackgroundColor?

h7wcgrx3  于 2022-12-05  发布在  Python
关注(0)|答案(2)|浏览(377)

My problem is I can't find the command to change the background color of DataViewListCtrl for selected text/row/item in DataViewListCtrl object. I looked into the documentation but there's no apparent reference.
https://docs.wxpython.org/wx.dataview.DataViewCtrl.html#wx-dataview-dataviewctrl
I'm using the sample_one.py script from this reference:
https://wiki.wxpython.org/How%20to%20add%20a%20menu%20bar%20in%20the%20title%20bar%20%28Phoenix%29
The DataViewListCtrl has this example code:

self.dvlc = dv.DataViewListCtrl(
            self,
            style=dv.DV_MULTIPLE
            | dv.DV_ROW_LINES
            | dv.DV_HORIZ_RULES
            | dv.DV_VERT_RULES,
        )

        self.dvlc.SetBackgroundColour("#ffffff")
        self.dvlc.SetForegroundColour("black")

What I'm looking to do is similar to Tkinter example below (change the selected text background color to blue):

style.configure(
    "Treeview", background="#FFFFDD", foreground="black", fieldbackground="#FFFFDD"
)
style.map("Treeview", background=[("selected", "#F0FFFF")])

I haven't been able to try a workaround yet as I'm not sure how to get the needed command/value, but on my end the selected DataViewListCtrl items have a #242424 color (I checked with an eyedropper from a screenshot).
I found the 2 parameters IsSelected and IsRowSelected in the doc:
https://docs.wxpython.org/wx.dataview.DataViewListCtrl.html#wx.dataview.DataViewListCtrl.IsRowSelected
https://docs.wxpython.org/wx.dataview.DataViewCtrl.html#wx.dataview.DataViewCtrl.IsSelected
and tested as

self.dvlc = dv.DataViewListCtrl(
            self,
            style=dv.DV_MULTIPLE
            | dv.DV_ROW_LINES
            | dv.DV_HORIZ_RULES
            | dv.DV_VERT_RULES,
        )

        # Give it some columns.
        self.dvlc.AppendTextColumn("Id", width=40)
        self.dvlc.AppendTextColumn("Artist", width=170)
        self.dvlc.AppendTextColumn("Title", width=260)
        self.dvlc.AppendTextColumn("Genre", width=80)

        # Load the data. Each item (row) is added as a sequence
        # of values whose order matches the columns.
        for itemvalues in musicdata:
            self.dvlc.AppendItem(itemvalues)

        # — 1st test with same result as 2nd below
        # if self.dvlc.IsSelected == True:
        #    self.dvlc.SetBackgroundColour("#0066ff")
        # else:
        #    self.dvlc.SetBackgroundColour("#F0FFFF")

        if self.dvlc.IsRowSelected == True:
            self.dvlc.SetBackgroundColour("#0066ff")
        else:
            self.dvlc.SetBackgroundColour("#F0FFFF")

        self.dvlc.SetForegroundColour("black")

I tried also without the == True explicit as

# — 1st test with same result as 2nd below
        # if self.dvlc.IsSelected:
        #    self.dvlc.SetBackgroundColour("#0066ff")
        # else:
        #    self.dvlc.SetBackgroundColour("#F0FFFF")

        if self.dvlc.IsRowSelected:
            self.dvlc.SetBackgroundColour("#0066ff")
        else:
            self.dvlc.SetBackgroundColour("#F0FFFF")

and the unexpected blue background displays by default (when not selected).
Is there some documented "selected" parameter or otherwise known working method to be able to capture the selected background color and change it in WxPython?

klr1opcd

klr1opcd1#

要在wxPython中更改DataViewListCtrl对象中所选项的背景颜色,可以使用SetItemBackgroundColour方法。该方法有两个参数:项对象和新的背景色。
下面的示例说明如何使用此方法更改DataViewListCtrl对象中选定项的背景色:

# Create the DataViewListCtrl object
dvlc = dv.DataViewListCtrl(
    self,
    style=dv.DV_MULTIPLE | dv.DV_ROW_LINES | dv.DV_HORIZ_RULES | dv.DV_VERT_RULES,
)

# Give it some columns
dvlc.AppendTextColumn("Id", width=40)
dvlc.AppendTextColumn("Artist", width=170)
dvlc.AppendTextColumn("Title", width=260)
dvlc.AppendTextColumn("Genre", width=80)

# Load the data
for itemvalues in musicdata:
    dvlc.AppendItem(itemvalues)

# Set the background color of the selected items to blue
dvlc.SetItemBackgroundColour(dvlc.GetSelection(), "#0066ff")

请注意,SetItemBackgroundColour方法仅在DataViewListCtrl对象设置了DV_MULTIPLE样式标志(允许选择多个项目)时才有效。

cedebl8k

cedebl8k2#

SetItemBackgroundColour方法似乎不是wxPython中wx.dataview.DataViewListCtrl类的一部分。你可以通过查看该类的文档来确认这一点,文档中列出了所有可用的方法和属性。
设置DataViewListCtrl对象中选定项的背景色的一种方法是使用SetBackgroundColour方法。此方法采用单个参数(即新的背景色),并设置整个DataViewListCtrl对象的背景色。下面是如何使用此方法的示例:

dvlc = dv.DataViewListCtrl(...)

# Load the data
for itemvalues in musicdata:
    dvlc.AppendItem(itemvalues)

# Set the background color of the selected items to blue
dvlc.SetBackgroundColour("#0066ff")

这会将整个DataViewListCtrl对象的背景色彩设定为蓝色,其中会包含选取的项目。
或者,您可以使用SetItemBackgroundColour方法来设定DataViewListCtrl对象中个别项目的背景色彩。这个方法需要两个参数:返回项目的索引和新的背景色。下面是一个示例,说明如何使用此方法设置选定项目的背景色:

dvlc = dv.DataViewListCtrl(...)

# Load the data
for itemvalues in musicdata:
    dvlc.AppendItem(itemvalues)

# Set the background color of the selected items to blue
for index in dvlc.GetSelections():
    dvlc.SetItemBackgroundColour(index, "#0066ff")

这会将每个选取项目的背景色彩设定为蓝色。请注意,您必须在选取项目的清单中循环,并使用SetItemBackgroundColour方法个别设定每个项目的背景色彩。

相关问题