我想在tkinter中创建一个矩形,它会随着输入的数据不断改变大小。我使用套接字接收数据,并希望显示一个矩形和一个圆。在每个循环中,矩形改变大小并更新圆的位置。当使用代码只是圆,它工作正常。但不是矩形的代码。我试过coords和item config。它不起作用。有人可以帮忙吗?如果使用tkinter无法做到这一点,是否有人可以建议另一个库?
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '169.254.1.248'
port=1992
s.bind((host,port))
s.listen(5)
cl,address = s.accept()
from tkinter import *
root = Tk()
root.overrideredirect(1)
root.geometry("320x240+0+0")
cnv1 = Canvas(root, width=38, height=240)
cnv1.pack(side="left",fill=BOTH,expand=1)
cnv2 = Canvas(root, width=276, height=240)
cnv2.pack(side="right",fill=BOTH,expand=1)
cnv1.create_rectangle(2, 2, 36, 237, fill="lightblue")
cnv2.create_rectangle(2, 2, 273, 237, fill="lightblue")
cnv2.create_line(2,119.5,273,119.5)
cnv2.create_line(137.5,2,137.5,237)
circ1=cnv1.create_oval(19,118.5,21,120.5, outline="lightblue", fill="", width=0)
circ2=cnv2.create_oval(134,116,140,122, outline="lightblue", fill="", width=1)
rect=cnv2.create_rectangle(2, 2, 4, 5, fill="", outline="", width =1)
def measurementvolume(z):
z2 = 2400;z1 =800;x2=1300;x1=400;y2=1500;y1=400
slope=((x2-x1)/(z2-z1))
offset = x2-(slope*z2)
vb=(slope*z) + offset
slope= ((y2-y1)/(z2-z1))
offset = y2-(slope*z2)
hb=(slope*z) + offset
return [vb,hb]
while True:
msg=cl.recv(1024)
strmsg=msg.decode()
if 'nodata' not in strmsg:
coords=strmsg.split()
del strmsg
cx=(float(coords[0]))/5.51
cy=(float(coords[1]))/5.51
cx=cx+117.5
cy=135.5-cy ##if x axis is not inverted
cz=float(coords[2])
cz=-800-cz
cz=cz/6.78
boundary=measurementvolume(cz)
bx=boundary[0]/5.51
by=boundary[1]/5.51
upco1=135.5-(abs(by)/2)
upco2=117.5-(abs(bx)/2)
upco3=upco1+abs(by)
upco4=upco2+abs(bx)
if (abs(by) > cy) and (abs(bx) > cx):
cnv2.itemconfig(rect,outline="black",fill="red", width=2)
cnv2.coords(rect,upco1,upco2,upco3,upco4)
cnv2.itemconfig(circ2,outline="black", fill="black", width=3)
cnv2.coords(circ2,cy-3,cx-3,cy+3,cx+3)
cnv1.itemconfig(circ1,outline="black", fill="black", width=3)
cnv1.coords(circ1,16,cz-3,22,cz+3)
del cx,cy,cz
else:
del strmsg
cnv2.itemconfig(rect,fill="", outline="lightblue", width =0)
cnv2.coords(rect,2, 2, 4, 5)
cnv2.itemconfig(circ2,outline="", fill="", width=0)
cnv2.coords(circ2,134,116,140,122)
cnv1.itemconfig(circ1,outline="", fill="", width=0)
cnv1.coords(circ1,19,118.5,21,120.5)
root.update()
cl.close()