Hi,
I am using python3 wxPython 4.1.0. When I insert an image into RichTextCtrl and click on it I can either bind a method to the left click or right click or let RichTextCtrl take care of the event. If I let RichTextCtrl take care of it, there will be a menu button in the context menu allowing me to edit some of the image properties.
What I would like to be able to do is show my own custom dialog either with left/right click or instead of the properties dialog.
I have a list of objects representing the images that can be inserted into the RichTextCtrl and I would like to be able to find which of them has been clicked on by the user in the RichTextCtrl.
Unfortunately I was not able to find out how to get any information about the image that was clicked.
Is there any way how to do that?
While I have written some code with wxpython I am still learning and I am either missing something or this is beyond my current knowledge unfortunately.
Here is a quick example that lets you insert an image and click on it:
import wx
import wx.richtext as rt
class RichTextFrame(wx.Frame):
def __init__(self, *args, **kw):
wx.Frame.__init__(self, *args, **kw)
self.make_menu_bar()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER)
self.sizer.Add(self.rtc, 1, flag=wx.EXPAND)
self.SetSizer(self.sizer)
rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="XML", ext="xml", type=99))
self.Bind(rt.EVT_RICHTEXT_RIGHT_CLICK, self.on_right_click)
def on_right_click(self, evt):
# This catches right clicks
evt.Skip()
def on_file_save_as(self, evt):
wildcard, types = rt.RichTextBuffer.GetExtWildcard(save=True)
dlg = wx.FileDialog(self, "Choose a filename",
wildcard=wildcard,
style=wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
if path:
file_type = types[dlg.GetFilterIndex()]
ext = rt.RichTextBuffer.FindHandlerByType(file_type).GetExtension()
if not path.endswith(ext):
path += '.' + ext
self.rtc.SaveFile(path, file_type)
dlg.Destroy()
def on_insert_image(self, evt):
# wx.Image('image.png'), wx.BITMAP_TYPE_PNG)
self.rtc.WriteImage(wx.Image(200, 200))
def on_file_exit(self, evt):
self.Close(True)
def forward_event(self, evt):
self.rtc.ProcessEvent(evt)
def make_menu_bar(self):
def do_bind(item, handler, update_ui=None):
self.Bind(wx.EVT_MENU, handler, item)
if update_ui is not None:
self.Bind(wx.EVT_UPDATE_UI, update_ui, item)
file_menu = wx.Menu()
do_bind(file_menu.Append(-1, "&Save\tCtrl+S", "Save a file"),
self.on_file_save_as)
do_bind(file_menu.Append(-1, "E&xit\tCtrl+Q", "Quit this program"),
self.on_file_exit)
edit_menu = wx.Menu()
do_bind(edit_menu.Append(-1, 'Insert image\tCtrl+i'), self.on_insert_image)
mb = wx.MenuBar()
mb.Append(file_menu, "&File")
mb.Append(edit_menu, "&Edit")
self.SetMenuBar(mb)
class MyApp(wx.App):
"""
Main class for running the gui
"""
def __init__(self):
wx.App.__init__(self)
self.frame = None
def OnInit(self):
self.frame = RichTextFrame(None, -1, "RichTextCtrl", size=(900, 500), style=wx.DEFAULT_FRAME_STYLE)
self.SetTopWindow(self.frame)
self.frame.Show()
return True
def OnExit(self):
print('_Done_')
return True