在Python中区分同一类的对象

你好,我正在尝试构建一个房间计划器,我正在努力寻找一种在房间中不同对象之间进行区分的方法

以下是制作对象和窗口的代码:

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,50.0)],"Stove"))
        self.objects.append(Object(self,[(150.0,150.0),(150.0,200.0),(200.0,150.0)],"Table"))

Room()
mainloop()

这是我用来在画布上移动对象的代码,我认为这可能会导致问题 (自身是指对象类)

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,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)
changsheng2 回答:在Python中区分同一类的对象

您可以使用id()

唯一地标识python对象。

https://docs.python.org/3/library/functions.html#id

本文链接:https://www.f2er.com/3072569.html

大家都在问