I want to program a program that will draw the text where I click and when I go through it it will be deleted. This is my code when I cross the line, everything is cleared and the ones I didn't cross are drawn back. Thanks to anyone who help me.
from tkinter import *
class Program(Tk):
def __init__(self):
super().__init__()
self.w, self.h = 250, 200
self.canvas = Canvas(width=500, height=400)
self.canvas.pack()
self.draw_line()
#self.x_lst_txt = None
self.txt_lst_org = []
self.txt2_lst_org = []
self.change = False
self.positon = False
self.idx= 0
self.new_lst=[]
self.text1 = "Programovanie"
self.text2 = "Je zábava"
self.color1 = "green"
self.color2 = "orange"
self.opr1 = "-"
self.opr2 = "+"
self.all_binds()
def draw_line(self):
self.line = self.canvas.create_line(self.w, 0, self.w, self.h*2, width=5)
def all_binds(self):
self.canvas.bind("<Button-1>", self.draw_texts)
self.canvas.bind("<Button-3>", self.draw_reacts)
self.canvas.bind_all("<Right>", self.move_line_right)
self.canvas.bind_all("<Left>", self.move_line_left)
def draw_texts(self, event):
self.xt = event.x
self.yt = event.y
uhol=self.yt
if self.xt < self.w:
self.text_left = self.canvas.create_text(self.xt, self.yt, text=self.text1, fill=self.color1, angle=uhol)
self.txt2_lst_org.append([self.xt, self.yt])
self.change = True
else:
self.text_right = self.canvas.create_text(self.xt, self.yt, text=self.text2, fill=self.color2, angle=-uhol)
self.txt_lst_org.append([self.xt, self.yt])
self.change = True
def draw_reacts(self, event):
self.xr = event.x
self.yr = event.y
for i in range(10):
rect = self.canvas.create_rectangle(self.xr, self.yr+i*10, self.xr+10, self.yr+10+i*10)
self.yr += 5
def select_x(self, lst):
if self.change:
for i in range(0, len(lst)):
if [len(i) for i in lst][self.idx] > 1:
self.new_lst.append(lst[i][0])
self.idx+=1
self.idx = 0
self.change=False
return sorted(self.new_lst)
return sorted(self.new_lst)
def delete_all(self, lst, x_lst):
self.new_lst = sorted(self.new_lst, reverse=self.positon)
x_lst = sorted(x_lst, reverse=self.positon)
del lst[0]
del x_lst[0]
del self.new_lst[0]
def replace_text(self, lst, txt, fill, opr):
for i in range(0, len(lst)+1):
x, y = lst[i]
angle = y
texts = self.canvas.create_text(x, y, text=txt, fill=fill, angle=f"{opr}{angle}")
def replace(self, lst, x_lst, con_lst, text, color, opr):
self.delete_all(lst, x_lst)
self.canvas.delete("all")
self.draw_line()
if lst != []:
self.replace_text(lst, text, color, opr)
elif con_lst != []:
self.replace_text(con_lst, text, color, opr)
def move_line_right(self, event):
self.canvas.move(self.line, 5, 0)
self.w+=5
self.positon = False
self.txt_lst_x = sorted(self.select_x(self.txt_lst_org))
self.txt_lst_org = sorted(self.txt_lst_org)
print("enter right",self.txt_lst_x, "org", self.txt_lst_org)
if self.txt_lst_x != []:
try:
if self.w >= self.txt_lst_x[0]:
print("TRUE RIGHT")
self.replace(self.txt_lst_org, self.txt_lst_x, self.txt2_lst_org,
self.text2, self.color2, self.opr1)
except IndexError:
pass
def move_line_left(self, event):
self.canvas.move(self.line, -5, 0)
self.w-=5
self.positon = True
self.txt2_lst_x = sorted(self.select_x(self.txt2_lst_org), reverse=self.positon)
self.txt2_lst_org = sorted(self.txt2_lst_org, reverse=self.positon)
print("enter left", self.txt2_lst_x, "org", self.txt2_lst_org)
if self.txt2_lst_x != []:
try:
if self.w <= self.txt2_lst_x[0]:
print("TRUE LEFT")
self.replace(self.txt2_lst_org, self.txt2_lst_x, self.txt_lst_org,
self.text1, self.color1, self.opr2)
except IndexError:
pass
main = Program()
main.mainloop()
deletemessage is documented and there are many examples on the internet. Why do you need our help? Also, please reduce this code down to a minimal reproducible example. If the question is about deleting canvas objects then we don't need code for moving them, selecting them, drawing text, etc.canvas.delete()instead ofreplace(). In current code if line touch text on first list then you get it and replace with text on second list - and this way you move it from left side to right side.self.- it can be misleading -x = event.xinstead ofself.xt = event.x. And you could use more readable names - ie.text_leftinstead of `txt_lst_org