Display picture through default viewer?

Hello,

Google didn’t help.

Is there a way to have wxPython display a picture through the default application, such as IrfanView on Windows?

Cheers,

import sys,os, wx

class ListBoxFrame(wx.Frame):
	def __init__(self, *args, **kwargs):  
		super().__init__(None, -1,title='Display picture')
		
		self.Centre()

		panel = wx.Panel(self)

		sizer = wx.BoxSizer(wx.VERTICAL)

		initial_files = ["c:\temp\file1.jpg", "c:\temp\file2.jpg", "c:\temp\file3.jpg"]
		self.lb1 = wx.ListBox(panel, choices=initial_files, style=(wx.LB_SINGLE | wx.LB_ALWAYS_SB))
		sizer.Add(self.lb1,1, wx.ALL | wx.EXPAND ,5)
		self.lb1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBoxDblClick)

		panel.SetSizer(sizer)
		panel.Layout()

	#TODO how to display picture with default application?
	def OnListBoxDblClick(self,event):
		sel = self.lb1.GetSelection()
		text = self.lb1.GetString(sel)
		self.statusbar.SetStatusText(text)
		pyperclip.copy(text)
	
app = wx.App()
ListBoxFrame().Show()
app.MainLoop()

I’m not sure whether there is a wx specific method, but this is what I’m using to open a folder.
It should also work for files.

import subprocess
if sys.platform=="win32":
    subprocess.call(['explorer', directory])
elif sys.platform=="darwin":
    subprocess.call(["open", directory])
else:
    directory = directory.replace(r'\\', "\\\\").replace('"', r'\"').replace(' ', r'\ ')
    subprocess.call(["xdg-open", directory])
1 Like

https://docs.wxpython.org/4.0.4/wx.FileType.html

Karsten

1 Like

This works (on Windows):

os.startfile("myfile.txt")

I can’t see how to mark this thread as solved.