Как переместить кнопку на холсте ткинтер? - PullRequest
0 голосов
/ 05 сентября 2018

Привет, я хочу иметь возможность перемещать лампочку с кнопкой на экране. Я попытался сделать так, чтобы кнопка меняла лампочку с белого на желтый, чтобы она выглядела как включенная и выключенная. Моя программа просто добавляет лампочку на холст, но вы не можете переместить ее или изменить цвет без появления ошибок. Я также хочу добавить на холст несколько лампочек, которые могут двигаться самостоятельно.

from tkinter import *

root=Tk()
root.grid()

canvas=Canvas(root,height=600,width=600)
canvas.grid()

input_id=0

def addInputs():
   global input_id
   input_id+=1
   input_tag="input-%s"%input_id
   tags=("input",input_tag)
   canvas.create_oval(200,200,250,250,fill="WHITE",width=5,tag=tags)
   canvas.create_rectangle(210,245,237,275,fill="LIGHTGREY",width=5,tag=tags)
   onBtn=Button(root,text="On/Off",command=lambda:inputStatusOn())
   onBtn.place(x=200,y=212)
   canvas.tag_bind(input_tag,onBtn,"<B1-Motion>", lambda event,tag=input_tag:moveInput(event,tag,onBtn))

def inputStatusOn():
   global input_id
   input_id+=1
   input_tag="input-%s"%input_id
   tags=("input",input_tag)
   canvas.delete("input")
   canvas.create_oval(200,200,250,250,fill="YELLOW",width=5,tag=tags)
   canvas.create_rectangle(210,245,237,275,fill="LIGHTGREY",width=5,tag=tags)
   onBtn=Button(root,text="On/Off",command=lambda:inputStatusOff(),tag=tags)
   onBtn.place(x=200,y=212)
   canvas.tag_bind(input_tag,onBtn,"<B1-Motion>", lambda event,tag=input_tag:moveInput(event,tag,onBtn))

def inputStatusOff():
   global input_id
   input_id+=1
   input_tag="input-%s"%input_id
   tags=("input",input_tag)
   canvas.delete("input")
   canvas.create_oval(200,200,250,250,fill="WHITE",width=5,tag=tags)
   canvas.create_rectangle(210,245,237,275,fill="LIGHTGREY",width=5,tag=tags)
   onBtn=Button(root,text="On/Off",command=lambda:inputStatusOn(),tag=tags)
   onBtn.place(x=200,y=212)
   canvas.tag_bind(input_tag,onBtn,"<B1-Motion>", lambda event,tag=input_tag:moveInput(event,tag,onBtn))




def moveInput(event, tag,onBtn):
   x=event.x
   y=event.y
   coords=canvas.coords(tag,onBtn)
   movex=x-coords[0]
   movey=y-coords[1]
   canvas.move(tag,onBtn, movex, movey)

btn=Button(root,text="Add Input",command=lambda:addInputs())
btn.place(x=100,y=100)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...