<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # wxPython 中的拖放 > 原文: [http://zetcode.com/wxpython/draganddrop/](http://zetcode.com/wxpython/draganddrop/) 在計算機圖形用戶界面中,拖放是單擊虛擬對象并將其拖動到其他位置或另一個虛擬對象上的動作(或支持以下動作)。 通常,它可用于調用多種動作,或在兩個抽象對象之間創建各種類型的關聯。 拖放操作使您可以直觀地完成復雜的事情。 在拖放操作中,我們將一些數據從數據源拖動到數據目標。 所以我們必須有: * 一些數據 * 數據來源 * 數據目標 在 wxPython 中,我們有兩個預定義的數據目標:`wx.TextDropTarget`和`wx.FileDropTarget`。 ## `wx.TextDropTarget` `wx.TextDropTarget`是用于處理文本數據的預定義放置目標。 `dragdrop_text.py` ```py #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ ZetCode wxPython tutorial In this example, we drag and drop text data. author: Jan Bodnar website: www.zetcode.com last modified: May 2018 """ from pathlib import Path import os import wx class MyTextDropTarget(wx.TextDropTarget): def __init__(self, object): wx.TextDropTarget.__init__(self) self.object = object def OnDropText(self, x, y, data): self.object.InsertItem(0, data) return True class Example(wx.Frame): def __init__(self, *args, **kw): super(Example, self).__init__(*args, **kw) self.InitUI() def InitUI(self): splitter1 = wx.SplitterWindow(self, style=wx.SP_3D) splitter2 = wx.SplitterWindow(splitter1, style=wx.SP_3D) home_dir = str(Path.home()) self.dirWid = wx.GenericDirCtrl(splitter1, dir=home_dir, style=wx.DIRCTRL_DIR_ONLY) self.lc1 = wx.ListCtrl(splitter2, style=wx.LC_LIST) self.lc2 = wx.ListCtrl(splitter2, style=wx.LC_LIST) dt = MyTextDropTarget(self.lc2) self.lc2.SetDropTarget(dt) self.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit, id=self.lc1.GetId()) tree = self.dirWid.GetTreeCtrl() splitter2.SplitHorizontally(self.lc1, self.lc2, 150) splitter1.SplitVertically(self.dirWid, splitter2, 200) self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelect, id=tree.GetId()) self.OnSelect(0) self.SetTitle('Drag and drop text') self.Centre() def OnSelect(self, event): list = os.listdir(self.dirWid.GetPath()) self.lc1.ClearAll() self.lc2.ClearAll() for i in range(len(list)): if list[i][0] != '.': self.lc1.InsertItem(0, list[i]) def OnDragInit(self, event): text = self.lc1.GetItemText(event.GetIndex()) tdo = wx.TextDataObject(text) tds = wx.DropSource(self.lc1) tds.SetData(tdo) tds.DoDragDrop(True) def main(): app = wx.App() ex = Example(None) ex.Show() app.MainLoop() if __name__ == '__main__': main() ``` 在示例中,我們在`wx.GenericDirCtrl`中顯示了一個文件系統。 所選目錄的內容顯示在右上方的列表控件中。 可以將文件名拖放到右下角的列表控件中。 ```py def OnDropText(self, x, y, data): self.object.InsertItem(0, data) return True ``` 當我們將文本數據放到目標上時,該數據將通過`InsertItem()`方法插入到列表控件中。 ```py dt = MyTextDropTarget(self.lc2) self.lc2.SetDropTarget(dt) ``` 創建放置目標。 我們使用`SetDropTarget()`方法將放置目標設置為第二個列表控件。 ```py self.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit, id=self.lc1.GetId()) ``` 當拖動操作開始時,將調用`OnDragInit()`方法。 ```py def OnDragInit(self, event): text = self.lc1.GetItemText(event.GetIndex()) tdo = wx.TextDataObject(text) tds = wx.DropSource(self.lc1) ... ``` 在`OnDragInit()`方法中,我們創建一個`wx.TextDataObject`,其中包含我們的文本數據。 從第一個列表控件創建放置源。 ```py tds.SetData(tdo) tds.DoDragDrop(True) ``` 我們使用`SetData()`將數據設置到放置源,并使用`DoDragDrop()`啟動拖放操作。 ## `wx.FileDropTarget` `wx.FileDropTarget`是接受文件的放置目標,這些文件是從文件管理器中拖動的。 `dragdrop_file.py` ```py #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ ZetCode wxPython tutorial In this example, we drag and drop files. author: Jan Bodnar website: www.zetcode.com last modified: May 2018 """ import wx class FileDrop(wx.FileDropTarget): def __init__(self, window): wx.FileDropTarget.__init__(self) self.window = window def OnDropFiles(self, x, y, filenames): for name in filenames: try: file = open(name, 'r') text = file.read() self.window.WriteText(text) except IOError as error: msg = "Error opening file\n {}".format(str(error)) dlg = wx.MessageDialog(None, msg) dlg.ShowModal() return False except UnicodeDecodeError as error: msg = "Cannot open non ascii files\n {}".format(str(error)) dlg = wx.MessageDialog(None, msg) dlg.ShowModal() return False finally: file.close() return True class Example(wx.Frame): def __init__(self, *args, **kw): super(Example, self).__init__(*args, **kw) self.InitUI() def InitUI(self): self.text = wx.TextCtrl(self, style = wx.TE_MULTILINE) dt = FileDrop(self.text) self.text.SetDropTarget(dt) self.SetTitle('File drag and drop') self.Centre() def main(): app = wx.App() ex = Example(None) ex.Show() app.MainLoop() if __name__ == '__main__': main() ``` 該示例創建一個簡單的`wx.TextCtrl`。 我們可以將文本文件從文件管理器拖到控件中。 ```py def OnDropFiles(self, x, y, filenames): for name in filenames: ... ``` 我們可以一次拖放多個文件。 ```py try: file = open(name, 'r') text = file.read() self.window.WriteText(text) ``` 我們以只讀模式打開文件,獲取其內容,然后將內容寫入文本控件窗口。 ```py except IOError as error: msg = "Error opening file\n {}".format(str(error)) dlg = wx.MessageDialog(None, msg) dlg.ShowModal() return False ``` 如果出現輸入/輸出錯誤,我們將顯示一個消息對話框并終止操作。 ```py self.text = wx.TextCtrl(self, style = wx.TE_MULTILINE) dt = FileDrop(self.text) self.text.SetDropTarget(dt) ``` `wx.TextCtrl`是放置目標。 在本章中,我們使用了 wxPython 中的拖放操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看