Как заставить программу знать, что два предмета касаются друг друга? Python / Tkinter

Используйте эту строку ниже, вместо функции header().

echo "<script>window.top.location = 'https://apps.facebook.com/yourappnamespace/';</script>";
0
задан c.s.b 13 July 2018 в 10:28
поделиться

1 ответ

Не полный ответ, но рабочий шаг в правильном направлении для вас.

from tkinter import *

gate_id = 0
def andGATE():
    global gate_id
    gate_id += 1
    gate_tag = "andgate-%s" % gate_id
    tags = ("andgate", gate_tag)
    inputs = ("andgate", gate_tag, "input")
    outputs = ("andgate", gate_tag, "output")

    canvas.create_line(150, 50, 150, 150, width=5, tags=tags)
    canvas.create_arc(150, 50, 200, 150, start=90, extent=-180, width=5, fill="black", tags=tags)

    canvas.create_line(150, 75, 120, 75, width=5, tags=tags)
    canvas.create_line(150, 120, 120, 120, width=5, tags=tags)

    canvas.create_line(200, 100, 250, 100, width=5, tags=tags)
    #End of output
    canvas.create_line(250,100,245,100, width=5, fill="red", tags=outputs)
    #End of inputs
    canvas.create_line(150, 75, 120, 75, width=5, tags=tags)
    canvas.create_line(120, 75, 125, 75, width=5, fill="blue", tags=inputs)
    canvas.create_line(120, 120, 125, 120, width=5, fill="blue", tags=inputs)

    canvas.create_rectangle(150, 50, 180, 150, width=5, fill="black", tags=outputs)

    canvas.tag_bind(gate_tag, "<B1-Motion>", lambda event, tag=gate_tag: moveANDGate(event, tag))

def gateSelected():
    sf=var.get()
    if sf=='AND':
        andGATE()


def moveANDGate(event, tag):
    x=event.x
    y=event.y
    coords=canvas.coords(tag)
    movex=x-coords[0]
    movey=y-coords[1]
    canvas.move(tag, movex, movey)
    #print("Move")

    #Determine if we have an overlap between an input and an output
    for item in canvas.find_all():
        tags = canvas.gettags(item)
        if tag in tags:
            if 'input' in tags:
                #current item is an input of the moved object
                #Get the items coordinates
                coords = canvas.coords(item)
                #Find if we overlap with other objects
                closest = canvas.find_overlapping(coords[0],coords[1],coords[2],coords[3])
                for closest_items in closest:
                    closest_tags = canvas.gettags(closest_items)
                    if 'output' in closest_tags:
                        #If we overlap with another object, print connected and the appropriate tags
                        print("connected", closest_tags, "-", tag)




root = Tk()
root.geometry("500x500")
canvas=Canvas(root,width=300,height=300)
canvas.pack()
var = StringVar(root)
var.set('AND')
choices = ['AND']
option = OptionMenu(root, var, *choices)
option.pack(side="left",padx=10,pady=10)
button = Button(root, text="Add Gate", command=gateSelected)
button.pack(side="left",padx=10,pady=10)
#clearButton = Button(root,text="Clear",command=calculate)
#clearButton.pack(side="left",padx=30,pady=10)
root.mainloop()

Когда когда-либо объект перемещается, код пытается найти, будет ли один из входных портов формы мы просто перемещается, перекрывается с выходным портом. Если обнаружено совпадение, выводится «подключено». Возможно, вы захотите расширить то, что я сделал, чтобы добавить идентификатор порта на каждый входной порт, чтобы вы могли отличить, какой вход подключен к выходу других ворот.

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

Вы можете найти документацию tkinterbook для холста, которая будет полезна ( http://effbot.org/tkinterbook /canvas.htm).

Чтобы «привязать» два компонента вместе, когда они приближаются друг к другу, вы можете изменить раздел кода внутри функции moveANDGate на следующий

#Determine if we have an overlap between an input and an output
for item in canvas.find_all():
    tags = canvas.gettags(item)
    if tag in tags:
        if 'input' in tags:
            #current item is an input of the moved object
            #Get the items coordinates
            coords = canvas.coords(item)
            #Find if we overlap with other objects
            closest = canvas.find_overlapping(coords[0]-5,coords[1]-5,coords[2]+5,coords[3]+5)
            for closest_item in closest:
                closest_tags = canvas.gettags(closest_item)
                if 'output' in closest_tags:
                    #If we overlap with another object, print connected and the appropriate tags
                    print("connected", closest_tags, "-", tag)
                    connected_coords = canvas.coords(closest_item)
                    snapx = coords[0] - connected_coords[0]
                    snapy = coords[1] - connected_coords[1]
                    canvas.move(tag, -snapx, -snapy)

Если порты находятся в пределах 5 пикселей друг от друга, они будут привязываться друг к другу. (на данный момент это не работает очень хорошо с более чем двумя воротами.)

1
ответ дан scotty3785 17 August 2018 в 13:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: