检查时间控制并在 listrCtrl 中更改颜色

Check Time Control and change color in listrCtrl

在################代码#########之间的代码部分,我尝试用if做一个控制但没有'没用。
我希望当变量 'ts' 等于变量 'prova' 时,同一列表 ctrl 中的行(项目)变为红色。对于我在 ctrl 列表中插入的所有项目。非常感谢!

import wx
import wx.gizmos as gizmos
import time
import datetime
from datetime import timedelta

class CWindow(wx.Frame):

    def __init__(self, parent, id, title):

        wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ wx.CLOSE_BOX ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, size=(600,500))

        dataCorrente = datetime.datetime.now()

        self.Panel = wx.Panel(self, -1)
        self.index = 0

        self.CTesto = wx.TextCtrl(self.Panel, 1, pos=(10,40), style=wx.TE_PROCESS_ENTER)
        self.CTesto.Bind(wx.EVT_TEXT_ENTER,self.add_line)

        self.list_ctrl = wx.ListCtrl(self.Panel, pos=(10,90),size=(-1,300),style=wx.LC_REPORT|wx.BORDER_SUNKEN)
        self.list_ctrl.InsertColumn(0, 'Name')
        self.list_ctrl.InsertColumn(1, 'Time START')
        self.list_ctrl.InsertColumn(2, 'Time FINISH', width=100)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)


        self.led = gizmos.LEDNumberCtrl(self.Panel, -1, pos = (350,25), size = (200,50), style = gizmos.LED_ALIGN_CENTER)
        self.led.SetBackgroundColour("#c0c0c0")
        self.led.SetForegroundColour("black")
        self.OnTimer(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        style = gizmos.LED_ALIGN_CENTER


    def OnTimer(self, event):

        current = time.localtime(time.time())
        global ts
        ts = time.strftime("%H:%M:%S", current)
        self.led.SetValue(ts)
        #print (ts) # In Loop it's OK

############################################################################################################################

        if ts == prova:
            self.list_ctrl.SetItemBackgroundColour(self.index, wx.RED)


############################################################################################################################

    def add_line(self,event):

        val = str(self.CTesto.GetValue())

        if val== '':

            msg = wx.MessageDialog(self, "Error", "Error", wx.OK| wx.ICON_ERROR)
            msg.ShowModal()
            msg.Destroy()

        else:

            dataCorrente = datetime.datetime.now()
            oraAttuale =(dataCorrente.strftime("%H:%M:%S"))
            plus = (datetime.datetime.strptime(oraAttuale, "%H:%M:%S") + datetime.timedelta(minutes=1))
            global plus2
            plus2 = plus.strftime("%H:%M:%S")

            self.list_ctrl.InsertItem(self.index, val)
            self.list_ctrl.SetItem(self.index, 1, oraAttuale)
            self.list_ctrl.SetItem(self.index, 2, str(plus2))
            self.index += 1


            InsVal = (val + " - " + oraAttuale + " - " + plus2 + '\n')
            self.CTesto.Clear()

            print (InsVal)

        prova = InsVal[-9:]

app = wx.App()  
frame = CWindow(None, -1, "Example")
frame.Show()
frame.Center()
app.MainLoop()

repr() 是你这个问题的朋友。
print(repr(self.prova)) 会显示它包含换行符,因此需要 stripped 关闭。
另请注意 ListCtrl 索引是从零开始的,因此要应用颜色,您必须使用 index - 1.
通过这些微小的调整,您的代码可以正常工作,正如我希望的那样。

import wx
import wx.gizmos as gizmos
import time
import datetime
from datetime import timedelta

class CWindow(wx.Frame):

    def __init__(self, parent, id, title):

        wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ wx.CLOSE_BOX ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, size=(600,500))

        dataCorrente = datetime.datetime.now()

        self.Panel = wx.Panel(self, -1)
        self.index = 0
        self.prova = {}
        self.ts = ""

        self.CTesto = wx.TextCtrl(self.Panel, 1, pos=(10,40), style=wx.TE_PROCESS_ENTER)
        self.CTesto.Bind(wx.EVT_TEXT_ENTER,self.add_line)

        self.list_ctrl = wx.ListCtrl(self.Panel, pos=(10,90),size=(-1,300),style=wx.LC_REPORT|wx.BORDER_SUNKEN)
        self.list_ctrl.InsertColumn(0, 'Name')
        self.list_ctrl.InsertColumn(1, 'Time START')
        self.list_ctrl.InsertColumn(2, 'Time FINISH', width=100)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)


        self.led = gizmos.LEDNumberCtrl(self.Panel, -1, pos = (350,25), size = (200,50), style = gizmos.LED_ALIGN_CENTER)
        self.led.SetBackgroundColour("#c0c0c0")
        self.led.SetForegroundColour("black")
        self.OnTimer(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        style = gizmos.LED_ALIGN_CENTER


    def OnTimer(self, event):

        current = time.localtime(time.time())
        self.ts = time.strftime("%H:%M:%S", current)
        self.led.SetValue(self.ts)

############################################################################################################################

        if self.ts in self.prova:
            self.list_ctrl.SetItemBackgroundColour(self.prova.pop(self.ts), wx.RED)

############################################################################################################################

    def add_line(self,event):

        val = str(self.CTesto.GetValue())

        if val== '':

            msg = wx.MessageDialog(self, "Error", "Error", wx.OK| wx.ICON_ERROR)
            msg.ShowModal()
            msg.Destroy()

        else:

            dataCorrente = datetime.datetime.now()
            oraAttuale =(dataCorrente.strftime("%H:%M:%S"))
            plus = (datetime.datetime.strptime(oraAttuale, "%H:%M:%S") + datetime.timedelta(minutes=1))
            plus2 = plus.strftime("%H:%M:%S")

            if plus2 in self.prova:
                msg = wx.MessageDialog(self, "Duplicated Time", "Error", wx.OK| wx.ICON_ERROR)
                msg.ShowModal()
                msg.Destroy()
                return
            self.list_ctrl.InsertItem(self.index, val)
            self.list_ctrl.SetItem(self.index, 1, oraAttuale)
            self.list_ctrl.SetItem(self.index, 2, str(plus2))
            self.index += 1


            InsVal = (val + " - " + oraAttuale + " - " + plus2 + '\n')
            self.CTesto.Clear()

        self.prova[plus2] = self.index -1

app = wx.App()
frame = CWindow(None, -1, "Example")
frame.Show()
frame.Center()
app.MainLoop()

编辑: 如果您想测试所有条目,在它们出现时将它们变为红色,您将必须更改 prova.
的性质 一种方法是将 prova 设为 dictionary 并使用上面编辑的代码。
这提供了一种防止重复条目的方法和一种删除字典中条目的简单方法prova,减少了所需的检查量。通过这种方式,代码可以连续 运行 数天而不会重复。字典条目是 Finish 时间和 listctrl 的索引值,用于执行突出显示。
删除dictionary条目(pop)的行为,returns高亮索引号。