471,357 Members | 1,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

wxPython Drag and Drop

12
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
Sep 12 '07 #1
5 8513
bartonc
6,596 Expert 4TB
From wxPyton in Action © Noel Rappin and Robin Dunn
Expand|Select|Wrap|Line Numbers
  1. import wx
  2.  
  3. class DragController(wx.Control):
  4.     """
  5.     Just a little control to handle dragging the text from a text
  6.     control.  We use a separate control so as to not interfere with
  7.     the native drag-select functionality of the native text control.
  8.     """
  9.     def __init__(self, parent, source, size=(25,25)):
  10.         wx.Control.__init__(self, parent, -1, size=size,
  11.                             style=wx.SIMPLE_BORDER)
  12.         self.source = source
  13.         self.SetMinSize(size)
  14.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  15.         self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
  16.  
  17.     def OnPaint(self, evt):
  18.         # draw a simple arrow
  19.         dc = wx.BufferedPaintDC(self)
  20.         dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
  21.         dc.Clear()
  22.         w, h = dc.GetSize()
  23.         y = h/2
  24.         dc.SetPen(wx.Pen("dark blue", 2))
  25.         dc.DrawLine(w/8,   y,  w-w/8, y)
  26.         dc.DrawLine(w-w/8, y,  w/2,   h/4)
  27.         dc.DrawLine(w-w/8, y,  w/2,   3*h/4)
  28.  
  29.     def OnLeftDown(self, evt):
  30.         text = self.source.GetValue()
  31.         data = wx.TextDataObject(text)
  32.         dropSource = wx.DropSource(self)
  33.         dropSource.SetData(data)
  34.         result = dropSource.DoDragDrop(wx.Drag_AllowMove)
  35.  
  36.         # if the user wants to move the data then we should delete it
  37.         # from the source
  38.         if result == wx.DragMove:
  39.             self.source.SetValue("")
  40.  
  41. class MyFrame(wx.Frame):
  42.     def __init__(self):
  43.         wx.Frame.__init__(self, None, title="Drop Source")
  44.         p = wx.Panel(self)
  45.  
  46.         # create the controls
  47.         label1 = wx.StaticText(p, -1, "Put some text in this control:")
  48.         label2 = wx.StaticText(p, -1,
  49.            "Then drag from the neighboring bitmap and\n"
  50.            "drop in an application that accepts dropped\n"
  51.            "text, such as MS Word.")
  52.         text = wx.TextCtrl(p, -1, "Some text")
  53.         dragctl = DragController(p, text)
  54.  
  55.         # setup the layout with sizers
  56.         sizer = wx.BoxSizer(wx.VERTICAL)
  57.         sizer.Add(label1, 0, wx.ALL, 5)
  58.         hrow = wx.BoxSizer(wx.HORIZONTAL)
  59.         hrow.Add(text, 1, wx.RIGHT, 5)
  60.         hrow.Add(dragctl, 0)
  61.         sizer.Add(hrow, 0, wx.EXPAND|wx.ALL, 5)
  62.         sizer.Add(label2, 0, wx.ALL, 5)
  63.         p.SetSizer(sizer)
  64.         sizer.Fit(self)
  65.  
  66.  
  67. app = wx.PySimpleApp()
  68. frm = MyFrame()
  69. frm.Show()
  70. app.MainLoop()
  71.  
