|
8 | 8 | """ |
9 | 9 |
|
10 | 10 | import os |
| 11 | +import sys |
11 | 12 |
|
12 | 13 | import requests |
13 | 14 | import wx |
@@ -117,16 +118,27 @@ def _retrieve_image_from_web(self, image_file): |
117 | 118 |
|
118 | 119 | def _load_image(self, image_file): |
119 | 120 | """ Load image file as wx.Image object """ |
120 | | - if image_file.startswith('http'): |
121 | | - image = self._retrieve_image_from_web(image_file) |
122 | | - else: |
123 | | - image = PILImage.open(image_file) |
124 | | - |
125 | | - filename = self._process_image_file_name(image_file) |
126 | | - image.save(filename, 'PNG') |
127 | | - self.GetParent().GetParent().temp_file = filename |
128 | | - |
129 | | - return wx.Image(filename) |
| 121 | + frame = self.GetParent().GetParent() |
| 122 | + |
| 123 | + try: |
| 124 | + if image_file.startswith('http'): |
| 125 | + image = self._retrieve_image_from_web(image_file) |
| 126 | + else: |
| 127 | + image = PILImage.open(image_file) |
| 128 | + filename = self._process_image_file_name(image_file) |
| 129 | + image.save(filename, 'PNG') |
| 130 | + frame.temp_file = filename |
| 131 | + return wx.Image(filename) |
| 132 | + |
| 133 | + except: |
| 134 | + message = f'Failed to retrieve image from {image_file}' |
| 135 | + dlg = wx.MessageDialog(self, |
| 136 | + message=message, |
| 137 | + caption='Could not find image', |
| 138 | + style=wx.ICON_WARNING) |
| 139 | + dlg.ShowModal() |
| 140 | + dlg.Destroy() |
| 141 | + sys.exit('Image not found') |
130 | 142 |
|
131 | 143 |
|
132 | 144 |
|
@@ -449,16 +461,19 @@ class _Base(wx.Frame): |
449 | 461 |
|
450 | 462 | def __init__(self, image_file, *args, **kw): |
451 | 463 | wx.Frame.__init__(self, *args, **kw) |
| 464 | + |
| 465 | + # Must call before panel is instantiated so app can close if |
| 466 | + # panel instantiation fails |
452 | 467 | self.temp_file = None |
| 468 | + self.Bind(wx.EVT_CLOSE, self._on_exit) |
| 469 | + |
453 | 470 | panel = _BasePanel(image_file=image_file, parent=self, |
454 | 471 | id=wx.ID_ANY) |
455 | 472 |
|
456 | 473 | # Set frame size limits |
457 | 474 | self.SetMinSize((300,300)) |
458 | 475 | self.SetMaxSize(wx.DisplaySize()) |
459 | 476 |
|
460 | | - self.Bind(wx.EVT_CLOSE, self._on_exit) |
461 | | - |
462 | 477 | self.Layout() |
463 | 478 | self.Show() |
464 | 479 |
|
@@ -498,5 +513,5 @@ def main(image_file): |
498 | 513 | app.MainLoop() |
499 | 514 |
|
500 | 515 | if __name__ == '__main__': |
501 | | - #main('images/medium.jpg') |
502 | | - main('https://scitechdaily.com/images/image-of-the-planetary-nebula-NGC-5189.jpg') |
| 516 | + main('images/medium.jpg') |
| 517 | + #main('https://scitechdaily.com/images/image-of-the-planetary-nebula-NGC-5189.jpg') |
0 commit comments