"The easiest Python introductory class" modified

Text correction

I have recommended the exercises in "The easiest Python introductory class" [^ 1]. This text carefully explains how each code is located there. So it's a very good text for beginners to read carefully to understand coding. However, there are some deviations from the common sense of coding, probably because it emphasizes the flow of logic. This is the correction.

[^ 1]: "The easiest Python introductory class" by Fumitaka Osawa, (Sotec Publishing, 2017).

Modification of example07-08-01.py (tag version)

The code around here is bad.

If you do it for a long time or do a lot, you will be able to enjoy it. At first, I gave up because of cpu, but it was too late. .. ..

I had an idea for a fix somewhere, but I've fixed it a long time ago, so I don't know what was the original. -Tags and Bindings Is the same as the first idea.

The problem is,

example07-08-01.py


    canvas.create_oval(self.x - 20, self.y - 20,
    self.x + 20, self.y + 20, fill="white", width=0)

is. I overwrote it with'white'and erased it. This is the usual way to draw a picture with code. It is efficient when writing by managing the entire screen as pict. However, if you do this for the object, the object will proliferate more and more. As a result, the required memory increases and drawing becomes sluggish. maybe

Why delete object correctly.

First, create with tag for identification with create_oval.

    canvas.create_oval(self.x - 20, self.y - 20,
                       self.x + 20, self.y + 20, 
                       fill=self.color, width=0, 
                       tag=self.color)

Instead of overwriting it with white, delete it as follows.

    canvas.delete(self.color)

Please try this. It should be very fast.

However, in windows, the shape may feel distorted. This seems to be due to the drawing library. It's perfect for the mac version.

Further orthodox (obj version)

Furthermore, the idea of erasing an object seems to be better than identifying it with a tag.

Basically,

    canvas.delete(self.ball)
    ...Omission...
    self.ball = canvas.create_oval(self.x - 20, self.y - 20,
                       self.x + 20, self.y + 20, 
                       fill=self.color, width=0)

Just make it an object called self.ball and delete it. However, from the original text, it is necessary to change the order of codes considerably.

I will add a completed form, so if you have time, please worry about what is going on. The order is

  1. Try to fix it as above
  2. I get angry if I don't have self.ball, so make it with init
  3. I'm angry without canvas, so bring it before generating balls is.
import tkinter as tk
speed = 10
class Ball:
  global speed
  def __init__(self, x, y, dx, dy, color):
    self.x = x
    self.y = y
    self.dx = dx
    self.dy = dy
    self.color = color
    self.ball = canvas.create_oval(self.x - 20, self.y - 20,
                       self.x + 20, self.y + 20, 
                       fill=self.color, width=0)

  def test(self):
    print(self.x)
    print(self.y)
  def move(self, canvas):
    canvas.delete(self.ball)
    self.x = self.x + self.dx
    self.y = self.y + self.dy
    self.ball = canvas.create_oval(self.x - 20, self.y - 20,
                       self.x + 20, self.y + 20, 
                       fill=self.color, width=0)
    if self.x >= canvas.winfo_width():
      self.dx = -speed
    if self.x < 0:
      self.dx = +speed
    if self.y >= canvas.winfo_height():
      self.dy = -speed
    if self.y < 0:
      self.dy = +speed

root = tk.Tk()
root.geometry("600x400")

canvas = tk.Canvas(root, width=600, height=400, bg="white")
canvas.place(x=0, y=0)

balls = [
  Ball(300, 400, 1, 1, "red"),
  Ball(200, 400, 2, 1, "green"),
  Ball(300, 200, 1, 2, "blue")
]
def loop():
  for b in balls:
    b.move(canvas)
  root.after(10, loop)

 
root.after(10, loop)
root.mainloop()

Recommended Posts

"The easiest Python introductory class" modified
Examine the object's class in python
"Kanrika" python class
Python class, instance
#Python basics (class)
Try to solve the Python class inheritance problem
Python: Prepare a serializer for the class instance:
The easiest way to use OpenCV with python
python syslog wrapper class
case class in python
Find out the location of Python class definition files.
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
the zen of Python
A python implementation of the Bayesian linear regression class
[Python] Class inheritance, override
python subprocess wrapper class
[Python] Split the date
Why is the first argument of [Python] Class self?
YOLO Python wrapper class
Class notation in Python
Python exception class list
How to use the __call__ method in a Python class
How to deploy the easiest python textbook pybot on Heroku
[Python] Which is executed first, the class variable or __init__?
Treat the Interface class like that with Python type annotations
python3 Measure the processing speed.
Python: Class and instance variables
Towards the retirement of Python2
Download the file in Python
Find the difference in Python
C / C ++ programmer challenges Python (class)
Compare the Python array-like guys
About the Python module venv
About the ease of Python
About the enumerate function (python)
[Python] Adjusting the color bar
Python class member scope summary
[Python] Get the previous month
Python class variables and instance variables
[Python 2/3] Parse the format string
Python Programming Workshop-Super Introductory Vol.4
Call the API with python3.
About the features of Python
[Python] Check the installed libraries
I downloaded the python source
The Power of Pandas: Python
Find the maximum python (improved)
Understanding the python class Struggle (1) Let's move it for the time being
New Python grammar and features not mentioned in the introductory book