Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_swap_menu.py
- import tkinter as tk
- COLORS = '''
- red
- orange
- yellow
- lime green
- blue
- violet
- gold
- lawn green
- royal blue
- turquoise
- cyan
- gold3
- dark khaki
- khaki
- gray
- light grey
- deep pink
- hot pink
- pink
- '''.strip().splitlines()
- block = {}
- seq = []
- from ctypes import windll, Structure, c_ulong, byref
- import ctypes
- class POINT(Structure):
- _fields_ = [("x", c_ulong), ("y", c_ulong)]
- def xyMouse():
- pt = POINT()
- windll.user32.GetCursorPos(byref(pt))
- return pt
- class DragAndDrop(tk.Frame):
- def __init__(self, parent):
- tk.Frame.__init__(self, parent)
- self.parent = parent
- self.parent.columnconfigure(0, weight=1)
- self.grid_columnconfigure(0, weight=1)
- self.block_count = 0
- self.block_h = 25
- self.button = tk.Button(self, text='Add', command=self.addblock)
- self.button.grid(row=0, column=0, columnspan=2, sticky='new')
- self.container = tk.Frame(self)
- self.container.grid(row=1, column=0, sticky='nsew')
- self.canvas = tk.Canvas(self.container, width=200, height=240)
- self.scrollbar = tk.Scrollbar(self.container,
- orient='vertical',command=self.canvas.yview)
- self.canvas.config(yscrollcommand=self.scrollbar.set)
- self.canvas.grid(row=0, column=0, sticky='nsew')
- self.scrollbar.grid(row=0, column=1, sticky='nse')
- self.container.bind('<Configure>', self.handle_scroll)
- def addblock(self):
- if COLORS:
- color=COLORS.pop(0)
- b = str(self.block_count)
- block[b] = tk.Frame(self.canvas, bd=1, relief='solid', bg=color)
- block[b].columnconfigure(0, weight=1)
- grip = tk.Label(block[b], bitmap="gray25", bg=color)
- grip.grid(row=0, column=0, sticky='w')
- grip.bind("<ButtonPress-1>", self.StartMove)
- grip.bind("<ButtonRelease-1>", self.StopMove)
- grip.bind("<B1-Motion>", self.OnMotion)
- idt = tk.Label(block[b], text=self.block_count+1, bg=color, anchor='center')
- idt.grid(row=0, columnspan=2)
- self.canvas.create_window((0, (self.block_count*self.block_h)),
- window=block[b], anchor="nw",
- width=200, height=24)
- seq.append(self.block_count+1)
- self.block_count += 1
- self.handle_scroll(self)
- def handle_scroll(self, event):
- self.canvas.configure(scrollregion=self.canvas.bbox("all"))
- def StartMove(self, event):
- ry = root.winfo_y()+56
- pt = xyMouse()
- self.ym = pt.y
- self.y = self.canvas.canvasy(self.ym)-ry
- self.current = self.canvas.find_closest(10, self.y)[0]
- block[str(self.current-1)].lift()
- self.prev = int((self.canvas.coords(self.current)[1])/self.block_h)
- def OnMotion(self, event):
- pt = xyMouse()
- ym = pt.y
- yy = ym-self.ym
- h = self.block_h
- pp = int((self.canvas.coords(self.current)[1]+h/2)/h)
- if pp != self.prev:
- self.canvas.coords(seq[pp], (0,self.prev*h))
- seq[pp],seq[self.prev] = seq[self.prev],seq[pp]
- self.prev = pp
- mv = min(self.block_h*(self.block_count-1)+h/2, max(0+h/2,self.y+yy))
- self.canvas.coords(self.current, (0,(mv-h/2)))
- def StopMove(self, event):
- h = self.block_h
- mv = int((self.canvas.coords(self.current)[1]+h/2)/h)*h
- self.canvas.coords(self.current, (0,(mv)))
- root = tk.Tk()
- app = DragAndDrop(root)
- app.grid(row=0, column=0, sticky='ew')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement