我正在使用Tkinter库通过拖放方法在Tkinter的画布小部件上填充多个窗口。 我将标签小部件拖放到画布上,这些小部件将存储我的IP地址。但是,当我将一个或多个标签拖放到画布上时,仅存储一个IP地址,而不存储所存储的地址列表。
请问有人对我如何使小部件实现此功能或任何更好的方法有任何想法吗?
注意:当我打印到控制台时,生成的IP地址范围将全部十个地址打印到控制台,而不是单个小部件。
def ipRange(self, start_ip, end_ip):
start = list(map(int, start_ip.split(".")))
end = list(map(int, end_ip.split(".")))
temp = start
ip_range = []
ip_range.append(start_ip)
while temp != end:
start[3] += 1
for i in (3, 2, 1):
if temp[i] == 256:
temp[i] = 0
temp[i - 1] += 1
ip_range.append(".".join(map(str, temp)))
return ip_range
def appear(self, canvas, xy):
ip_range = self.ipRange('192.168.1.0', '192.168.1.10')
if self.canvas:
# we are already on a canvas; do nothing
return
self.X, self.Y = xy
# Create a label which identifies the object, including a unique number
# self.label = Label(canvas, text=self.Name, borderwidth=2, relief=RAISED)
for ip in ip_range:
print ip
self.computerLabel = Label(canvas, text=ip)
# Display the label on a window on the canvas. We need the ID returned by the canvas so we can move the label around as the mouse moves.
self.computerID = canvas.create_window(self.X - self.OffsetX, self.Y - self.OffsetY, window=self.computerLabel, anchor="nw")
# Note the canvas on which we drew the label.
self.canvas = canvas
我的预期输出是每次将新的标签小部件拉到画布上时,都会生成一个新的IP地址并将该IP地址存储在标签中。 (例如,在任何给定标签上仅显示“ 192.168.0.10”,而不是存储从“ 192.168.0.1”,“ 192.168.0.10”开始的范围的标签)