Jumat, 10 Oktober 2025

WXPYTHON - MessageBox

 




import wx


class MyFrame(wx.Frame):

    def __init__(self):

        super().__init__(parent=None, title='My wxPython App with Sizers')

        panel = wx.Panel(self)


        # Create a vertical box sizer

        vbox = wx.BoxSizer(wx.VERTICAL)


        # Add a StaticText

        text_label = wx.StaticText(panel, label="Enter your name:")

        vbox.Add(text_label, 0, wx.ALL | wx.EXPAND, 5) # Add with padding and expansion


        # Add a TextCtrl

        self.name_input = wx.TextCtrl(panel)

        vbox.Add(self.name_input, 0, wx.ALL | wx.EXPAND, 5)


        # Add a Button

        greet_button = wx.Button(panel, label="Greet")

        greet_button.Bind(wx.EVT_BUTTON, self.on_greet)

        vbox.Add(greet_button, 0, wx.ALL | wx.CENTER, 5)


        panel.SetSizer(vbox) # Set the sizer for the panel

        self.Layout() # Re-calculate layout

        self.Show()


    def on_greet(self, event):

        name = self.name_input.GetValue()

        if name:

            wx.MessageBox(f"Hello, {name}!", "Greeting", wx.OK | wx.ICON_INFORMATION)

        else:

            wx.MessageBox("Please enter your name.", "Warning", wx.OK | wx.ICON_WARNING)


if __name__ == '__main__':

    app = wx.App()

    frame = MyFrame()

    app.MainLoop()

Tidak ada komentar: