I made a program where the MakeRect
function makes a rectangle at specified parameters and the DeleteObj
function deletes the last one added, and it looks like the code works. However, the DeleteObj
function wont work against the rectangles added by the MakeRect
function, because the list insertion somehow gets wrong, I do not know why. When I call the MakeRect
function It is supposed to be from [0, 1]
to [0, 1, 2]
and finally [0, 1, 2, 3]
and when I call the DeleteObj
function, it goes from [0, 1, 2, 3]
to [0, 1, 2]
and so on. Instead, I get [1, 0]
, [2, 0]
, [3, 0]
and so on, and when I call the DeleteObj
function, it goes back to just []
, and when I activate the MakeRect
function again, it instantly goes to [4.0]
and so on. Can somebody help me? Here’s the code:
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
while True:
event, values = window.read()
N = 0
OL = [0]
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)
if event in (sg.WIN_CLOSED, 'Exit'):
break
window.close()
P.S. Bonus points if you can solve the TypeError: 'NoneType' object is not subscriptable
error. Full:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Help.py", line 34, in <module>
TLX = values["TLX"]
TypeError: 'NoneType' object is not subscriptable