【发布时间】:2020-07-22 05:39:09
【问题描述】:
我有一个 ListCtrl 可以使用各种项目进行自我更新。 为此,我将其清空,然后附加几个项目。
然后我想捕捉 EVT_LIST_ITEM_FOCUSED 事件。 在 Windows、Unix 和 MacOS 上,它运行良好。
最后,我想在更新列表后赶上活动。 这在 Unix 和 MacOS 上自动发生,但在 Windows 上并非如此。 这就是为什么我想在“update()”方法结束时生成一个事件。
代码示例:
import wx
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None)
self.Show()
# Create the ListCtrl
self.list_ctrl = wx.ListCtrl(self, style=wx.LC_REPORT)
self.list_ctrl.AppendColumn("Column")
# Bind the event to the callback function
self.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.on_focus, self.list_ctrl)
# Fill the list with fruits
self.update(["apples", "bananas", "pears"])
def update(self, name_list):
"""Refill the ListCtrl with the content of the list."""
# Empty the ListCtrl
self.list_ctrl.DeleteAllItems()
# Refill it
for element in name_list:
self.list_ctrl.Append([element])
def on_focus(self, event):
"""Print what is currently focused."""
focused = event.GetItem().GetText()
if focused == "":
print("No focus.")
else:
print(f"{focused} is focused.")
app = wx.App()
main_frame = MainFrame()
app.MainLoop()
此代码在 Unix 和 MacOS 程序的开头打印“apples is focus”。 在 Windows 上,它什么也不打印,因为事件没有被触发。 我想要的是在 Windows 上收到“苹果专注于”的消息。
约束:
- 我想使用一个事件,因为我打算将它
Skip()发送到层次结构中更高的面板。 - 我想用我选择的项目文本设置这个事件,这样如果 ListCtrl 中没有项目,程序可以打印“无焦点”。因此调用
self.list_ctrl.Focus(0)不起作用,因为没有项目时它什么也不做。
感谢您的帮助,祝您有美好的一天。
【问题讨论】:
-
如果列表中没有元素,则没有焦点,
self.list_ctrl.Focus(0)将使程序崩溃。还是我误解了你的问题 -
@RolfofSaxony 它不会使程序崩溃,而是什么都不做。但这不是问题:我只想生成一个事件来触发我的
on_focus方法,而不管我的列表中有多少项目。 -
如果没有项目,就没有事件。您可以随时使用
on_focus(None) -
@RolfofSaxony 我考虑过,但我将这个事件跳过到层次结构中更高的面板,所以我真的想构建一个事件对象。
-
您的代码示例确实应该说明您的问题。希望通过这个过程,虽然您没有得到答案,但您已经确定了您的问题。