When I insert an image into a RichTextCtrl in wxPython and right click on it I get a menu which has a Picture button and opens a Picture properties dialog.
Is there a way I could substitute that dialog for a custom dialog that I make and get the image information into it?
Ideally I want to get the image data or even better the filename of the image. But looking at the xml that RichTextCtrl produces I do not think recovering the filename is possible. But if I could get the data I might be able to find the image.
The on_right_click method below is able to completely consume the event and prevent the menu from showing if you remove the Skip() but I was not able to find out how to get any information on what has been clicked on.
Here is an example code in python3 wxPython 4.1.0 that lets you insert a image and click on it and save the document in xml:
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
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
Thanks.
I did not manage to get this to work with the WriteImage. But I did workaround it by using a custom subclass of RichTextFieldTypeStandard that can hold an image which overrides methods: CanEditProperties, GetPropertiesMenuLabel and EditProperties.
By inserting a field like this in the text, the EditProperties method can access the field's name which can be a path to the image on disk.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.