Advertisement
Guest User

Untitled

a guest
Nov 16th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.93 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. icomove = '''
  4. iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAABMlBMVEX///8AAAAAAAAAAAAAAAAAAAA
  5. AAAAAAAAAAABDRUUAAABNUU9fYmJjZmVaXlxnbGpqbmu4vL3b3d7Cw8PP0NCNkI2OkJCSlpSZm5uZnJ
  6. qgoaGlpqapqamqq6urq6utra2urq6vr6+wsLCysrK2tra5ubm6vr+7u7vBwcHCwsLDxMPIyMjLy8vNz
  7. c3Ozs7P0NDW1tbY2Njb29vb3d7c3Nvh4eHi4uLj4+Pk5OTl5eXn6Ojo6enp6enp6urp6uvq6+vr6+vr
  8. 7Ozs7Ozs7e3t7e3t7u7u7u7u7+/u8PHv7+/v8PDw8PDw8fHx8fHx8fLx8vLy8vLy8/Pz8/Pz9PT09PT
  9. 09fX19fX19vb29vb29/f39/f3+Pj4+Pj5+fj5+fn6+vn6+vr6+/v7+/v8/Pz9/f3+/v4I2c3KAAAAFX
  10. RSTlMAAQIDJTtFS0x4gZKYpq62xPf6/v7RkfSJAAABnUlEQVR42o2Tg4IjQRBA17vJ2Q7PVmxb45moB
  11. /n/X7iurvGcahG8l64XHf17zi6vonN55gmX2nS+4nhBFCVJVlRVgzEufcK0XKm32p3eYDSeTGezBcza
  12. J1wtao0Ww9PZYrlacRzHc5srn8AxDhg2CaJEZxf3Uq74Vrc/mkwpplBWFNqhkqSXciV04eFLwApN3MA
  13. Yz72UK4nyxYoXJAXobrcjhJgvnBQQGBdlVaOU4JgJTGGCAlxSHGzAWElMYYI2s7lDDZMKmILCkheRA8
  14. Ox0pjChDUnyD5uwRzSFDvCRpBU5IhRwBQU6AKbA8LJsBQUdhIs8HG4yGIKCs4BDFsGkQRJz+ImW8ADm
  15. GDuRtVivvDjlV/QNu4Bptb8+vHD5y9PDVaCAmEb8ADSfnuwSOmxaadcMmGNG+CAZf6ltS8+Mp0UFCAB
  16. Nxjt79lt7oHlpNy9CAmk+u3Nz4eWk3L/+ikVLgPCp/dPTDflxskRCKb3EhnD18+8lFsn/i9OPJ1KpzO
  17. Zd17K7eMj30RTbvp5NOXeNT+PptyJ/eM7fH703/MLSVTu/2Cp7PcAAAAASUVORK5CYII=
  18. '''
  19.  
  20. class DragWin(tk.Toplevel):
  21.     def __init__(self, parent: callable, *args, **kwargs):
  22.         super().__init__(parent, *args, **kwargs)
  23.         self.overrideredirect(True)
  24.         self.lift()
  25.         self.wm_attributes('-topmost', True)
  26.         self.ico = tk.PhotoImage(data=icomove)
  27.         lbl = tk.Label(self, image=self.ico, bg='white')
  28.         lbl.grid(row=0, column=0)
  29.  
  30. class DragDropElement:
  31.     def __init__(self, obj: callable, tipe: str='text', *args, **kwargs):
  32.         self.obj = obj
  33.         self._dragable = True
  34.         self._dropable = True
  35.         self.obj.dropable = True
  36.         self.tipe = tipe
  37.         self.obj.tipe = self.tipe
  38.         self.obj.drop_data = self.drop_data
  39.         self.mwin = None
  40.         self.obj.bind("<Button-3>", self._on_press)
  41.         self.obj.bind("<B3-Motion>", self._on_motion)
  42.         self.obj.bind("<ButtonRelease-3>", self._on_release)
  43.  
  44.     @property
  45.     def dragable(self) -> bool:
  46.         return self._dragable
  47.     @dragable.setter
  48.     def dragable(self, value: bool) -> None:
  49.         self._dragable = value
  50.    
  51.     @property
  52.     def dropable(self) -> bool:
  53.         return self._dropable
  54.     @dropable.setter
  55.     def dropable(self, value: bool) -> None:
  56.         self._dropable = value
  57.  
  58.     def _on_press(self, evt: callable) -> None:
  59.         if not self._dragable: return
  60.         x,y = self.obj.winfo_pointerxy()
  61.         self.mwin = DragWin(self.obj)
  62.         self.mwin.geometry(f'+{x}+{y}')
  63.        
  64.     def _on_motion(self, evt: callable) -> None:
  65.         if not self._dragable: return
  66.         x, y = self.obj.winfo_pointerxy()
  67.         self.mwin.geometry(f'+{x}+{y}')
  68.  
  69.     def _on_release(self, evt: callable) -> None:
  70.         if not self._dragable: return
  71.         self.mwin.destroy()
  72.         self.mwin = None
  73.         x, y = self.obj.winfo_pointerxy()
  74.         obj = self.__verify_to_coords((x,y))
  75.         if obj and hasattr(obj, 'dropable'):
  76.             txt = self.obj.get()
  77.             obj.drop_data(self.tipe, txt)
  78.  
  79.     def __verify_to_coords(self, c2: tuple) -> callable:
  80.         top = self.__get_top(self.obj)
  81.         if not top: return
  82.         w = top.winfo_containing(*c2)
  83.         return top._nametowidget(w)        
  84.  
  85.     def __get_top(self, wdg: callable) -> callable:
  86.         p = wdg.winfo_parent()
  87.         w = wdg._nametowidget(p)
  88.         if w:
  89.             if isinstance(w, tk.Tk):
  90.                 top = w
  91.             else:
  92.                 top = self.__get_top(w)
  93.         else:
  94.             top = None
  95.         return top
  96.  
  97.     def drop_data(self, descriptor, data: any) -> None:
  98.         if not self._dropable: return
  99.         if descriptor != self.obj.tipe: return
  100.         if self.tipe == 'text':
  101.             if self.obj.get():
  102.                 data = ' ' + data
  103.             self.obj.insert(tk.END, data)
  104.        
  105.  
  106. class WinDest(tk.Toplevel):
  107.     def __init__(self, parent, *args, **kwargs):
  108.         super().__init__(parent, *args, **kwargs)
  109.         self.parent = parent
  110.         lbl = tk.Label(self, text='Casella destinazione :')
  111.         lbl.grid(row=0, column=0, padx=5, pady=5, sticky='w')
  112.         en = tk.Entry(self)
  113.         en.grid(row=0, column=1, padx=5, pady=5, sticky='ew')
  114.         self.e_test = DragDropElement(en)
  115.         self.e_test.dragable = False  # commentare x attivare
  116.  
  117.     def message(self, msg):
  118.         self.dida.configure(text=msg)
  119.         self.update()
  120.    
  121.    
  122. class WinTest(tk.Tk):
  123.     def __init__(self):
  124.         super().__init__()
  125.         lbl = tk.Label(self, text='Casella sorgente :')
  126.         lbl.grid(row=0, column=0, padx=5, pady=5, sticky='w')
  127.         en = tk.Entry(self)
  128.         en.grid(row=0, column=1, padx=5, pady=5, sticky='ew')
  129.         self.e_test = DragDropElement(en)
  130.         self.e_test.dragable = False
  131.         pn_bt = tk.Frame(self)
  132.         pn_bt.grid(row=1, column=0, columnspan=2, padx=5, pady=5, sticky='ew')
  133.         self.bt_active = tk.Button(pn_bt, text='Attiva', command=self._on_active)
  134.         self.bt_active.grid(row=0, column=0, padx=(5,2), pady=5, sticky='ew')
  135.         bt_close = tk.Button(pn_bt, text='Chiudi', command=self.destroy)
  136.         bt_close.grid(row=0, column=1, padx=(2,5), pady=5, sticky='ew')
  137.         pn_bt.grid_columnconfigure(0, weight=1, uniform='bt')
  138.         pn_bt.grid_columnconfigure(1, weight=1, uniform='bt')
  139.         self.grid_rowconfigure(0, weight=1)
  140.         self.grid_columnconfigure(0, weight=1)
  141.  
  142.     def _on_active(self):
  143.         self.e_test.dragable = not self.e_test.dragable
  144.         if self.e_test.dragable:
  145.             self.bt_active.configure(text='Disattiva')
  146.         else:
  147.             self.bt_active.configure(text='Attiva')
  148.  
  149.     def message(self, msg):
  150.         self.dida.configure(text=msg)
  151.         self.update()
  152.  
  153.  
  154. if __name__ == '__main__':
  155.     app = WinTest()
  156.     w = WinDest(app)
  157.     app.mainloop()
  158.     # Questo OK
  159.  
Tags: xdiscussione
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement