我使用wxpython创建GUI,我有以下自定义对话框类:
class GetDataDlg(wx.Dialog):
def __init__(self, *args, **kwargs):
self.parameters = kwargs.pop('parameters', None)
request = kwargs.pop('request', None)
assert self.parameters is not None
assert request is not None
strings = re.findall('[A-Z][a-z]*', request)
info = ""
for string in strings:
if len(string) == 1:
info = info + string
elif not info:
info = string.lower()
else:
info = info + " " + string.lower()
wx.Dialog.__init__(self, *args, **kwargs)
self.data = {}
main_sizer = wx.BoxSizer(wx.VERTICAL)
input_text = wx.StaticText(self, label="Please type the new {}".format(info))
main_sizer.Add(input_text, 1, wx.ALL, 10)
input_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.Add(input_sizer, 1, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 10)
text_sizer = wx.BoxSizer(wx.VERTICAL)
input_sizer.Add(text_sizer, 1, wx.ALIGN_LEFT | wx.RIGHT, 10)
ctrl_sizer = wx.BoxSizer(wx.VERTICAL)
input_sizer.Add(ctrl_sizer, 1, wx.ALIGN_RIGHT)
self.controls = controls = {}
for key in self.parameters:
text = wx.StaticText(self, label=key)
text_sizer.Add(text, 0, wx.BOTTOM, 17)
ctrl = controls[key] = wx.TextCtrl(self)
ctrl_sizer.Add(ctrl, 0, wx.BOTTOM, 10)
ok_button = wx.Button(self, id=wx.ID_OK)
main_sizer.Add(ok_button, 1, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)
self.SetSizer(main_sizer)
self.Fit()
self.Layout()
ok_button.Bind(wx.EVT_BUTTON, self.save_data)
def save_data(self, event):
for item in self.parameters:
self.data[item] = self.controls[item].GetValue()
event.Skip()
在我的主框架中,我将对话框称为:
dlg = GetDataDlg(self, parameters=parameter, request=item)
result = dlg.ShowModal()
现在我需要检测用户是否按下了我的代码提供的ok按钮或者类本身提供的对话框右上角的关闭按钮。 result
似乎在两种情况下没有变化,dlg
的其他属性也没有变化。此外,我无法检查是否存在dlg.data
,因为即使按下关闭按钮,对话框也会显示保存值。
有没有人有任何想法?
答案 0 :(得分:1)
你没有约束关闭事件
您是否尝试插入self.Bind(wx.EVT_CLOSE, self.OnQuit)
,其中OnQuit返回wx.ID_CANCEL