Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

wxpython - Is there a way to overwrite wx.EditableListBox edit button action?

I'm trying to implement a version of wx.avd.EditableListBox in which items edition is done with a DirDialog (in order to manage a list of pathes). Here is what I've done so far. I'm note sure it's the cleanest way to go, tbh. It almost works, but when a directory is selected and the dialog closes, the item in the list is still editable as a string. How could I block item edition from EditableListBox?

EDIT: some screenshot explaining the issue RE-EDIT : found out what's the problem. It's the call to event.Skip(). Removing it solves this issue.

Clicking on this button:

enter image description here

makes apprear a dir chooser (since it's what my code is intended to do):

enter image description here

But after selecting a directory, the item in the list is in edit mode as a string, like in the EditableListBox:

enter image description here

What I would like to do is avoid this and have it back to regular state, with the path that was selected in the Dir Dialog:

enter image description here

import wx, wx.adv
import wx.lib.inspection

class EditableDirBox(wx.adv.EditableListBox):
    def __init__(self, parent):
        super().__init__(parent)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLine)
        self.SetStrings([".", ".."])
        self.GetEditButton().Bind(wx.EVT_BUTTON, self.OnButtonEdit)
        self.selectedIndex=0
        
    def OnButtonEdit(self, event) :
        if self.selectedIndex != -1:
            lines = self.GetStrings()
            path = lines[self.selectedIndex]
            dialog = wx.DirDialog (None, "Choose input directory", "",
                                   wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)

            if dialog.ShowModal() == wx.ID_OK:
                dirname = dialog.GetPath()
                lines[self.selectedIndex] = dirname
                self.SetStrings(lines)
        event.Skip()

    def OnSelectLine(self, event) :
        lines=self.GetStrings()
        index = lines.index(event.GetText())
        self.selectedIndex = index
        print()
        event.Skip()


app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Hello World", size=(500,500))
panel = wx.Panel(frame)
sizer = wx.BoxSizer(wx.HORIZONTAL)
panel.SetSizer(sizer)

widget = EditableDirBox(panel)
sizer.Add(widget, flag=wx.ALIGN_CENTER)

frame.Show(True)     # Show the frame.
app.MainLoop()
question from:https://stackoverflow.com/questions/65861003/is-there-a-way-to-overwrite-wx-editablelistbox-edit-button-action

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...