from tkinter import * # lets the user place and rearrange tables in a room #Global constants room_width=500 room_height=450 table_size = 90 # Represents a table with four chairs around it. class Table() : def __init__(self, x, y) : self.xPos = x self.yPos = y self.table_rad = table_size/3 self.chair_rad = table_size/6 #Is the position (x, y) on top of this table def posOn(self, x, y) : left = self.xPos - self.table_rad right= self.xPos + self.table_rad top = self.yPos - self.table_rad bot = self.yPos + self.table_rad return (left <= x and x <= right and top <= y and y <= bot) #Set the position of this table to be (x, y) def setPos(self, x, y) : self.xPos = x self.yPos = y #Draw the table on the canvas def draw(self, canvas) : left = self.xPos - self.table_rad right= self.xPos + self.table_rad top = self.yPos - self.table_rad bot = self.yPos + self.table_rad self.__draw_chair(canvas, left, self.yPos) self.__draw_chair(canvas, right, self.yPos) self.__draw_chair(canvas, self.xPos, top) self.__draw_chair(canvas, self.xPos, bot) canvas.create_rectangle(left, top, right, bot, fill="brown") def __draw_chair(self, canvas, x, y) : left = x - self.chair_rad right= x + self.chair_rad top = y - self.chair_rad bot = y + self.chair_rad canvas.create_oval(left, top, right, bot, fill="black") #----------------------------------------------------------------------------- class Room : #Create a new room, with the canvas it should draw itself on. def __init__(self, canv) : self.canvas = canv self.reset() # reset the room to have no tables. def reset(self) : self.tables = [] self.canvas.delete(ALL) self.canvas.update() # remember the table that the position (x, y) is on, if any def select_table(self, x, y) : self.selected_table = None for table in self.tables : if table.posOn(x, y) : self.selected_table = table return # either create a new table at the mouse position, or # move the selected table to the mouse position def create_or_move_table(self, x, y) : left = x - table_size/2 right= x + table_size/2 top = y - table_size/2 bot = y + table_size/2 if left < 0 or right > room_width or top < 0 or bot > room_height : print("too close to edge", end='\n') else : if self.selected_table is None : self.tables.append(Table(x, y)) else : self.selected_table.setPos(x, y) self.redraw_tables() #redraw all the tables def redraw_tables(self) : self.canvas.delete(ALL) for table in self.tables : table.draw(self.canvas) self.canvas.update() #----------------------------------------------------------------------------- # set up the window with a canvas and the room class Gui : def __init__(self): window= Tk() Label(window, text="Click mouse to move or place tables").pack() button = Button(window, text="Restart", command = self.do_restart) button.pack() canvas = Canvas(window, width=room_width, height=room_height, bg = 'white') canvas.bind("", self.mousepress) canvas.bind("", self.mouserelease) canvas.pack() self.room = Room(canvas) window.mainloop() def do_restart(self): self.room.reset() def mousepress(self, event): self.room.select_table(event.x, event.y) def mouserelease(self,event): self.room.create_or_move_table(event.x, event.y) #------------------------------------------------------------------ # Create the gui. Gui()