driscollis / applications_with_wxpython

Code examples for the book, Creating GUI Application with wxPython
https://www.blog.pythonlibrary.org
117 stars 26 forks source link

Chapter 7 - Code in book does not match repo #14

Closed cmcknight closed 5 years ago

cmcknight commented 5 years ago

In the book, the on_create_archive method reads:

Speaking of which, you should write that method next:

 1 def on_create_archive(self, event):
 2     if not self.archive_olv.GetObjects():
 3         self.show_message('No files / folders to archive',
 4                           'Error', wx.ICON_ERROR)
 5         return
 6 
 7     dlg = wx.DirDialog(self, "Choose a directory:",
 8                        style=wx.DD_DEFAULT_STYLE,
 9                        defaultPath=self.current_directory)
10 
11     if dlg.ShowModal() == wx.ID_OK:
12         path = dlg.GetPath()
13         self.current_directory = path
14         archive_filename = self.archive_filename.GetValue()
15         archive_type = self.archive_types.GetValue()
16 
17         full_save_path = pathlib.Path(
18             path, '{filename}.{type}'.format(
19                 filename=archive_filename,
20                 type=archive_type.lower()
21             ))
22         controller.create_archive(
23             full_save_path,
24             self.archive_olv.GetObjects(),
25             archive_type)
26         message = f'Archive created at {full_save_path}'
27         self.show_message(message, 'Archive Created',
28                           wx.ICON_INFORMATION)
29     dlg.Destroy()

but the repo code reads:

    def on_create_archive(self, event):
        if not self.archive_olv.GetObjects():
            self.show_message('No files / folders to archive',
                              'Error', wx.ICON_ERROR)
            return

        dlg = wx.DirDialog(self, "Choose a directory:",
                           style=wx.DD_DEFAULT_STYLE,
                           defaultPath=self.current_directory)
        archive_filename = self.archive_filename.GetValue()
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.current_directory = path
            archive_type = self.archive_types.GetValue()

            full_save_path = pathlib.Path(
                path, '{filename}.{type}'.format(
                    filename=archive_filename,
                    type=archive_type.lower()
                ))
            controller.create_archive(
                full_save_path,
                self.archive_olv.GetObjects(),
                archive_type)
            message = f'Archive created at {full_save_path}'
            self.show_message(message, 'Archive Created',
                              wx.ICON_INFORMATION)
        dlg.Destroy()

In the book, the archive_filename is assigned after the dialog has its ShowModal() method invoked. In the repo, the archive_filename is assigned after the dialog is created.

driscollis commented 5 years ago

Okay. Thanks for the feedback. I'm actually going to change this example to use Python's with statement because this is actually the old way of creating dialogs which I am trying not to do in this book. But it's hard to do that as I'm been doing it the old way for so long.