ColourDialog object

I am using Windows 10, Python 3.8.3, wxPython 4.1.0.
(1) Is it possible to change the location of a ColourDialog object? SetPosition changes the position as returned by GetPosition, but when ShowModal is called the ColourDialog always appears at (0,0).
(2) Is it possible to cancel a ColourDialog object with the Esc key? Binding either the ColourDialog object or its parent to EVT_CHAR does not trap keystrokes while the ColourDialog is active.
Thanks in advance
Rob Cliffe

This answer applies here too. The native dialogs are not really a wx.Dialog despite the fact that they appear to derive from that class.

That said, it positioning itself at (0,0) is unexpected, and cancelling the dialog with ESC should be built-in. Does it also happen in the 4.1.1 release? Can you provide a small sample that demonstrates the problem?

[I got an error the first time I sent this so I’m sending it again. Apologies if you get a duplicate email.]

Thanks for replying, Robin, I’m sure you must be very busy.
I’m on Windows 10 running Python 3.8.3. I’ve upgraded wxPython to 4.1.1 and it seems to make no difference. Here is a simple demo program (also attached as an ASCII file):

import wx

class MyFrame(wx.Frame):

    def __init__(self, parent=None, id=-1):
        wx.Frame.__init__(self, parent, id, pos=wx.DefaultPosition, size=(700, 500), style = 0)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

    def OnLeftDown(self, event):
        dlg = wx.ColourDialog(None)
        wx.MessageBox('dlg.GetPosition() = ' + str(dlg.GetPosition()))
            # displays "dlg.GetPosition() = (100,100)"

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame()
        return True

app = MyApp()
# end of program

When I run the program and mouse-click on the frame, it shows the MessageBox and then the ColourDialog. Whatever I set its position to, the ColourDialog always appears in the same position, which is almost but not quite at the top left-hand corner of the frame (at a guess, at about position (10,0)). Also the ColourDialog does not shut down in response to the Esc key, and I have not so far found a way of making it do so.

Rob Cliffe (706 Bytes)

Ah, when you said it always appears at (0,0) I thought you meant it appeared at the top-left of the screen, but instead it looks like Windows is making it relative to the application’s main window instead. As for the ESC key, if a widget in the dialog has the keyboard focus then it does close with ESC as expected. There may be some interaction with the message dialog and/or the frame that is not letting the focus automatically transfer to the color dialog.

Ass mentioned before, these dialogs are not real wx.Dialogs but just a facade over the native APIs that create and show one of the system’s “common dialogs.” That has the advantage of a consistent and authentic look and behavior as dictated by the platform’s standards, and the disadvantage that most of the abilities of being a wx.Dialog are not available.

If you really need it to do something different than the platform standard then you can create your own wx.Dialog that uses the wx.lib.colourchooser widget.

Thanks again Robin.

Sorry I wasn’t clear. Yes, if I click on one (or more) of the colours or buttons within the colour dialog, press Escape, the colour dialog disappears. But it does not respond to the Escape key, even though the current colour is apparently selected, i.e. it has a box cursor round it. I have tried to programmatically simulate “clicking on the current colour” but nothing I have tried has worked, which I guess is not surprising given that it is not a real wx.Dialog object. Now I’m confused: I googled “wx.lib download” and found and downloaded the 4.1.1 version, but it contains .pdb files that I have no idea what to do with. What am I missing? Rob Cliffe


wx.lib is a subpackage of the wx package, so it needs to be imported. (colourchooser is another subpackage.)

See wx.lib.colourchooser — wxPython Phoenix 4.1.1 documentation and Phoenix/ at 64e5d863f7833f10df6a0fbcf3221a730562224b · wxWidgets/Phoenix · GitHub

OK, I got a PyColourChooser object going, but it doesn’t seem to be any use: it has no Confirm or Cancel options that would allow a selected colour to be exported. I realise it was not written by you, so I understand if you don’t want to comment on it. However: I have now written my own class, cribbing code from PyColourChooser, so: Problem Solved. Thanks for your pointers. Rob Cliffe

That is the way it is intended to be used. It just provides the widgets needed for selecting a color, and the rest of the dialog (or whatever context you want to use it in) is up to you.