Random Color Button for wxPython Window

  • Share this:

Code introduction


This code defines a function named xxx that creates a button in a wxPython window. Clicking this button changes the background color of the window to a randomly generated color.


Technology Stack : wxPython

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import wx
import random

def generate_random_color():
    return wx.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

def xxx(window, button_id):
    # This function creates a random color button that changes the background color of a wxPython window
    def on_button_click(event):
        # Generate a random color
        random_color = generate_random_color()
        # Set the background color of the window to the random color
        window.SetBackgroundColour(random_color)
        # Refresh the window to display the new color
        window.Refresh()
    
    # Create a new button with the specified ID
    button = wx.Button(window, button_id, label="Change Color")
    # Bind the button click event to the on_button_click function
    button.Bind(wx.EVT_BUTTON, on_button_click)                
              
Tags: