Question: I would like to see why the folloing python code is not fully passing when I run a tester on my code: Code: import tkinter
I would like to see why the folloing python code is not fully passing when I run a tester on my code:
Code:
import tkinter as tk
from rectpack import newPacker
import sys
class CustomCanvas:
def initself height: int, width: int:
self.canvas tkCanvaswidthwidth, heightheight, bg'white'
self.canvas.pack
class Rectangle:
def initself height: int, width: int, x: int y: int :
self.height height
self.width width
self.x x
self.y y
def reprself:
return fRectangleheight selfheight width selfwidth x selfx y selfy
def packallrect, canvassize:
# Create a new packer instance
packer newPacker
# Add rectangles to the packer note: dimensions are width x height
for rect in allrect:
packer.addrectrectwidth, rect.height
# Define the dimensions of the canvas
canvaswidth, canvasheight canvassize
# Add the canvas as a bin to the packer
packer.addbincanvaswidth, canvasheight
# Perform the packing
packer.pack
# Retrieve the packed rectangles
packedrectangles
for rect in packer.rectlist:
# Unpack the values: width, height, x y binindex, rotation
width, height, x y binindex, rotation rect
# Adjust dimensions for rotation if needed
if rotation :
# The rectangle was rotated, so swap width and height
packedrectangles.appendRectanglewidth height, x y
else:
# No rotation
packedrectangles.appendRectangleheight width, x y
return packedrectangles
def main:
if lensysargv:
printUsage: python Assignmentpy
sysexit
filepath sysargv
with openfilepathr as file:
lines file.readlines
# Parse canvas size
canvasheight, canvaswidth mapint linesstripsplit
# Create canvas object
canvas CustomCanvascanvasheight, canvaswidth
# Parse the rectangles
rectangles
for line in lines::
height, width mapint line.stripsplit
rectangles.appendRectangleheight width
# Pack rectangles
packedrectangles packrectanglescanvaswidth, canvasheight
# Draw rectangles on canvas
for rect in packedrectangles:
canvas.canvas.createrectangle
rect.x rect.y rect.x rect.width, rect.y rect.height,
outline'black', fill'lightblue'
# Start Tkinter loop
tkmainloop
if namemain:
main
Tester Results:
Import Tests
PASS: Rectangle & pack imported without error
Rectangle Tests
PASS: Creating Rectangle with args
PASS: Testing instantiated Rectangle properties
PASS: Creating Rectangle with args
Pack Tests
Import Tests
PASS: Rectangle & pack imported without error
FAIL: Returned Rectangle was not logically equlivant to given rectangle at index
Import Tests
PASS: Rectangle & pack imported without error
PASS: fill packed correctly
Import Tests
PASS: Rectangle & pack imported without error
PASS: fill packed correctly
Import Tests
PASS: Rectangle & pack imported without error
PASS: fill packed correctly
Import Tests
PASS: Rectangle & pack imported without error
PASS: fill packed correctly
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
