Home>

I'm currently writing a GUI in wxPython to display images and paths by drag and drop.
I've succeeded in displaying the path by drag and drop, but I don't know how to resize the image and display it on the panel. I would appreciate it if you could teach me what kind of code should be used.
I look forward to working with you.

Corresponding source code
#Image display.py
import wx
import wx.adv
class No1FileDropTarget (wx.FileDropTarget):
    def __init __ (self, window):
        wx.FileDropTarget.__init__ (self)
        self.window = window
    def OnDropFiles (self, x, y, filenames):
        #Display file path in text field
        for file in filenames:
            self.window.no1text.SetValue (file)
        return True
class No2FileDropTarget (wx.FileDropTarget):
    def __init __ (self, window):
        wx.FileDropTarget.__init__ (self)
        self.window = window
    def OnDropFiles (self, x, y, filenames):
        #Display file path in text field
        for file in filenames:
            self.window.no2text.SetValue (file)
        return True
class No3FileDropTarget (wx.FileDropTarget):
    def __init __ (self, window):
        wx.FileDropTarget.__init__ (self)
        self.window = window
    def OnDropFiles (self, x, y, filenames):
        #Display file path in text field
        for file in filenames:
            self.window.no3text.SetValue (file)
        return True
class No4FileDropTarget (wx.FileDropTarget):
    def __init __ (self, window):
        wx.FileDropTarget.__init__ (self)
        self.window = window
    def OnDropFiles (self, x, y, filenames):
        #Display file path in text field
        for file in filenames:
            self.window.no4text.SetValue (file)
        return True
class No5FileDropTarget (wx.FileDropTarget):
    def __init __ (self, window):
        wx.FileDropTarget.__init__ (self)
        self.window = window
    def OnDropFiles (self, x, y, filenames):
        #Display file path in text field
        for file in filenames:
            self.window.no5text.SetValue (file)
        return True
class No6FileDropTarget (wx.FileDropTarget):
    def __init __ (self, window):
        wx.FileDropTarget.__init__ (self)
        self.window = window
    def OnDropFiles (self, x, y, filenames):
        #Display file path in text field
        for file in filenames:self.window.no6text.SetValue (file)
        return True

