Можно ли обновлять Canvas каждый раз, когда данные вводятся в поля ввода? - PullRequest
1 голос
/ 01 мая 2020

У меня есть этот код, где он показывает aws план каждого здания. Это работает отлично, но я не хочу всегда нажимать кнопку «Рисовать», чтобы увидеть это. Я сделал OptionMenu, где, если меню 1 меняется, пункты в меню 2 меняются. Я также сделал Labels, где он печатает сумму полей Entry. Они оба используют трассировку, но я не могу найти ничего об этом, используя Canvas. Может кто-нибудь указать мне правильное направление?

from tkinter import *

root = Tk()
root.title("drawing lines")
root.state('zoomed')
# Building 1
L1 = Label(root, text="Building 1 Width")
L2 = Label(root, text="Bay 1 Spacing")
L3 = Label(root, text="Bay 2 Spacing")
L4 = Label(root, text="Bay 3 Spacing")
L5 = Label(root, text="Bay 4 Spacing")
L6 = Label(root, text="Bay 5 Spacing")
L7 = Label(root, text="Bay 6 Spacing")
L100 = Label(root, text="Sidewall Offset")
L1.place(y=20)
L2.place(y=50)
L3.place(y=80)
L4.place(y=110)
L5.place(y=140)
L6.place(y=170)
L7.place(y=200)
L100.place(y=230)

E1 = Entry(root, width=5)  # Building Width
E1.place(x=200, y=20)
E3 = Entry(root, width=5)  # Bay 1 Width
E3.place(x=200, y=50)
E4 = Entry(root, width=5)  # Bay 2 Width
E4.place(x=200, y=80)
E5 = Entry(root, width=5)  # Bay 3 Width
E5.place(x=200, y=110)
E6 = Entry(root, width=5)  # Bay 4 Width
E6.place(x=200, y=140)
E7 = Entry(root, width=5)  # Bay 5 Width
E7.place(x=200, y=170)
E8 = Entry(root, width=5)  # Bay 6 Width
E8.place(x=200, y=200)
E100 = Entry(root, width=5)  # Sidewall Offset
E100.place(x=200, y=230)


# Building 2
L8 = Label(root, text="Building 2 Width")
L9 = Label(root, text="Bay 1 Spacing")
L10 = Label(root, text="Bay 2 Spacing")
L11 = Label(root, text="Bay 3 Spacing")
L12 = Label(root, text="Bay 4 Spacing")
L13 = Label(root, text="Bay 5 Spacing")
L14 = Label(root, text="Bay 6 Spacing")
L8.place(x=400, y=20)
L9.place(x=400, y=50)
L10.place(x=400, y=80)
L11.place(x=400, y=110)
L12.place(x=400, y=140)
L13.place(x=400, y=170)
L14.place(x=400, y=200)

E9 = Entry(root, width=5)  # Building Width
E9.place(x=600, y=20)
E11 = Entry(root, width=5)  # Bay 1 Width
E11.place(x=600, y=50)
E12 = Entry(root, width=5)  # Bay 2 Width
E12.place(x=600, y=80)
E13 = Entry(root, width=5)  # Bay 3 Width
E13.place(x=600, y=110)
E14 = Entry(root, width=5)  # Bay 4 Width
E14.place(x=600, y=140)
E15 = Entry(root, width=5)  # Bay 5 Width
E15.place(x=600, y=170)
E16 = Entry(root, width=5)  # Bay 6 Width
E16.place(x=600, y=200)


sf=3  # Scale Factor

def draw():
    total1=0
    of1=20 # Horizontal Offset
    of2=20+int(E100.get())  # Vertical Offset for building 1
    w = Canvas(root, width=1000, height=500, bg='white')
    w.place(y=250)

    list1=[]
    list1.append(E3.get())
    list1.append(E4.get())
    list1.append(E5.get())
    list1.append(E6.get())
    list1.append(E7.get())
    list1.append(E8.get())
    list1 = [0 if x =="" else x for x in list1]
# Building 1
    for i in range(0, len(list1)):
        total1 = total1+float(list1[i])
        w.create_rectangle(sf*of1, sf*of2, sf*(of1+float(total1)), sf*(of2+float(E1.get())))

# Building 2
    total2=0
    of3=total1
    of4=20  # Vertical offset for building 2
    list2=[]
    list2.append(E11.get())
    list2.append(E12.get())
    list2.append(E13.get())
    list2.append(E14.get())
    list2.append(E15.get())
    list2.append(E16.get())
    list2 = [0 if x =="" else x for x in list2]
    for i in range(0, len(list2)):
        total2 = total2+float(list2[i])
        w.create_rectangle(sf*(total1+of1), sf*of4, sf*(of1+ (total1+float(total2))), sf*(of4+float(E9.get())))

    #Building 1 Center Line
    w.create_line(sf*of1, sf*(of2+(float(E1.get())/2)), sf*(of1+total1), sf*(of2+(float(E1.get())/2)), dash =(5,5))
    #Building 2 Center Line
    w.create_line(sf*(of1+of3), sf*(of4+(float(E9.get())/2)), sf*(of1+total1+total2), sf*(of4+(float(E9.get())/2)), dash=(5,5))
    #Building 1 Bay spacing Markers
    total3 = 0
    for i in range(0, len(list1)):
        if list1[i] !=0:
            total3 = total3 + float(list1[i])
            w.create_text(sf*(of1+total3 - float(list1[i])/2), sf*(10+float(E100.get())), text=list1[i])
        else:
            break

    # Building 2 Bay Spacings
    total4 = 0
    for i in range(0, len(list2)):
        if list2[i] != 0:  
            total4 = total4 + float(list2[i])
            w.create_text(sf*(total1+of1+(total4 - float(list2[i])/2)), sf*10, text=list2[i])
        else:
            break

    #Building 1 Width Marker
    w.create_text(sf*10, sf*(of2+float(E1.get())/2), text= E1.get())
    #Building 2 Width Marker
    w.create_text(sf*(of1+total1+total2+10), sf*(of4+float(E9.get())/2), text = E9.get())




b = Button(root, text="Draw", command = draw)
b.pack()

root.mainloop()

1 Ответ

0 голосов
/ 02 мая 2020

Вы можете привязать клавишу возврата к записи, чтобы вызвать функцию отрисовки:

entry.bind('<Return>', draw)

Вот небольшой пример того, как др aws количество точек, введенных в Entry, в случайные места на холсте на Canvas

import random
import tkinter as tk


WIDTH, HEIGHT = 600, 600

def draw(dummy_e):
    n = int(entry.get())
    dx, dy = 2, 2
    for _ in range(n):
        x, y = random.randrange(WIDTH), random.randrange(WIDTH)
        canvas.create_oval(x+dx, y+dy, x-dx, y-dy)


if __name__ == '__main__':

    root = tk.Tk()
    canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='cyan')
    canvas.pack()

    entry = tk.Entry(root)
    entry.pack()

    entry.bind('<Return>', draw)

    root.mainloop()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...