Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created May 9, 2017 18:24
Show Gist options
  • Save driscollis/a9251062bb165df3954e696f8e557ef9 to your computer and use it in GitHub Desktop.
Save driscollis/a9251062bb165df3954e696f8e557ef9 to your computer and use it in GitHub Desktop.
import wx
class AttrDialog(wx.Dialog):
def __init__(self, xml_obj):
wx.Dialog.__init__(self, None, title='Add Attribute')
self.xml_obj = xml_obj
lbl_sizer = wx.BoxSizer(wx.HORIZONTAL)
attr_sizer = wx.BoxSizer(wx.HORIZONTAL)
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer = wx.BoxSizer(wx.VERTICAL)
attr_lbl = wx.StaticText(self, label='Attribute')
lbl_sizer.Add(attr_lbl, 0, wx.ALL, 5)
lbl_sizer.AddSpacer((140, -1))
value_lbl = wx.StaticText(self, label='Value')
lbl_sizer.Add(value_lbl, 0, wx.ALL, 5)
self.attr_text = wx.TextCtrl(self)
attr_sizer.Add(self.attr_text, 1, wx.ALL, 5)
self.value_text = wx.TextCtrl(self)
attr_sizer.Add(self.value_text, 1, wx.ALL, 5)
save_btn = wx.Button(self, label='Save')
save_btn.Bind(wx.EVT_BUTTON, self.on_save)
btn_sizer.Add(save_btn, 0, wx.ALL|wx.CENTER, 5)
cancel_btn = wx.Button(self, label='Cancel')
cancel_btn.Bind(wx.EVT_BUTTON, self.on_cancel)
btn_sizer.Add(cancel_btn, 0, wx.ALL|wx.CENTER, 5)
main_sizer.Add(btn_sizer, 0, wx.CENTER)
main_sizer.Add(lbl_sizer)
main_sizer.Add(attr_sizer, 0, wx.EXPAND)
main_sizer.Add(btn_sizer, 0, wx.CENTER)
self.SetSizer(main_sizer)
self.ShowModal()
def on_cancel(self, event):
"""
Event handler that is called when the Cancel button is
pressed.
Will destroy the dialog
"""
self.Destroy()
def on_save(self, event):
"""
Event handler that is called when the Save button is
pressed.
Updates the XML object with the new node element and
tells the UI to update to display the new element
before destroying the dialog
"""
attr = self.attr_text.GetValue()
value = self.value_text.GetValue()
if attr or value:
self.xml_obj.attrib[attr] = value
self.Destroy()
import attr_dialog
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Test')
panel = wx.Panel(self)
btn = wx.Button(panel, label='Attr')
btn.Bind(wx.EVT_BUTTON, self.on_attr)
def on_attr(self, event):
dlg = attr_dialog.AttrDialog(xml_obj='')
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment