wxPython - Using Save File Dialog
I use this snippet to create a dialog for saving file using wxPython library:
class ReportDialog(wx.Dialog):
def __init__(self, parent, id=-1, title='Report',
text='Select Type Of Report'):
size = (350, 200)
wx.Dialog.__init__(self, parent, id, title, size=size)
self.panel = ReportPanel(self)
sizer = wx.GridSizer(1, 1)
sizer.Add(self.panel, flag=wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL)
self.SetSizer(sizer)
self.Show()
REPORT_TYPES = ['All', 'Teachers', 'Students', 'Parents']
class ReportPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.setup_report_panel()
self.do_layout()
self.setup_bindings()
def setup_report_panel(self):
self.errmsg = wx.StaticText(self, label="")
self.errmsg.SetForegroundColour((255,0,0))
self.reporttypeselectbox = wx.ComboBox(self, choices=REPORT_TYPES, size=(300, 30),\
style=wx.CB_READONLY)
self.reporttypeselectbox.SetValue("Select Report Type")
self.okbutton = wx.Button(self, wx.ID_OK, ' OK ', size=(150, 30))
def do_layout(self):
v_sizer = wx.BoxSizer(wx.VERTICAL)
v_sizer.Add(self.errmsg)
selectbox_sizer = wx.BoxSizer()
selectbox_sizer.Add(self.reporttypeselectbox)
button_sizer = wx.BoxSizer()
button_sizer.Add(self.okbutton)
v_sizer.Add(selectbox_sizer)
v_sizer.Add((0,30), 1, wx.EXPAND)
v_sizer.Add(button_sizer)
self.SetSizer(v_sizer)
def setup_bindings(self):
self.okbutton.Bind(wx.EVT_BUTTON, self.on_save_file)
def get_report_type(self):
report_type = None
report_type = self.reporttypeselectbox.GetValue()
return report_type
def on_save_file(self, event):
report_type = self.get_report_type()
if report_type!='Select Report Type':
self.errmsg.SetLabel("")
dlg = wx.FileDialog(
self, message="Save file as ...",
defaultDir=".",
defaultFile="", wildcard="*.*", style=wx.SAVE
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
print path
fp = open(path, 'wb')
fp.write("bau bau")
fp.close()
dlg.Destroy()
else:
self.errmsg.SetLabel("Please select report type.")
class ReportDialog(wx.Dialog):
def __init__(self, parent, id=-1, title='Report',
text='Select Type Of Report'):
size = (350, 200)
wx.Dialog.__init__(self, parent, id, title, size=size)
self.panel = ReportPanel(self)
sizer = wx.GridSizer(1, 1)
sizer.Add(self.panel, flag=wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL)
self.SetSizer(sizer)
self.Show()
REPORT_TYPES = ['All', 'Teachers', 'Students', 'Parents']
class ReportPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.setup_report_panel()
self.do_layout()
self.setup_bindings()
def setup_report_panel(self):
self.errmsg = wx.StaticText(self, label="")
self.errmsg.SetForegroundColour((255,0,0))
self.reporttypeselectbox = wx.ComboBox(self, choices=REPORT_TYPES, size=(300, 30),\
style=wx.CB_READONLY)
self.reporttypeselectbox.SetValue("Select Report Type")
self.okbutton = wx.Button(self, wx.ID_OK, ' OK ', size=(150, 30))
def do_layout(self):
v_sizer = wx.BoxSizer(wx.VERTICAL)
v_sizer.Add(self.errmsg)
selectbox_sizer = wx.BoxSizer()
selectbox_sizer.Add(self.reporttypeselectbox)
button_sizer = wx.BoxSizer()
button_sizer.Add(self.okbutton)
v_sizer.Add(selectbox_sizer)
v_sizer.Add((0,30), 1, wx.EXPAND)
v_sizer.Add(button_sizer)
self.SetSizer(v_sizer)
def setup_bindings(self):
self.okbutton.Bind(wx.EVT_BUTTON, self.on_save_file)
def get_report_type(self):
report_type = None
report_type = self.reporttypeselectbox.GetValue()
return report_type
def on_save_file(self, event):
report_type = self.get_report_type()
if report_type!='Select Report Type':
self.errmsg.SetLabel("")
dlg = wx.FileDialog(
self, message="Save file as ...",
defaultDir=".",
defaultFile="", wildcard="*.*", style=wx.SAVE
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
print path
fp = open(path, 'wb')
fp.write("bau bau")
fp.close()
dlg.Destroy()
else:
self.errmsg.SetLabel("Please select report type.")
Comments
Post a Comment