1
我使用wxPython的創建GUI的對話框已關閉,我有以下的自定義對話框類:wxPython的:檢測是否
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
的存在,因爲即使按關閉按鈕,對話框也會保存值。
有沒有人有任何想法?
這很奇怪。當使用按鈕關閉對話框時,ShowModal應該返回它的id(wx.ID_OK)。當我關閉對話框時,我得到了wx.ID_CANCEL。您從ShowModal獲得的實際價值是多少? –
@PetrBlahos謝謝你的回答。我在調試器中得到了result = {int} 5100。我是否錯誤地形象化了ID? – Francesco
ID_OK是5100,ID_CANCEL是5101.很奇怪,當你關閉對話框時,你會得到ID_OK。你在哪個平臺和wx版本上? –