You can download this code by clicking the button below.
This code is now available for download.
This function creates a wxPython window with a button. Clicking the button generates a random color, which is applied to the button and the window title.
Technology Stack : wxPython
Code Type : Graphical User Interface (GUI)
Code Difficulty : Intermediate
import wx
import random
def generate_random_color():
def random_color():
return '#{:02x}{:02x}{:02x}'.format(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
frame = wx.Frame(None, title="Random Color Generator", size=(200, 100))
panel = wx.Panel(frame)
color_button = wx.Button(panel, label="Generate Color", pos=(50, 30))
def on_color_click(event):
color_button.SetBackgroundColour(random_color())
frame.SetTitle("Color: " + color_button.GetBackgroundColour().GetAsString())
color_button.Bind(wx.EVT_BUTTON, on_color_click)
frame.Show()
app = wx.App(False)
frame.Show()
app.MainLoop()