I am trying to make a drag and drop function in PySimpleGUI, where clicking on a canvas object selects it and lets you drag around, as long as it’s still clicked. If you click anything else other than said object, the object deselects. How do you do that with a code this complex, bonus points if you help me understand tkinter.dnd?
import PySimpleGUI as sg
sg.theme('DefaultNoMoreNagging')
items_files = [ [sg.Button(pad = (0, 0), tooltip = 'Delete from Frontmost', key = 'DelF')] ]
tab2 = [ [sg.Button(key = 'Rect', pad = (0, 0), tooltip = 'Rectangle')] ]
BC = 'white'
CW = 600
CH = 450
LXM = (CW / 2)
LYM = (CH / 2)
mid_left = [ [sg.Text(text = "X:"), sg.Input(default_text = "0", key = "TLX", size = (3, 1))],
[sg.Text(text = "Y:"), sg.Input(default_text = "0", key = "TLY", size = (3, 1))],
[sg.Text(text = "Scale X:"), sg.Input(default_text = "0", key = "TSX", size = (3, 1))],
[sg.Text(text = "Scale Y:"), sg.Input(default_text = "0", key = "TSY", size = (3, 1))] ]
can = sg.Canvas(size = (CW, CH), background_color = BC, border_width = 1, key = "canvas")
mid_screen = [ [sg.Frame("Insert Values", mid_left, expand_x = True, expand_y = True), can] ]
layout = [ [sg.Frame("", items_files, pad = (1, 1), expand_x = False, expand_y = False), sg.TabGroup([[sg.Tab('Insert', tab2)]])],
[mid_screen] ]
window = sg.Window('title', layout, finalize = True)
tkc = can.TKCanvas
N = 0
OL = [0]
while True:
event, values = window.read()
if event in (None, sg.WIN_CLOSED, 'Exit'):
break
TLX = values["TLX"]
TLY = values["TLY"]
TSX = values["TSX"]
TSY = values["TSY"]
LT = 1
FC = 'red'
LC = 'black'
def MakeRect():
pum = N
OL.insert(pum, tkc.create_rectangle((float(LXM) + float(TLX) + float(TSX)), (float(LYM) + float(TLY) + float(TSY)), (float(LXM) + float(TLX) - float(TSX)), (float(LYM) + float(TLY) - float(TSY)), fill = FC, outline = LC, width = LT, tag = pum))
pum = N + 1
return pum
def DeleteObj():
pum = N
tkc.delete(OL[pum])
del OL[pum]
pum = N - 1
if event == 'Rect':
MakeRect()
print (OL)
print (N)
if event == 'DelF':
DeleteObj()
print (OL)
print (N)
window.close()
Bonus question: If I select an object, it should return the tag some parameters.If this is possible, can these parameters be edited?