Creating a Dialog with Close and Message Buttons

  • Share this:

Code introduction


This function creates a dialog with two buttons: one to close the dialog and another to print a message.


Technology Stack : wxPython

Code Type : wxPython Dialog with Buttons

Code Difficulty : Intermediate


                
                    
import wx

def create_dialog_with_buttons(parent):
    # Create a dialog with two buttons: one to close the dialog and one to print a message
    dialog = wx.Dialog(parent, title="Hello Dialog")
    panel = wx.Panel(dialog)
    
    # Create a vertical sizer
    vbox = wx.BoxSizer(wx.VERTICAL)
    
    # Create two buttons
    close_button = wx.Button(panel, label="Close")
    message_button = wx.Button(panel, label="Print Message")
    
    # Bind button events
    close_button.Bind(wx.EVT_BUTTON, lambda event: dialog.Close())
    message_button.Bind(wx.EVT_BUTTON, lambda event: print("Message button clicked!"))
    
    # Add buttons to the sizer
    vbox.Add(close_button, 0, wx.ALL | wx.CENTER, 5)
    vbox.Add(message_button, 0, wx.ALL | wx.CENTER, 5)
    
    # Set the panel's sizer
    panel.SetSizer(vbox)
    
    # Fit the dialog to fit the contents
    dialog.Fit()
    return dialog                
              
Tags: