wxPython Drag and Drop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndoe
    New Member
    • Sep 2007
    • 12

    #1

    wxPython Drag and Drop

    i need a source code in python for example to make application with drag and drop fiture,i can't understand learning from wxPython2.6 docs,Thank for help
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    From wxPyton in Action © Noel Rappin and Robin Dunn[CODE=python]import wx

    class DragController( wx.Control):
    """
    Just a little control to handle dragging the text from a text
    control. We use a separate control so as to not interfere with
    the native drag-select functionality of the native text control.
    """
    def __init__(self, parent, source, size=(25,25)):
    wx.Control.__in it__(self, parent, -1, size=size,
    style=wx.SIMPLE _BORDER)
    self.source = source
    self.SetMinSize (size)
    self.Bind(wx.EV T_PAINT, self.OnPaint)
    self.Bind(wx.EV T_LEFT_DOWN, self.OnLeftDown )

    def OnPaint(self, evt):
    # draw a simple arrow
    dc = wx.BufferedPain tDC(self)
    dc.SetBackgroun d(wx.Brush(self .GetBackgroundC olour()))
    dc.Clear()
    w, h = dc.GetSize()
    y = h/2
    dc.SetPen(wx.Pe n("dark blue", 2))
    dc.DrawLine(w/8, y, w-w/8, y)
    dc.DrawLine(w-w/8, y, w/2, h/4)
    dc.DrawLine(w-w/8, y, w/2, 3*h/4)

    def OnLeftDown(self , evt):
    text = self.source.Get Value()
    data = wx.TextDataObje ct(text)
    dropSource = wx.DropSource(s elf)
    dropSource.SetD ata(data)
    result = dropSource.DoDr agDrop(wx.Drag_ AllowMove)

    # if the user wants to move the data then we should delete it
    # from the source
    if result == wx.DragMove:
    self.source.Set Value("")

    class MyFrame(wx.Fram e):
    def __init__(self):
    wx.Frame.__init __(self, None, title="Drop Source")
    p = wx.Panel(self)

    # create the controls
    label1 = wx.StaticText(p , -1, "Put some text in this control:")
    label2 = wx.StaticText(p , -1,
    "Then drag from the neighboring bitmap and\n"
    "drop in an application that accepts dropped\n"
    "text, such as MS Word.")
    text = wx.TextCtrl(p, -1, "Some text")
    dragctl = DragController( p, text)

    # setup the layout with sizers
    sizer = wx.BoxSizer(wx. VERTICAL)
    sizer.Add(label 1, 0, wx.ALL, 5)
    hrow = wx.BoxSizer(wx. HORIZONTAL)
    hrow.Add(text, 1, wx.RIGHT, 5)
    hrow.Add(dragct l, 0)
    sizer.Add(hrow, 0, wx.EXPAND|wx.AL L, 5)
    sizer.Add(label 2, 0, wx.ALL, 5)
    p.SetSizer(size r)
    sizer.Fit(self)


    app = wx.PySimpleApp( )
    frm = MyFrame()
    frm.Show()
    app.MainLoop()
    [/CODE]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by ndoe
      i need a source code in python for example to make application with drag and drop fiture,i can't understand learning from wxPython2.6 docs,Thank for help
      By the way, it's best to stay current with releases. The current version on 2.8.4.

      Comment

      • ndoe
        New Member
        • Sep 2007
        • 12

        #4
        Originally posted by bartonc
        By the way, it's best to stay current with releases. The current version on 2.8.4.
        thanks for your help's but i have a new problem, i want make some application like pySketch but GUI use Boa Constructor and it's not same like i use python modul,can somebody help me? It's hard to me i use python just 1 months but my task making editing tool

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by ndoe
          thanks for your help's but i have a new problem, i want make some application like pySketch but GUI use Boa Constructor and it's not same like i use python modul,can somebody help me? It's hard to me i use python just 1 months but my task making editing tool
          Using Boa Constructor, you are still allowed to edit the files in many ways. You can even change the parameters to create() and __init__(), as this snippet shows:[CODE=python]#Boa:Frame:Stat ionReportDialog

          import wx

          from DatabaseNames import *
          from DBGridPanels import StationReportPa nel


          def BitmapPrintout( src_dc, w, h):
          pDialog = wx.PrintDialog( None) # Declare Printer Dialog.
          result = pDialog.ShowMod al() # Store dialog result after showing it.
          pData = pDialog.GetPrin tDialogData() # Get resutlts from Printer Dialog.
          dc = pDialog.GetPrin tDC() # Get DC data from Printer Dialog.

          # EmptyBitmap(int width, int height, int depth=-1) -> Bitmap
          bmp = wx.EmptyBitmap( w, h)
          ## bmpDC = wx.MemoryDC()

          dc.SetDeviceOri gin(400,1000) # Set the origin of the printed bitmap on the paper.
          dc.SetUserScale (10,10) # Set printer scale: if (1,1), each pixel will correspond to a dot
          # on the printer; so, if your printer prints at 300 DPI, a 300 pixel
          # wide image will be printed 1 inch wide.

          dc.StartDoc("pr inting...") # Begin new document to be printed.
          dc.StartPage() # Begin new page to be printed.
          ## src_dc.SelectOb ject(bmp) # "Enable" bitmap for "blitting".
          dc.Blit(0, 0, 0, 0, src_dc, 0, 0) # Copy bitmap into printer DC.
          ## bmpDC.SelectObj ect(wx.NullBitm ap) # "Free" bitmap.
          dc.EndPage()
          dc.EndDoc()



          def create(parent, dbServer, tableName, whereDict):
          return StationReportDi alog(parent, dbServer, tableName, whereDict)[/CODE]

          Comment

          • ndoe
            New Member
            • Sep 2007
            • 12

            #6
            Originally posted by bartonc
            Using Boa Constructor, you are still allowed to edit the files in many ways. You can even change the parameters to create() and __init__(), as this snippet shows:[CODE=python]#Boa:Frame:Stat ionReportDialog

            import wx

            from DatabaseNames import *
            from DBGridPanels import StationReportPa nel


            def BitmapPrintout( src_dc, w, h):
            pDialog = wx.PrintDialog( None) # Declare Printer Dialog.
            result = pDialog.ShowMod al() # Store dialog result after showing it.
            pData = pDialog.GetPrin tDialogData() # Get resutlts from Printer Dialog.
            dc = pDialog.GetPrin tDC() # Get DC data from Printer Dialog.

            # EmptyBitmap(int width, int height, int depth=-1) -> Bitmap
            bmp = wx.EmptyBitmap( w, h)
            ## bmpDC = wx.MemoryDC()

            dc.SetDeviceOri gin(400,1000) # Set the origin of the printed bitmap on the paper.
            dc.SetUserScale (10,10) # Set printer scale: if (1,1), each pixel will correspond to a dot
            # on the printer; so, if your printer prints at 300 DPI, a 300 pixel
            # wide image will be printed 1 inch wide.

            dc.StartDoc("pr inting...") # Begin new document to be printed.
            dc.StartPage() # Begin new page to be printed.
            ## src_dc.SelectOb ject(bmp) # "Enable" bitmap for "blitting".
            dc.Blit(0, 0, 0, 0, src_dc, 0, 0) # Copy bitmap into printer DC.
            ## bmpDC.SelectObj ect(wx.NullBitm ap) # "Free" bitmap.
            dc.EndPage()
            dc.EndDoc()



            def create(parent, dbServer, tableName, whereDict):
            return StationReportDi alog(parent, dbServer, tableName, whereDict)[/CODE]
            hello bartonc may i ask again? i use one button and one panel to show a bitmap to panel and would drag a bitmap in a panel,to show a bitmap it successful but to drag is still not working here's a part of the code,
            [CODE=python]
            def OnButton1Button (self, event):
            bmp1 = wx.Bitmap(u'sec tor1.bmp')
            panel= self.panel1
            pos = 10
            wx.StaticBitmap (panel, -1, bmp1, (10, pos), (bmp1.GetWidth( ),bmp1.GetHeigh t()))
            bmp = wx.Bitmap(u'sec tor1.bmp')
            shape = DragShape(bmp)
            shape.fullscree n = True
            #shape.pos = (200, 5)
            self.shapes.app end(shape)
            [/CODE]

            how to drag a bitmap with a panel as a parent? and i follow the python 2.6 docs
            for referension for this i hope you know where's the error for draging.thanks :D

            Comment

            Working...