区分 Python 中相同 class 的对象
Distiguishing between objects of the same class in Python
你好,我正在尝试构建一个房间规划器,但我正在努力寻找一种方法来区分房间中的不同对象
这是制作对象的代码,window:
from tkinter import *
class Object():
def __init__(self, screen, pos, name):
self.ob_xy = pos
self.ob = screen.room.create_polygon(self.ob_xy)
self.type = name
class Room():
def __init__(self):
self.screen = Tk()
self.screen.geometry("800x500")
self.screen.configure(background = "light blue")
self.room = Canvas(self.screen, width = 500, height= 500)
self.room.grid(row= 0, column = 0)
self.objects = []
self.objects.append(Object(self, [(50.0, 50.0), (50.0, 100.0), (100.0, 100.0), (100.0, 50.0)],"Stove"))
self.objects.append(Object(self, [(150.0, 150.0), (150.0, 200.0), (200.0, 200.0), (200.0, 150.0)], "Table"))
Room()
mainloop()
这是我用来在 canvas 周围移动对象的代码,我认为这可能是导致问题的原因
(self 指的是对象 class)
def make_ob_range(self, ob_xy):
'''finds the corners of the object'''
self.max_x = 0.0
self.max_y = 0.0
self.min_x = 1000000.0
self.min_y = 1000000.0
for pos in range(len(ob_xy)):
coord = ob_xy[pos]
if (pos+1) % 2 == 1:
if coord > self.max_x:
self.max_x = coord
elif coord < self.min_x:
self.min_x = coord
else:
if coord > self.max_y:
self.max_y = coord
elif coord < self.min_y:
self.min_y = coord
self.mid_x = (self.max_x - self.min_x)/2 + self.min_x
self.mid_y = (self.max_y - self.min_y)/2 + self.min_y
###The following coordinates work with moving the object around the canvas###
def on_object(self, screen):
"""figures out if the mouse is on the object"""
self.find_mouse_loc(self, screen)
coords = screen.room.coords(self.ob)
self.make_ob_range(self, coords)
if self.mousex < self.max_x and self.mousex > self.min_x and self.mousey < self.max_y and self.mousey > self.min_y:
self.object_moving = True
self.move_object(self, screen)
def move_object(self, screen):
"""moves the object around after the mouse"""
while self.object_moving:
coords = screen.room.coords(self.ob)
self.make_ob_range(self, coords)
if self.min_x < 0 or self.max_x > 500 or self.min_y < 0 or self.max_y > 500:
self.object_moving = False
self.move_object_into_canvas(self, screen, coords)
else:
self.movex = self.mousex - self.mid_x
self.movey = self.mousey - self.mid_y
self.mid_x += self.movex
self.mid_y += self.movey
#moves the object
screen.room.move(self.ob, self.movex, self.movey)
screen.room.update()
#resets the mouse position and calculates the new coordinates
self.find_mouse_loc(self, screen)
您可以使用 id()
唯一标识 python 个对象
你好,我正在尝试构建一个房间规划器,但我正在努力寻找一种方法来区分房间中的不同对象
这是制作对象的代码,window:
from tkinter import *
class Object():
def __init__(self, screen, pos, name):
self.ob_xy = pos
self.ob = screen.room.create_polygon(self.ob_xy)
self.type = name
class Room():
def __init__(self):
self.screen = Tk()
self.screen.geometry("800x500")
self.screen.configure(background = "light blue")
self.room = Canvas(self.screen, width = 500, height= 500)
self.room.grid(row= 0, column = 0)
self.objects = []
self.objects.append(Object(self, [(50.0, 50.0), (50.0, 100.0), (100.0, 100.0), (100.0, 50.0)],"Stove"))
self.objects.append(Object(self, [(150.0, 150.0), (150.0, 200.0), (200.0, 200.0), (200.0, 150.0)], "Table"))
Room()
mainloop()
这是我用来在 canvas 周围移动对象的代码,我认为这可能是导致问题的原因 (self 指的是对象 class)
def make_ob_range(self, ob_xy):
'''finds the corners of the object'''
self.max_x = 0.0
self.max_y = 0.0
self.min_x = 1000000.0
self.min_y = 1000000.0
for pos in range(len(ob_xy)):
coord = ob_xy[pos]
if (pos+1) % 2 == 1:
if coord > self.max_x:
self.max_x = coord
elif coord < self.min_x:
self.min_x = coord
else:
if coord > self.max_y:
self.max_y = coord
elif coord < self.min_y:
self.min_y = coord
self.mid_x = (self.max_x - self.min_x)/2 + self.min_x
self.mid_y = (self.max_y - self.min_y)/2 + self.min_y
###The following coordinates work with moving the object around the canvas###
def on_object(self, screen):
"""figures out if the mouse is on the object"""
self.find_mouse_loc(self, screen)
coords = screen.room.coords(self.ob)
self.make_ob_range(self, coords)
if self.mousex < self.max_x and self.mousex > self.min_x and self.mousey < self.max_y and self.mousey > self.min_y:
self.object_moving = True
self.move_object(self, screen)
def move_object(self, screen):
"""moves the object around after the mouse"""
while self.object_moving:
coords = screen.room.coords(self.ob)
self.make_ob_range(self, coords)
if self.min_x < 0 or self.max_x > 500 or self.min_y < 0 or self.max_y > 500:
self.object_moving = False
self.move_object_into_canvas(self, screen, coords)
else:
self.movex = self.mousex - self.mid_x
self.movey = self.mousey - self.mid_y
self.mid_x += self.movex
self.mid_y += self.movey
#moves the object
screen.room.move(self.ob, self.movex, self.movey)
screen.room.update()
#resets the mouse position and calculates the new coordinates
self.find_mouse_loc(self, screen)
您可以使用 id()