Reputation: 197
I want to have a time based file name when saving a log file. When I try to set the default filename, the dialog comes up with a blank filename, as shown below. I have tried the equivalent positional function call and it also does not work.
Any idea how to get wx.FileDialog() to set the filename so that all you need to do is to click "Save" to save the file with the default name?
Using the following versions under Windows 10:
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)]
wx.version: 4.1.1 msw (phoenix) wxWidgets 3.1.5
Code that doesn't work:
def OnSave(self, event):
default_file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ".log"
dlg = wx.FileDialog(self.frame, message = "Save Log Contents",
defaultDir = os.getcwd(),
defaultFile = default_file,
wildcard = "Log files (*.log)|*.log",
style = wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_CANCEL:
dlg.Destroy()
return
file_path = dlg.GetPath()
self.window.tc.AppendText("%s Saving log to %s\n" % (datetime.datetime.now(), file_path))
self.window.tc.SaveFile(file_path)
dlg.Destroy()
return True
Upvotes: 0
Views: 283
Reputation: 4127
You can't have :
in your file name. Change this line:
default_file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ".log"
To this:
default_file = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S') + ".log"
And you will get much better results.
How I discovered this (it wasn't immediate):
I didn't spot this until I tried setting the default file name with
dlg.SetFilename(default_file)
prior to opening the dialog, and I got an error message. Then I changed the default filename to 'junk.log' and it worked okay. Then I printed out the default_file to the console and discovered the issue.
Upvotes: 2
Reputation: 22443
Sadly, all I can do is confirm that under Linux wx 4.1.1, that it works as expected.
import wx
import datetime
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.select_button = wx.Button(panel, label="Select files")
self.convert_and_merge_button = wx.Button(panel, label="Merge files")
sizer.Add(self.select_button, 0, 0, 0)
sizer.Add(self.convert_and_merge_button, 0, 0, 0)
self.select_button.Bind(wx.EVT_BUTTON, self.pick_files)
self.convert_and_merge_button.Bind(wx.EVT_BUTTON, self.convert_and_merge)
self.load_options = "Pdf and Image Files |*.pdf;*.gif;*.bmp;*.tif;*.png;"
self.save_options = "Pdf Files |*.pdf;*.log;"
self.convert_and_merge_button.Enable(False)
panel.SetSizer(sizer)
def pick_files(self, event):
with wx.FileDialog(self, "Pick files", wildcard=self.load_options,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
self.files_list = fileDialog.GetPaths()
self.convert_and_merge_button.Enable()
def convert_and_merge(self, event):
default_file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ".log"
print (default_file)
with wx.FileDialog(self, "Convert and merge", wildcard=self.save_options,
defaultDir = "/home/rolf",
defaultFile=default_file,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
# pass parameters to Converter class
merge_file = fileDialog.GetPath()
#Test that the output file is not in the input list
if merge_file in self.files_list:
wx.MessageBox('The chosen output file is in the input files\n Choose another file', 'Error', wx.OK | wx.ICON_INFORMATION)
return
self.converter(self.files_list, merge_file)
def converter(self, files_list, merge_file):
print ("Merging:\n"+str(files_list)+"\n into\n"+str(merge_file))
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'A test dialog')
frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
Upvotes: 1