English 中文(简体)
Python 框架列表框
原标题:Python frames- List Box
  • 时间:2012-05-21 15:47:24
  •  标签:
  • wxpython

THe below code creates a frame with a list box to drag and drop. I would like to understand the code in the below line..

文件 和 TextDropTablet 是一个类。 此类被调用 2 个函数 - OnFileDrop & amp; OnTextDrop. 我的问题是如何调用 OnFileDrop 或 OnTextDrop 而不通过参数文件或文本 。

self.dt = FileAndTextDropTarget(self.OnFileDrop, self.OnTextDrop)

<强度 > 守则:

class DropTargetFrame(wx.Frame):
    pdb.set_trace()
    def __init__(self, parent, id=wx.ID_ANY, title="", 
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.DEFAULT_FRAME_STYLE,
                 name="DropTargetFrame"):
        super(DropTargetFrame, self).__init__(parent, id,
                                              title, pos,
                                              size, style,
                                              name)

        # Attributes
        choices = ["Drag and Drop Text or Files here",]
        self.list = wx.ListBox(self, 
                               choices=choices)
        self.dt = FileAndTextDropTarget(self.OnFileDrop,
                                        self.OnTextDrop)

        self.list.SetDropTarget(self.dt)

        # Setup
        self.CreateStatusBar()

    def OnFileDrop(self, files):
        self.PushStatusText("Files Dropped")
        for f in files:
            self.list.Append(f)

    def OnTextDrop(self, text):
        self.PushStatusText("Text Dropped")
        self.list.Append(text)


class FileAndTextDropTarget(wx.PyDropTarget):
    """Drop target capable of accepting dropped files and text"""
    def __init__(self, file_callback, text_callback):
        assert callable(file_callback)
        assert callable(text_callback)
        super(FileAndTextDropTarget, self).__init__()

        # Attributes
        self.fcallback = file_callback # Drop File Callback
        self.tcallback = text_callback # Drop Text Callback
        self._data = None
        self.txtdo = None
        self.filedo = None

        # Setup
        self.InitObjects()

    def InitObjects(self):
        """Initializes the text and file data objects"""
        self._data = wx.DataObjectComposite()
        self.txtdo = wx.TextDataObject()
        self.filedo = wx.FileDataObject()
        self._data.Add(self.txtdo, False)
        self._data.Add(self.filedo, True)
        self.SetDataObject(self._data)

    def OnData(self, x_cord, y_cord, drag_result):
        """Called by the framework when data is dropped on the target"""
        if self.GetData():
            data_format = self._data.GetReceivedFormat()
            if data_format.GetType() == wx.DF_FILENAME:
                self.fcallback(self.filedo.GetFilenames())
            else:
                self.tcallback(self.txtdo.GetText())

        return drag_result
问题回答

据我的理解,当您调用“ SetDropTablet” 时, 您告诉 wxPython 要在您的图形界面的这部分上监视鼠标事件。 TextDataObject 和类似的东西包含“ 智能”, 以便知道它支持什么格式, 当它到达那里时如何使用它( 在 wxPython 演示中) 。

所以当您选择了某个东西并开始拖动它时, wxPython 可以显示。 另请参阅 :





相关问题
Getting value of TextCtrl from a different wxPanel

I was trying to get my first wxWindow application to work and I ran into following difficulty: I create wxPanel and add a wxNotebook object to it. Then I add a page to notebook created from another ...

Displaying OpenCV iplimage data structures with wxPython

Here is my current code (language is Python): newFrameImage = cv.QueryFrame(webcam) newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage) wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY)....

Tree view GUI widget/gui library that can do multiple icons?

Screenshot I m looking to recreate this in Python; I can t find a library that seems to have what I need. Are there any GUI libraries that might possibly have this? - I have scoured wxWidgets (which ...

Alternatives to wx.lib.masked.NumCtrl

In a wxPython application I m developing I need a lot of input fields for numbers (integers and floats), so I tried using wx.lib.masked.NumCtrl, but my users now tell me that it s quite uncomfortable ...

热门标签