Sep 12 '07 #2
bartonc
6,596 Expert 4TB
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.
Sep 12 '07 #3
ndoe
12
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
Sep 14 '07 #4
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. #Boa:Frame:StationReportDialog
  2.  
  3. import wx
  4.  
  5. from DatabaseNames import *
  6. from DBGridPanels import StationReportPanel
  7.  
  8.  
  9. def BitmapPrintout(src_dc, w, h):
  10.     pDialog = wx.PrintDialog(None)       # Declare Printer Dialog.
  11.     result = pDialog.ShowModal()        # Store dialog result after showing it.
  12.     pData = pDialog.GetPrintDialogData() # Get resutlts from Printer Dialog.
  13.     dc = pDialog.GetPrintDC()           # Get DC data from Printer Dialog.
  14.  
  15. #    EmptyBitmap(int width, int height, int depth=-1) -> Bitmap
  16.     bmp = wx.EmptyBitmap(w, h)
  17. ##    bmpDC = wx.MemoryDC()
  18.  
  19.     dc.SetDeviceOrigin(400,1000)       # Set the origin of the printed bitmap on the paper.
  20.     dc.SetUserScale(10,10)             # Set printer scale: if (1,1), each pixel will correspond to a dot
  21.         # on the printer; so, if your printer prints at 300 DPI, a 300 pixel
  22.         # wide image will be printed 1 inch wide.
  23.  
  24.     dc.StartDoc("printing...")         # Begin new document to be printed.
  25.     dc.StartPage()                     # Begin new page to be printed.
  26. ##    src_dc.SelectObject(bmp)           # "Enable" bitmap for "blitting".
  27.     dc.Blit(0, 0, 0, 0, src_dc, 0, 0)     # Copy bitmap into printer DC.
  28. ##    bmpDC.SelectObject(wx.NullBitmap)   # "Free" bitmap.
  29.     dc.EndPage()
  30.     dc.EndDoc()
  31.  
  32.  
  33.  
  34. def create(parent, dbServer, tableName, whereDict):
  35.     return StationReportDialog(parent, dbServer, tableName, whereDict)
Sep 14 '07 #5
ndoe
12
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:
Expand|Select|Wrap|Line Numbers
  1. #Boa:Frame:StationReportDialog
  2.  
  3. import wx
  4.  
  5. from DatabaseNames import *
  6. from DBGridPanels import StationReportPanel
  7.  
  8.  
  9.   def BitmapPrintout(src_dc, w, h):
  10.     pDialog = wx.PrintDialog(None)       # Declare Printer Dialog.
  11.     result = pDialog.ShowModal()        # Store dialog result after showing it.
  12.     pData = pDialog.GetPrintDialogData() # Get resutlts from Printer Dialog.
  13.     dc = pDialog.GetPrintDC()           # Get DC data from Printer Dialog.
  14.  
  15. #    EmptyBitmap(int width, int height, int depth=-1) -> Bitmap
  16.     bmp = wx.EmptyBitmap(w, h)
  17. ##    bmpDC = wx.MemoryDC()
  18.  
  19.     dc.SetDeviceOrigin(400,1000)       # Set the origin of the printed bitmap on the paper.
  20.     dc.SetUserScale(10,10)             # Set printer scale: if (1,1), each pixel will correspond to a dot
  21.         # on the printer; so, if your printer prints at 300 DPI, a 300 pixel
  22.         # wide image will be printed 1 inch wide.
  23.  
  24.     dc.StartDoc("printing...")         # Begin new document to be printed.
  25.     dc.StartPage()                     # Begin new page to be printed.
  26. ##    src_dc.SelectObject(bmp)           # "Enable" bitmap for "blitting".
  27.     dc.Blit(0, 0, 0, 0, src_dc, 0, 0)     # Copy bitmap into printer DC.
  28. ##    bmpDC.SelectObject(wx.NullBitmap)   # "Free" bitmap.
  29.     dc.EndPage()
  30.     dc.EndDoc()
  31.  
  32.  
  33.  
  34. def create(parent, dbServer, tableName, whereDict):
  35.     return StationReportDialog(parent, dbServer, tableName, whereDict)
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,
Expand|Select|Wrap|Line Numbers
  1. def OnButton1Button(self, event):
  2.         bmp1 = wx.Bitmap(u'sector1.bmp')
  3.         panel= self.panel1
  4.         pos = 10
  5.         wx.StaticBitmap(panel, -1, bmp1, (10, pos),         (bmp1.GetWidth(),bmp1.GetHeight()))
  6.         bmp = wx.Bitmap(u'sector1.bmp')
  7.         shape = DragShape(bmp) 
  8.         shape.fullscreen = True
  9.         #shape.pos = (200, 5)
  10.         self.shapes.append(shape)
  11.  
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
Sep 17 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

25 posts views Thread by James Goldwater | last post: by
15 posts views Thread by Grant Edwards | last post: by
2 posts views Thread by Emiliano Molina | last post: by
reply views Thread by Robin Dunn | last post: by
reply views Thread by Robin Dunn | last post: by
4 posts views Thread by stef | last post: by
44 posts views Thread by bg_ie | last post: by
1 post views Thread by Eric von Horst | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.