Advertisement
here2share

# Tk_swap_menu.py

Oct 17th, 2020
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. # Tk_swap_menu.py
  2.  
  3. import tkinter as tk
  4.  
  5. COLORS = '''
  6. red
  7. orange
  8. yellow
  9. lime green
  10. blue
  11. violet
  12. gold
  13. lawn green
  14. royal blue
  15. turquoise
  16. cyan
  17. gold3
  18. dark khaki
  19. khaki
  20. gray
  21. light grey
  22. deep pink
  23. hot pink
  24. pink
  25. '''.strip().splitlines()
  26.  
  27. block = {}
  28. seq = []
  29.  
  30. from ctypes import windll, Structure, c_ulong, byref
  31. import ctypes
  32. class POINT(Structure):
  33.     _fields_ = [("x", c_ulong), ("y", c_ulong)]
  34. def xyMouse():
  35.     pt = POINT()
  36.     windll.user32.GetCursorPos(byref(pt))
  37.     return pt
  38.  
  39. class DragAndDrop(tk.Frame):
  40.     def __init__(self, parent):
  41.         tk.Frame.__init__(self, parent)
  42.         self.parent = parent
  43.         self.parent.columnconfigure(0, weight=1)
  44.         self.grid_columnconfigure(0, weight=1)
  45.  
  46.         self.block_count = 0
  47.         self.block_h = 25
  48.  
  49.         self.button = tk.Button(self, text='Add', command=self.addblock)
  50.         self.button.grid(row=0, column=0, columnspan=2, sticky='new')
  51.  
  52.         self.container = tk.Frame(self)
  53.         self.container.grid(row=1, column=0, sticky='nsew')
  54.  
  55.         self.canvas = tk.Canvas(self.container, width=200, height=240)
  56.         self.scrollbar = tk.Scrollbar(self.container,
  57.                 orient='vertical',command=self.canvas.yview)
  58.         self.canvas.config(yscrollcommand=self.scrollbar.set)
  59.         self.canvas.grid(row=0, column=0, sticky='nsew')
  60.         self.scrollbar.grid(row=0, column=1, sticky='nse')
  61.  
  62.         self.container.bind('<Configure>', self.handle_scroll)
  63.  
  64.     def addblock(self):
  65.         if COLORS:
  66.             color=COLORS.pop(0)
  67.             b = str(self.block_count)
  68.             block[b] = tk.Frame(self.canvas, bd=1, relief='solid', bg=color)
  69.             block[b].columnconfigure(0, weight=1)
  70.  
  71.             grip = tk.Label(block[b], bitmap="gray25", bg=color)
  72.             grip.grid(row=0, column=0, sticky='w')
  73.  
  74.             grip.bind("<ButtonPress-1>", self.StartMove)
  75.             grip.bind("<ButtonRelease-1>", self.StopMove)
  76.             grip.bind("<B1-Motion>", self.OnMotion)
  77.             idt = tk.Label(block[b], text=self.block_count+1, bg=color, anchor='center')
  78.             idt.grid(row=0, columnspan=2)
  79.  
  80.             self.canvas.create_window((0, (self.block_count*self.block_h)),
  81.                 window=block[b], anchor="nw",
  82.                 width=200, height=24)
  83.             seq.append(self.block_count+1)
  84.             self.block_count += 1
  85.         self.handle_scroll(self)
  86.  
  87.     def handle_scroll(self, event):
  88.         self.canvas.configure(scrollregion=self.canvas.bbox("all"))
  89.  
  90.     def StartMove(self, event):
  91.         ry = root.winfo_y()+56
  92.         pt = xyMouse()
  93.         self.ym = pt.y
  94.         self.y = self.canvas.canvasy(self.ym)-ry
  95.         self.current = self.canvas.find_closest(10, self.y)[0]
  96.         block[str(self.current-1)].lift()
  97.         self.prev = int((self.canvas.coords(self.current)[1])/self.block_h)
  98.        
  99.     def OnMotion(self, event):
  100.         pt = xyMouse()
  101.         ym = pt.y
  102.         yy = ym-self.ym
  103.         h = self.block_h
  104.         pp = int((self.canvas.coords(self.current)[1]+h/2)/h)
  105.         if pp != self.prev:
  106.             self.canvas.coords(seq[pp], (0,self.prev*h))
  107.             seq[pp],seq[self.prev] = seq[self.prev],seq[pp]
  108.             self.prev = pp
  109.         mv = min(self.block_h*(self.block_count-1)+h/2, max(0+h/2,self.y+yy))
  110.         self.canvas.coords(self.current, (0,(mv-h/2)))
  111.  
  112.     def StopMove(self, event):
  113.         h = self.block_h
  114.         mv = int((self.canvas.coords(self.current)[1]+h/2)/h)*h
  115.         self.canvas.coords(self.current, (0,(mv)))
  116.        
  117.  
  118. root = tk.Tk()
  119.  
  120. app = DragAndDrop(root)
  121. app.grid(row=0, column=0, sticky='ew')
  122. root.mainloop()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement