1

How does one go about creating a custom dialog in wxPython? My intention is to create a dialog with 2 buttons called 'Gmail' and 'Outlook', instead of the 'OK' and 'CANCEL' or any other built-in options.

Does anyone know of a tutorial? Can this even be done?

Thanks in advance.

1 Answer 1

3

Yes you can create your own custom Dialogs.

import wx
from wx.lib import sized_controls

ID_GMAIL = wx.NewId()
ID_OUTLOOK = wx.NewId()


class CustomDialog(sized_controls.SizedDialog):

    def __init__(self, *args, **kwargs):
        super(CustomDialog, self).__init__(*args, **kwargs)
        pane = self.GetContentsPane()

        static_line = wx.StaticLine(pane, style=wx.LI_HORIZONTAL)
        static_line.SetSizerProps(border=(('all', 0)), expand=True)

        pane_btns = sized_controls.SizedPanel(pane)
        pane_btns.SetSizerType('horizontal')
        pane_btns.SetSizerProps(align='center')

        button_ok = wx.Button(pane_btns, ID_GMAIL, label='Gmail')
        button_ok.Bind(wx.EVT_BUTTON, self.on_button)

        button_ok = wx.Button(pane_btns, ID_OUTLOOK, label='Outlook')
        button_ok.Bind(wx.EVT_BUTTON, self.on_button)

        self.Fit()

    def on_button(self, event):
        if self.IsModal():
            self.EndModal(event.EventObject.Id)
        else:
            self.Close()


if __name__ == '__main__':
    app = wx.App(False)
    dlg = CustomDialog(None, title='Custom Dialog')
    result = dlg.ShowModal()
    if result == ID_GMAIL:
        print('Gmail')
    elif result == ID_OUTLOOK:
        print('Outlook')
    dlg.Destroy()
    app.MainLoop()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I have some questions though. What does 'self.GetContentsPane()' do? Furthermore, when I try to implement this under an event, nothing happens. No GUI at all. :/ I'll add my code with it implemented as an EDIT:
Never mind the 'GUI not working' part of my question. Was a foolish mistake from my part. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.