Flashter
@Flashter
Учусь программировать

Как удалять объекты в canvas python?

Задача следующая: на экране генерируются в разных местах цифры. При клике на них они должны исчезать.

canv.create_text(x1, y1, anchor=W, font=("Purisa", size),
                text = 1, tag=1)
canv.create_text(x1, y1, anchor=W, font=("Purisa", size),
                text = 2, tag=2)
canv.create_text(x1, y1, anchor=W, font=("Purisa", size),
                text = 3, tag=3)
canv.tag_bind(???, '<Button-1>', click)


Как сформировать несколько бинтов? и как потом понимать на какую цифру нажали и ее удалить?
  • Вопрос задан
  • 4983 просмотра
Пригласить эксперта
Ответы на вопрос 1
SuckMyPython
@SuckMyPython
matrix.polling(none_stop=True)
Готово!

# -*- coding: utf8 -*-
from tkinter import*
import tkinter.ttk as ttk

root = Tk()

canv = Canvas(root, width=120, height=50)
canv.pack()

def click1(self):
	canv.delete(z1)

def click2(self):
	canv.delete(z2)

def click3(self):
	canv.delete(z3)
	
z1 = canv.create_text(20, 20, anchor=W, font=("Purisa", 20), text = 1, tag=1)
z2 = canv.create_text(50, 20, anchor=W, font=("Purisa", 20), text = 2, tag=2)
z3 = canv.create_text(80, 20, anchor=W, font=("Purisa", 20), text = 3, tag=3)

canv.tag_bind(z1, '<Button-1>', click1)
canv.tag_bind(z2, '<Button-1>', click2)
canv.tag_bind(z3, '<Button-1>', click3)

root.mainloop()


Но, если вам нужно что-то вроде игры на основе рандома то вот:
# -*- coding: utf8 -*-
from tkinter import*
import tkinter.ttk as ttk
import random

root = Tk()
root.geometry("500x500")

def z0():
	btn.destroy()
	canv = Canvas(root, width=500, height=500)
	canv.pack()

	def click(self):
		global rin		
		global num
		canv.delete(num)

		dbrin = rin
		print(dbrin)

		z1()
		
	def z1():
		global rin
		global num
		rin = random.SystemRandom().choice(["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"])
		xy = random.SystemRandom().choice(["50", "100", "150", "200", "250", "300", "350", "400", "450"])
		yx = random.SystemRandom().choice(["450", "400", "350", "300", "250", "200", "150", "100", "50"])

		x1 = xy
		y1 = yx
		
		num = canv.create_text(x1, y1, anchor=W, font=("Purisa", 20), text = rin)
		canv.tag_bind(num, '<Button-1>', click)

	z1()

btn = Button(root, text="START", width=10, height=5, fg="black", command=z0)
btn.pack()

root.mainloop()
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
19 апр. 2024, в 11:14
65000 руб./за проект
19 апр. 2024, в 11:08
5000 руб./за проект
19 апр. 2024, в 10:59
150000 руб./за проект