gpt4 book ai didi

wxpython - 当我单击 wx.Dialog 中的“确定”或“取消”按钮时,为什么没有触发 EVT_CLOSE?

转载 作者:行者123 更新时间:2023-12-02 21:54:46 25 4
gpt4 key购买 nike

我有一个 wx.Dialog 子类,当用户单击“确定”按钮时,它需要执行一些清理操作。 wx.Dialog documentation表示单击“确定”或“取消”应发出 EVT_CLOSE 事件:

EVT_CLOSE: The dialog is being closed by the user or programmatically (see Window.Close ). The user may generate this event clicking the close button (typically the ‘X’ on the top-right of the title bar) if it’s present (see the CLOSE_BOX style) or by clicking a button with the ID_CANCEL or ID_OK ids.

但是,我使用的是 WX 2.9.5.0(通过 wxPython),当我在此测试应用程序中单击“确定”或“取消”时,不会调用 OnClose 方法。当我单击系统的关闭按钮(我使用的是 OS X)时,会调用 OnClose。我是否错误地实现了此事件处理,或者 wx.Dialog 确实不符合其文档?在后一种情况下,拦截“确定”按钮点击的最佳方法是什么?

from __future__ import print_function
import wx

class TestDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, title='Test Dialog')

sizer = wx.BoxSizer(wx.VERTICAL)

message = wx.StaticText(self, wx.NewId(), 'This is some dummy text')
sizer.Add(message)

ok_button = wx.Button(self, wx.ID_OK, 'OK')
cancel_button = wx.Button(self, wx.ID_CANCEL, 'Cancel')

btn_sizer = self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL)
btn_sizer.Add(cancel_button)
btn_sizer.Add(ok_button)
sizer.Add(btn_sizer)

self.SetSizer(sizer)

self.Bind(wx.EVT_CLOSE, self.OnClose)

def OnClose(self, event):
print('In OnClose')
event.Skip()

if __name__ == '__main__':
app = wx.App(False)

dialog = TestDialog(None)
result = dialog.ShowModal()
print('Result: {}'.format(result))

最佳答案

当您单击模式对话框上的“确定”或“取消”按钮时,该对话框不会以 Close 关闭,而是以 EndModal 结束,因此 EVT_CLOSE 事件未发送。模式对话框正常完成时需要运行的代码通常放在调用 ShowModal 之后。我认为在这种情况下,文档是不正确的。

OTOH,如果对话框显示为无模式(使用 Show 而不是 ShowModal),则应使用 Close 关闭它们,您将得到EVT_CLOSE 事件。

关于wxpython - 当我单击 wx.Dialog 中的“确定”或“取消”按钮时,为什么没有触发 EVT_CLOSE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21388417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com