class MyFrame (wx.Frame):
    def __init __ (self, * args, ** kwds):
        kwds ["style"] = kwds.get ("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__ (self, * args, ** kwds)
        self.SetSize ((1000, 850))
        self.datepicker_ctrl_1 = wx.adv.DatePickerCtrl (self, wx.ID_ANY)
        self.no1labeltext = wx.TextCtrl (self, wx.ID_ANY, "No1", style = wx.TE_CENTRE | wx.TE_READONLY)
        self.no2labeltext = wx.TextCtrl (self, wx.ID_ANY, "No2", style = wx.TE_CENTRE | wx.TE_READONLY)
        self.no3labeltext = wx.TextCtrl (self, wx.ID_ANY, "No3", style = wx.TE_CENTRE | wx.TE_READONLY)
        self.no1panel = wx.Panel (self, wx.ID_ANY, style = wx.BORDER_SIMPLE)
        self.no2panel = wx.Panel (self, wx.ID_ANY, style = wx.BORDER_SIMPLE)
        self.no3panel = wx.Panel (self, wx.ID_ANY, style = wx.BORDER_SIMPLE)
        self.no1text = wx.TextCtrl (self, wx.ID_ANY, "")
        self.no2text = wx.TextCtrl (self, wx.ID_ANY, "")
        self.no3text = wx.TextCtrl (self, wx.ID_ANY, "")
        self.no4labeltext = wx.TextCtrl (self, wx.ID_ANY, "No4", style = wx.TE_CENTRE | wx.TE_READONLY)
        self.no5labeltext = wx.TextCtrl (self, wx.ID_ANY, "No5", style = wx.TE_CENTRE | wx.TE_READONLY)
        self.no6labeltext = wx.TextCtrl (self, wx.ID_ANY, "No6", style = wx.TE_CENTRE | wx.TE_READONLY)
        self.no4panel = wx.Panel (self, wx.ID_ANY, style = wx.BORDER_SIMPLE)
        self.no5panel = wx.Panel (self, wx.ID_ANY, style = wx.BORDER_SIMPLE)
        self.no6panel = wx.Panel (self, wx.ID_ANY, style = wx.BORDER_SIMPLE)
        self.no4text = wx.TextCtrl (self, wx.ID_ANY, "")
        self.no5text = wx.TextCtrl (self, wx.ID_ANY, "")
        self.no6text = wx.TextCtrl (self, wx.ID_ANY, "")
        #Drop target settings
        dt1 = No1FileDropTarget (self)
        self.no1panel.SetDropTarget (dt1)
        dt2 = No2FileDropTarget (self)
        self.no2panel.SetDropTarget (dt2)
        dt3 = No3FileDropTarget (self)
        self.no3panel.SetDropTarget (dt3)
        dt4 = No4FileDropTarget (self)
        self.no4panel.SetDropTarget (dt4)
        dt5 = No5FileDropTarget (self)
        self.no5panel.SetDropTarget (dt5)
        dt6 = No6FileDropTarget (self)
        self.no6panel.SetDropTarget (dt6)
        self.__set_properties ()
        self.__do_layout ()
    def __set_properties (self):
        self.SetTitle ("frame")
        self.SetBackgroundColor (wx.Color (255, 252, 180))
        self.datepicker_ctrl_1.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no1labeltext.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no2labeltext.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no3labeltext.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no1panel.SetBackgroundColor (wx.Colour (255, 255, 255))
        self.no1panel.SetFocus ()
        self.no2panel.SetBackgroundColor (wx.Colour (255, 255, 255))
        self.no2panel.SetFocus ()
        self.no3panel.SetBackgroundColor (wx.Colour (255, 255, 255))
        self.no4labeltext.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no5labeltext.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no6labeltext.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        self.no4panel.SetBackgroundColor (wx.Colour (255, 255, 255))
        self.no5panel.SetBackgroundColor (wx.Colour (255, 255, 255))
        self.no6panel.SetBackgroundColor (wx.Colour (255, 255, 255))def __do_layout (self):
        sizer_1 = wx.BoxSizer (wx.VERTICAL)
        sizer_6 = wx.BoxSizer (wx.HORIZONTAL)
        grid_sizer_2 = wx.FlexGridSizer (3, 3, 0, 0)
        sizer_4 = wx.BoxSizer (wx.HORIZONTAL)
        grid_sizer_1 = wx.FlexGridSizer (3, 3, 0, 0)
        sizer_3 = wx.BoxSizer (wx.HORIZONTAL)
        sizer_1.Add ((20, 20), 0, 0, 0)
        sizer_3.Add ((20, 20), 0, 0, 0)
        datelabel = wx.StaticText (self, wx.ID_ANY, u "shooting date:")
        datelabel.SetFont (wx.Font (13, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        sizer_3.Add (datelabel, 0, 0, 0)
        sizer_3.Add (self.datepicker_ctrl_1, 0, 0, 0)
        sizer_1.Add (sizer_3, 1, wx.EXPAND, 0)
        sizer_1.Add ((20, 20), 0, 0, 0)
        grid_sizer_1.Add (self.no1labeltext, 0, wx.ALL | wx.EXPAND, 0)
        grid_sizer_1.Add (self.no2labeltext, 0, wx.ALL | wx.EXPAND, 0)
        grid_sizer_1.Add (self.no3labeltext, 0, wx.ALL | wx.EXPAND, 0)
        grid_sizer_1.Add (self.no1panel, 1, wx.EXPAND, 0)
        grid_sizer_1.Add (self.no2panel, 1, wx.EXPAND, 0)
        grid_sizer_1.Add (self.no3panel, 1, wx.EXPAND, 0)
        grid_sizer_1.Add (self.no1text, 0, wx.EXPAND, 0)
        grid_sizer_1.Add (self.no2text, 0, wx.EXPAND, 0)
        grid_sizer_1.Add (self.no3text, 0, wx.EXPAND, 0)
        grid_sizer_1.AddGrowableRow (1)
        grid_sizer_1.AddGrowableCol (0)
        grid_sizer_1.AddGrowableCol (1)
        grid_sizer_1.AddGrowableCol (2)
        sizer_4.Add (grid_sizer_1, 10, wx.ALL | wx.EXPAND, 1)
        sizer_1.Add (sizer_4, 7, wx.EXPAND, 0)
        grid_sizer_2.Add (self.no4labeltext, 0, wx.ALL | wx.EXPAND, 0)
        grid_sizer_2.Add (self.no5labeltext, 0, wx.ALL | wx.EXPAND, 0)
        grid_sizer_2.Add (self.no6labeltext, 0, wx.ALL | wx.EXPAND, 0)
        grid_sizer_2.Add (self.no4panel, 1, wx.EXPAND, 0)
        grid_sizer_2.Add (self.no5panel, 1, wx.EXPAND, 0)
        grid_sizer_2.Add (self.no6panel, 1, wx.EXPAND, 0)
        grid_sizer_2.Add (self.no4text, 0, wx.EXPAND, 0)
        grid_sizer_2.Add (self.no5text, 0, wx.EXPAND, 0)
        grid_sizer_2.Add (self.no6text, 0, wx.EXPAND, 0)
        grid_sizer_2.AddGrowableRow (1)
        grid_sizer_2.AddGrowableCol (0)
        grid_sizer_2.AddGrowableCol (1)
        grid_sizer_2.AddGrowableCol (2)
        sizer_6.Add (grid_sizer_2, 10, wx.ALL | wx.EXPAND, 1)
        sizer_1.Add (sizer_6, 7, wx.EXPAND, 0)
        sizer_1.Add ((20, 20), 0, 0, 0)
        self.SetSizer (sizer_1)
        self.Layout ()
class MyApp (wx.App):
    def OnInit (self):
        self.frame = MyFrame (None, wx.ID_ANY, "")
        self.SetTopWindow (self.frame)
        self.frame.Show ()
        return True
if __name__ == "__main__":
    app = MyApp (0)
    app.MainLoop ()
  • Answer # 1

    It seems that the file path income of the image is made, so

    If you just want to display an image
    Display area uses wx.StaticBitmap instead of wx.Panel

    Create image object with wx.Image

    If you need to resize, use the Scale method

    Convert to bitmap with wx.Bitmap

    Change the display image with the SetBitmap method of wx.StaticBitmap

    * If you do not resize, you can load it directly into wx.Bitmap.
    If you want to resize the image to match the resizing of the window
    The Image object read from the file is stored in a variable somewhere.


    It's not related to the question,
    Nos. 1 to 6 are in different classes,
    It is better to put them together in one class.

    If there are individual differences, the difference can be used as a variable and given from the outside.
    Make it a subclass. The common parts should be combined into one class.

    The 6 panels have nothing to do with the question, so
    First in one panel, regardless of drag and drop
    Let's simply create a program that displays images.

    The code to be posted when asking a question has also become compact,
    The problem will also be easier to understand.