Create a dialog box and add text entry box and colour to the dialog box and text using wxpyton

Dani AI

Generated

The request from was for a wxPython dialog that contains a text-entry box and lets the dialog/text be colored. As suggested, posting a minimal working example is useful for comparison; below is a compact, self-contained example that demonstrates a modal wx.Dialog with a wx.TextCtrl, a button that opens wx.ColourDialog, and how to apply the chosen color to the text control (optionally to the dialog background).

The example uses wx.ColourDialog to let the user pick a color, retrieves the color with GetColourData().GetColour(), then calls SetForegroundColour on the text control and Refresh() so the change is visible. Layout is handled by sizers and SetSizerAndFit so the dialog sizes correctly. The dialog returns wx.ID_OK when accepted, and the entered text is available via GetValue().

import wx

class ColorDialog(wx.Dialog):
    def __init__(self, parent=None):
        super().__init__(parent, title="Text + Color Dialog")
        sizer = wx.BoxSizer(wx.VERTICAL)

        self.text = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
        sizer.Add(self.text, 0, wx.EXPAND | wx.ALL, 10)

        pick = wx.Button(self, label="Choose Text Color")
        pick.Bind(wx.EVT_BUTTON, self.on_pick_color)
        sizer.Add(pick, 0, wx.ALIGN_RIGHT | wx.ALL, 10)

        btns = self.CreateButtonSizer(wx.OK | wx.CANCEL)
        sizer.Add(btns, 0, wx.ALIGN_RIGHT | wx.ALL, 10)

        self.SetSizerAndFit(sizer)

    def on_pick_color(self, event):
        data = wx.ColourData()
        dlg = wx.ColourDialog(self, data)
        if dlg.ShowModal() == wx.ID_OK:
            col = dlg.GetColourData().GetColour()
            self.text.SetForegroundColour(col)
            # optional: self.SetBackgroundColour(col)
            self.Refresh()
        dlg.Destroy()

if __name__ == "__main__":
    app = wx.App(False)
    dlg = ColorDialog()
    if dlg.ShowModal() == wx.ID_OK:
        print("Entered:", dlg.text.GetValue())
    dlg.Destroy()
    app.MainLoop()

Troubleshooting notes: ensure a wx.App is created before showing the dialog; call Refresh() after changing colours; confirm the installed wxPython version (installable from PyPI with pip install -U wxPython). Further class/method details are in the official docs: wxPython docs.

Recommended Answers

All 2 Replies

That sounds like your homework. Try sharing what you have so far and what you think is broken.

commented: import wx class my_window(wx.Frame): def __init__(self, parent, id): #error __int__ wx.Frame.__init__(self,parent,id,' Search', size=(30 +0

Sadly, replies via comments like that is limited. Try a full reply with your code as well as point out what line you think is the problem as well as what the problem is. Unlike some places, we don't complete assignments and homework but will look at issues and share how one might solve them so that in the end you can claim you did the work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.