61.0k views
5 votes
Q3 – Please decide the result of the following script.

import turtle
S1 = turtle.Turtle()
for i in range(20):
S1.forward(i * 10)
S1.right(144)
turtle.done()

A. Many stars.
B. A spiraling star
C. Many circles.
D. Five ovals.

Q4. Please complete the following script so it will create a button on the canvas.
from tkinter import *
tk = Tk()
btn = Button(tk, text="click me")


A. btn.pack()
B. tk.pack()
C. Button.fresh()
D. btn.pack(“update”)

Q5. How to create a line from (0,0) to (500,500) using tkinter? Given the following script for setting the environment.
from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()

A. canvas.drawLine(0,0,500,500)
B. canvas.create_line(0, 0, 500, 500)
C. goto(500,500)
D. pen.goto(500,500)

Q6. How would you create a triangle with tkinter? (Given similar setting in question 5).
A. canvas.create_triangle(10, 10, 100, 10, 100, 110, fill="",outline="black")
B. canvas.create_line(10, 10, 100, 10, 100, 110,)
C. canvas.polygon(10, 10, 100, 10, 100, 110, outline="black")
D. canvas.create_polygon(10, 10, 100, 10, 100, 110, fill="",outline="black")

Q7. How to change an object to a different color? Please choose the right code.
>>> from tkinter import *
>>> tk = Tk()
>>> canvas = Canvas(tk, width=400, height=400)
>>> canvas.pack()
>>> myObject = canvas.create_polygon(10, 10, 10, 60, 50, 35, fill='red')
A. canvas.itemconfig(myObject, fill='blue')
B. myObject.fill(“blue”)
C. canvas.myObject(fill=”blue”)
D. canvas.pack(fill=”blue”)

Question 8-9 – Given the definition of the ball class, please answer the following 2 questions (assuming the canvas object has been created already):
class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
def draw(self):
self.canvas.move(self.id, 0, -1)

Q8 – Please pick the correct way to create a blue ball object.
A. blueBall = Ball(canvas, “blue”)
B. blueBall = draw(Ball, “blue”)
C. blueBall = Ball(self, canvas, “blue”)
D. blueBall = Ball(canvas, “blue”)

Q9 – Please add a new attribute “size” to the Ball class definition. Choose the right way below:
A. def __init__(self, canvas, color, size):
self.size = size
B. def __init__(self, canvas, color, size):
ball.size = size
C. def __init__(self, canvas, color):
self.size = size
D. def __init__(canvas, color, size):
self.size = size

1 Answer

3 votes

Answer:

  1. Based on the given script, the result would be B. A spiraling
  2. The correct answer is A. btn.pack(). To create a button on the canvas, you need to specify its position within the Tkinter window. Here is the completed script :

star.from tkinter import *

tk = Tk()

btn = Button(tk, text="click me")

btn.pack()

tk.mainloop()

3. To create a line from (0, 0) to (500, 500) using Tkinter, you can use the `create_line()` method of the `Canvas` widget. Here's how you can modify the given script to create the line:

```python

from tkinter import *

tk = Tk()

canvas = Canvas(tk, width=500, height=500)

canvas.pack()

# Create the line

canvas.create_line(0, 0, 500, 500)

tk.mainloop()

```

4. To create a triangle using Tkinter, you can use the `create_polygon()` method of the `Canvas` widget. Here's how you can modify the given script to create a triangle:

```python

from tkinter import *

tk = Tk()

canvas = Canvas(tk, width=500, height=500)

canvas.pack()

# Create the triangle

triangle_coords = [250, 100, 100, 400, 400, 400]

canvas.create_polygon(triangle_coords, outline='black', fill='red')

tk.mainloop()

```

5. To change the color of an object created using Tkinter's `create_polygon()` method, you can use the `itemconfig()` method of the `Canvas` widget. Here's the correct code to change the color of the object to a different color:

```python

from tkinter import *

tk = Tk()

canvas = Canvas(tk, width=400, height=400)

canvas.pack()

myObject = canvas.create_polygon(10, 10, 10, 60, 50, 35, fill='red')

# Change the color of the object

canvas.itemconfig(myObject, fill='blue')

tk.mainloop()

```

6. To create a blue ball object using the given Ball class, you can use the following code:

```python

canvas = Canvas(tk, width=500, height=500)

canvas.pack()

blue_ball = Ball(canvas, "blue")

```

7. The correct way to add a new attribute "size" to the Ball class definition is option A:

```python

def __init__(self, canvas, color, size):

self.size = size

```

Step-by-step explanation:

  1. The script creates a turtle object named S1 and then uses a for loop to iterate 20 times. In each iteration, the turtle moves forward by a distance that increases with each iteration (i * 10), and then turns right by 144 degrees. This pattern of movement creates a spiral shape resembling a star.
  2. The pack() method is used to organize and display the button within the Tkinter window. By calling btn.pack(), the button will be positioned based on the default packing rules.
  3. By adding the line `canvas.create_line(0, 0, 500, 500)`, you are instructing the canvas to draw a line from the coordinates (0, 0) to (500, 500).
  4. In this example, the `create_polygon()` method is used to create a polygon shape, which can be used to create a triangle. The `triangle_coords` variable holds the coordinates of the triangle's three vertices (x1, y1, x2, y2, x3, y3).You can modify the values in `triangle_coords` to adjust the position and shape of the triangle. The `outline` parameter sets the color of the outline of the triangle, and the `fill` parameter sets the color of the triangle's interior.
  5. The line `canvas.itemconfig(myObject, fill='blue')` is used to modify the fill color of the object. In this case, it changes the color to blue. You can replace `'blue'` with any other valid color name or color code to achieve the desired color for your object.
  6. By passing the canvas object and the color "blue" as arguments when creating a new instance of the Ball class (`Ball(canvas, "blue")`), you will create a blue ball object on the canvas. The `canvas` object is assumed to be already created and assigned to the `tk` variable.
  7. In this option, the "size" attribute is included as a parameter in the `__init__` method, and it is assigned to `self.size`. This allows each instance of the Ball class to have its own "size" attribute, which can be accessed and modified as needed.

User Roy Berris
by
7.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.