Benny Lutin
Benny Lutin

Reputation: 25

Show file path using WX

I am trying to show the file path after it been dropped in NewText label but it only show me the path on the top window... what can i do to make it work.

This is my code I am trying to show path where is 'Path will be' staticText

import wx            

class MyFileDropTarget(wx.FileDropTarget):
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)
        self.window = window            

    def OnDropFiles(self, x, y, filename):
        #self.window.SetInsertionPointEnd()
        TempTxt = filename
        print(TempTxt)
        print(type(TempTxt))
        TempTxt = str(TempTxt)
        print(type(TempTxt))
        self.window.LabelTextUpdate(TempTxt)
        return True            


class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title)            
        self.InitUI()
        self.Center()            

    def InitUI(self):            
        panel = wx.Panel(self)
        FileDrTr = MyFileDropTarget(self)            
        font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)    
        font.SetPointSize(9)            
        verBox = wx.BoxSizer(wx.VERTICAL)            
        horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
        TextLabel = wx.StaticText(panel, label = 'Drop file hear')
        TextLabel.SetFont(font)
        horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
        DropePlace = wx.TextCtrl(panel)
        DropePlace.SetDropTarget(FileDrTr)            
        horBoxOne.Add(DropePlace, proportion=1)
        verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)            
        verBox.Add((-1, 10))           
        horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
        NewText = wx.StaticText(panel, label = 'Path will be')
        horBoxTwo.Add(NewText, flag=wx.RIGHT, border=5)
        verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)            
        panel.SetSizer(verBox)            

    def LabelTextUpdate(self, txt):            
        print(txt)
        print(type(txt))
        NewText = wx.StaticText(self, label = txt)           


def main():
    app = wx.App()
    ex = Example(None, title = 'drope and see file path')
    ex.Show()
    app.MainLoop()        

if __name__ == '__main__':
    main()

Upvotes: 1

Views: 352

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

Because we are reacting to an event a file drop, it makes sense to assign the processing to an event as well.
Introducing wx.lib.newevent:
here we create our own event (EVT_DROP_EVENT) and assign it a label (drop_event).

drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()

We now have an event to bind to and we can assign it a callback routine, in this case LabelTextUpdate.

self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)

When the file is dropped, we set the event and fire it off:

    evt = drop_event(data=TempTxt)
    wx.PostEvent(self.obj,evt)

The callback routine is called due to binding to the EVT_DROP_EVENT and in this routine we process the data.

Hope that's clear (see below).

import wx

import wx.lib.newevent
drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()

class MyFileDropTarget(wx.FileDropTarget):
    def __init__(self, obj):
        wx.FileDropTarget.__init__(self)
        self.obj = obj

    def OnDropFiles(self, x, y, filename):
        #filename is a list of 1 or more files
        #here we are assuming only 1 file
        TempTxt = filename[0]
        evt = drop_event(data=TempTxt)
        wx.PostEvent(self.obj,evt)

        return True


class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title)
        self.InitUI()
        self.Center()

    def InitUI(self):
        panel = wx.Panel(self)
        FileDrTr = MyFileDropTarget(self)
        font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(9)
        verBox = wx.BoxSizer(wx.VERTICAL)
        horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
        TextLabel = wx.StaticText(panel, label = 'Drop file here')
        TextLabel.SetFont(font)
        horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
        DropePlace = wx.TextCtrl(panel)
        DropePlace.SetDropTarget(FileDrTr)

        #Bind the drop event listener
        self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)


        horBoxOne.Add(DropePlace, proportion=1)
        verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        verBox.Add((-1, 10))
        horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
        self.NewText = wx.StaticText(panel, label = 'Path will be')
        horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)
        verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        panel.SetSizer(verBox)

    def LabelTextUpdate(self, event):
        txt = event.data
        self.NewText.SetLabel(txt)


def main():
    app = wx.App()
    ex = Example(None, title = 'drop and see file path')
    ex.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

enter image description here

Upvotes: 0

Petr Blahos
Petr Blahos

Reputation: 2433

In your InitUI you have created some widgets, one of them is the NewText, which I suppose it the one you want to use for displaying the file path. You have placed it in the window using a sizer. So far so good.

In LabelTextUpdate you create a completely new widget and set its text. The widget is placed inside your window, probably at the top-left coordinates, therefore it looks as if you changed the text of the window. But in fact you placed a new widgets.

In fact, you want to change the NewText's label. You must keep the reference to the NewText within the object, so in InitUI you will do (notice the self):

    self.NewText = wx.StaticText(panel, label = 'Path will be')
    horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)

And LabelTextUpdate will use this member variable:

def LabelTextUpdate(self, txt):            
    self.NewText.SetLabel(txt)           

That's it.

Upvotes: 1

Related Questions