Random Button Creator with Text and Color

  • Share this:

Code introduction


This function creates a random button with random text and color on the specified panel and binds an event handler to it. When the button is clicked, it prints the button's text to the console.


Technology Stack : wxPython

Code Type : Function

Code Difficulty : Intermediate


                
                    
import wx
import random

def create_random_button(panel):
    # Create a random button with random text and color
    button_text = random.choice(['Start', 'Stop', 'Exit', 'Continue', 'Pause'])
    button_color = random.choice(['RED', 'GREEN', 'BLUE', 'YELLOW', 'BLACK'])
    button = wx.Button(panel, label=button_text, color=eval(button_color))
    button.Bind(wx.EVT_BUTTON, lambda event: print(f"{button_text} button clicked"))
    return button                
              
Tags: