wx.ComboCtrl 与 wx.ListCtrl 突出显示背景和大小

问题描述

我正在构建一个带有 wx.ListCtrl 的 wx.ComboCtrl。这样做的原因是因为我想设置选项的前景色(颜色向用户显示项目的状态)。我希望这些颜色在下拉框和用户进行选择时显示

我遇到的问题是,在 Linux (Ubuntu 20.04) 上,进行选择后,wx.ComboCtrl 的背景颜色保持蓝色(前景色保持白色),即使我将焦点移到另一个小部件.我为要在 ComboCtrl 上显示的文本设置了哪种颜色并不重要,它仍然是带有蓝色背景的白色文本。看截图。

enter image description here

如果我将焦点移动到另一个窗口然后又回到我自己的窗口,我只能让它显示认(灰色)背景和我选择的前景色。

在 Windows 中不会发生这种情况:在选择一个项目后,ComboCtrl 的背景颜色是认的(灰色),但是它确实在选择的周围显示了一条小虚线。看截图。

enter image description here

这是我用来重现问题的修改后的演示代码代码中的注释是我尝试过的一些东西的遗留物。

#!/usr/bin/env python

import wx
import os

#----------------------------------------------------------------------

#----------------------------------------------------------------------
# This class is used to provide an interface between a ComboCtrl and the
# ListCtrl that is used as the popoup for the combo widget.

class ListCtrlComboPopup(wx.ComboPopup):

    def __init__(self):
        wx.ComboPopup.__init__(self)
        self.lc = None

    def AddItem(self,txt,_colour):
        self.lc.InsertItem(self.lc.GetItemCount(),txt)
        _entry = self.lc.GetItem(self.lc.GetItemCount() - 1)
        _entry.SetTextColour(_colour)
        #_entry.SetItemTextColour(_colour)
        self.lc.SetItem(_entry)

    def OnMotion(self,evt):
        item,flags = self.lc.HitTest(evt.GetPosition())
        if item >= 0:
            self.lc.Select(item)
            self.curitem = item

    def OnLeftDown(self,evt):
        self.value = self.curitem
        self.dismiss()


    # The following methods are those that are overridable from the
    # ComboPopup base class.  Most of them are not required,but all
    # are shown here for demonstration purposes.

    # This is called immediately after construction finishes.  You can
    # use self.GetCombo if needed to get to the ComboCtrl instance.
    def Init(self):
        self.value = -1
        self.curitem = -1

    # Create the popup child control.  Return true for success.
    def Create(self,parent):
        self.lc = wx.ListCtrl(parent,style=wx.LC_SINGLE_SEL | wx.SIMPLE_BORDER | wx.LC_REPORT | wx.LC_NO_HEADER)
        self.lc.InsertColumn(0,'')
        self.lc.Bind(wx.EVT_MOTION,self.OnMotion)
        self.lc.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
        return True

    # Return the widget that is to be used for the popup
    def GetControl(self):
        return self.lc

    # Called just prior to displaying the popup,you can use it to
    # 'select' the current item.
    def SetStringValue(self,val):
        idx = self.lc.FindItem(-1,val)
        if idx != wx.NOT_FOUND:
            self.lc.Select(idx)

    # Return a string representation of the current item.
    def GetStringValue(self):
        if self.value >= 0:
            return self.lc.GetItemText(self.value)
        return ""

    # Called immediately after the popup is shown
    def OnPopup(self):
        wx.ComboPopup.OnPopup(self)

    # Called when popup is dismissed
    def Ondismiss(self):
        print (self.GetStringValue())
        wx.ComboPopup.Ondismiss(self)

    # This is called to custom paint in the combo control itself
    # (ie. not the popup).  Default implementation draws value as
    # string.
    def PaintComboControl(self,dc,rect):
        wx.ComboPopup.PaintComboControl(self,rect)

    # Receives key events from the parent ComboCtrl.  Events not
    # handled should be skipped,as usual.
    def OnComboKeyEvent(self,event):
        wx.ComboPopup.OnComboKeyEvent(self,event)

    # Implement if you need to support special action when user
    # double-clicks on the parent wxComboCtrl.
    def OnCombodoubleClick(self):
        wx.ComboPopup.OnCombodoubleClick(self)

    # Return final size of popup. Called on every popup,just prior to OnPopup.
    # minWidth = preferred minimum width for window
    # prefheight = preferred height. Only applies if > 0,# maxHeight = max height for window,as limited by screen size
    #   and should only be rounded down,if necessary.
    def GetAdjustedSize(self,minWidth,prefheight,maxHeight):
        return wx.ComboPopup.GetAdjustedSize(self,maxHeight)

    # Return true if you want delay the call to Create until the popup
    # is shown for the first time. It is more efficient,but note that
    # it is often more convenient to have the control created
    # immediately.
    # Default returns false.
    def LazyCreate(self):
        return wx.ComboPopup.LazyCreate(self)

#----------------------------------------------------------------------


class MyTestPanel(wx.Panel):
    def __init__(self,parent,log):
        self.log = log
        wx.Panel.__init__(self,-1)
        
        txt = wx.TextCtrl(self,wx.ID_ANY,pos=(100,100))

        comboCtrl = wx.ComboCtrl(self,"Third item",(10,10),size=(200,-1),style=wx.CB_READONLY)
        popupCtrl = ListCtrlComboPopup()

        # It is important to call SetPopupControl() as soon as possible
        comboCtrl.SetPopupControl(popupCtrl)

        # Populate using wx.ListView methods
        popupCtrl.AddItem("First Item",[255,127,0])
        popupCtrl.AddItem("Second Item",[192,45])
        popupCtrl.AddItem("Third Item",[25,223,172])
        #popupCtrl.GetAdjustedSize(100,35,100)
        #comboCtrl.SetTextColour(_colour)
        comboCtrl.SetForegroundColour(wx.Colour(235,55,55))


#----------------------------------------------------------------------

def runTest(frame,nb,log):
    win = MyTestPanel(nb,log)
    return win

#----------------------------------------------------------------------



overview = """<html><body>
<h2><center>wx.combo.ComboCtrl</center></h2>

A combo control is a generic comboBox that allows a totally custom
popup. In addition it has other customization features. For instance,position and size of the dropdown button can be changed.

</body></html>
"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['',os.path.basename(sys.argv[0])] + sys.argv[1:])

问题 1:

如何才能在选择项目后显示适当的文本颜色(我以编程方式设置的颜色)和认(灰色)背景颜色。

问题 2:

当下拉 ComboCtrl 时,它显示的是 ListCtrl,它只有一列。可以看到列表中的“第二项”没有完全显示,因为列太窄了。我如何才能使列始终与小部件本身的宽度相同,即使 ComboCtrl 调整大小(由于调整父窗口大小)?

问题 3:

不太重要,但是当我们讨论这个主题时:有没有办法摆脱在 Windows 中运行时显示在所选项目周围的小虚线框?

提前,非常感谢您对此的想法和想法。

马克。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)