Browse Source

closes #710 - better menu dialogs

pull/746/head
Chris 3 years ago
parent
commit
6e39efa8fd
3 changed files with 81 additions and 2 deletions
  1. 31
      README.md
  2. 40
      gooey/gui/components/dialogs.py
  3. 12
      gooey/gui/components/menubar.py

31
README.md

@ -463,6 +463,7 @@ Currently, three types of menu options are supported:
* AboutDialog
* MessageDialog
* Link
* HtmlDialog
<img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/47251026-9ffc1400-d3e1-11e8-9095-982a6367561b.png" width="400" height="auto" align="right" />
@ -531,6 +532,36 @@ Example:
}
```
<img src="https://github.com/chriskiehl/GooeyImages/raw/images/docs/menus/html-dialog.PNG" width="400" height="auto" align="right" />
**HtmlDialog** gives you full control over what's displayed in the message dialog (bonus: people can copy/paste text from this one!).
Schema:
* `caption` - (_optional_) the caption in the title bar of the modal
* `html` - (_required_) the html you want displayed in the dialog. Note: only a small subset of HTML is supported. []See the WX docs for more info](https://wxpython.org/Phoenix/docs/html/html_overview.html).
Example:
```python
{
'type': 'HtmlDialog',
'menuTitle': 'Fancy Dialog!',
'caption': 'Demo of the HtmlDialog',
'html': '''
<body bgcolor="white">
<img src=/path/to/your/image.png" />
<h1>Hello world!</h1>
<p><font color="red">Lorem ipsum dolor sit amet, consectetur</font></p>
</body>
'''
}
```
**A full example:**
Two menu groups ("File" and "Help") with four menu items between them.

40
gooey/gui/components/dialogs.py

@ -0,0 +1,40 @@
import rewx.components as c
import wx
import wx.html2
from rewx import wsx, render
def _html_window(html):
return wsx(
[c.Block, {'orient': wx.VERTICAL, 'flag': wx.EXPAND},
[c.HtmlWindow, {'style': wx.TE_READONLY, 'flag': wx.EXPAND | wx.ALL,
'proportion': 1, 'value': html}]]
)
class HtmlDialog(wx.Dialog):
"""
A MessageDialog where the central contents are an HTML window
customizable by the user.
"""
def __init__(self, *args, **kwargs):
caption = kwargs.pop('caption', '')
html = kwargs.pop('html', '')
super(HtmlDialog, self).__init__(None, *args, **kwargs)
wx.InitAllImageHandlers()
self.SetTitle(caption)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(render(_html_window(html), self), 1, wx.EXPAND)
# in addition to creating the sizer, this actually attached
# a few common handlers which makes it feel more dialog-y. Thus
# it being done here rather than in rewx
btnSizer = self.CreateStdDialogButtonSizer(wx.OK)
sizer.Add(btnSizer, 0, wx.ALL | wx.EXPAND, 9)
self.SetSizer(sizer)
self.Layout()

12
gooey/gui/components/menubar.py

@ -4,6 +4,7 @@ from functools import partial
import wx
from gooey.gui import three_to_four
from gooey.gui.components.dialogs import HtmlDialog
class MenuBar(wx.MenuBar):
@ -38,7 +39,8 @@ class MenuBar(wx.MenuBar):
handlers = {
'Link': self.openBrowser,
'AboutDialog': self.spawnAboutDialog,
'MessageDialog': self.spawnMessageDialog
'MessageDialog': self.spawnMessageDialog,
'HtmlDialog': self.spawnHtmlDialog
}
f = handlers[item['type']]
return partial(f, item)
@ -59,6 +61,10 @@ class MenuBar(wx.MenuBar):
caption=item.get('caption', '')).ShowModal()
def spawnHtmlDialog(self, item, *args, **kwargs):
HtmlDialog(caption=item.get('caption', ''), html=item.get('html')).ShowModal()
def spawnAboutDialog(self, item, *args, **kwargs):
"""
Fill the wx.AboutBox with any relevant info the user provided
@ -78,4 +84,6 @@ class MenuBar(wx.MenuBar):
if field in item:
getattr(about, method)(item[field])
three_to_four.AboutBox(about)
three_to_four.AboutBox(about)
Loading…
Cancel
Save