python code question - working toward understanding code

Hi there folks!

After getting a wireless transceiver USB dongle to communicate with an arduino MEGA2560 from ....click here..... , I'm deciding to piece together a python script for controlling the arduiino from my desktop windows 8 computer 'PC'. After doing that, the plan is to use the script/code to move my 'zumobot' around (- ie. the little tank-like robot with rubber tracks, based on arduino UNO).

Along the way, I came across code on the internet ('stackoverflow'), that demonstrates how to make our PC keyboard detect more-than-one keyboard key being pressed - simultaneously. I tried this code (script included in this post), and it really works - with GUI 'pong' paddle graphics! It works in python 3..... I ran it with 'IDLE' software. It also works in python 2, but need to change the word 'tkinder' to capital 'Tkinder' before running it.

Now, I know basic python programming (working with arrays, file reading/writing, loops, conditional statements, serial communications etc), but certainly don't yet know about the objects or 'classes' programming. I will be working towards understanding it by gathering information.

A couple of questions I'd like to kindly ask is ---- what does "init" mean in the code below? Like, how come there are these really long underscores on each side of the word 'init'?

The other question is - what does the line "if name == "main": mean? I don't know where name came from, and also where main came from. Not only that.....they have underscores too.

Thanks in advance to all for helping me - to point me in the right direction in understanding this kind of coding style. It looks quite advanced, but at the same time amazes me how it created a GUI window that makes the square paddles move up and down independently.

Thanks all!!

'''Example that demonstrates keeping track of multiple key events'''
from tkinter import *   #named tkinder in python 3, and capital Tkinter in python 2

class Playfield:
    def __init__(self):
        # this dict keeps track of keys that have been pressed but not
        # released
        self.pressed = {}

        self._create_ui()

    def start(self):
        self._animate()
        self.root.mainloop()

    def _create_ui(self):
        self.root = Tk()
        self.p1label = Label(text="press w, s to move player 1 up, down", 
                             anchor="w")
        self.p2label = Label(text="press o, l to move player 2 up, down", 
                             anchor="w")
        self.canvas = Canvas(width=440, height=440)
        self.canvas.config(scrollregion=(-20, -20, 420, 420))

        self.p1label.pack(side="top", fill="x")
        self.p2label.pack(side="top", fill="x")
        self.canvas.pack(side="top", fill="both", expand="true")

        self.p1 = Paddle(self.canvas, tag="p1", color="red", x=0, y=0)
        self.p2 = Paddle(self.canvas, tag="p2", color="blue", x=400, y=0)

        self._set_bindings()

    def _animate(self):
        if self.pressed["w"]: self.p1.move_up()
        if self.pressed["s"]: self.p1.move_down()
        if self.pressed["o"]: self.p2.move_up()
        if self.pressed["l"]: self.p2.move_down()
        self.p1.redraw()
        self.p2.redraw()
        self.root.after(10, self._animate)

    def _set_bindings(self):
        for char in ["w","s","o", "l"]:
            self.root.bind("<KeyPress-%s>" % char, self._pressed)
            self.root.bind("<KeyRelease-%s>" % char, self._released)
            self.pressed[char] = False

    def _pressed(self, event):
        self.pressed[event.char] = True

    def _released(self, event):
        self.pressed[event.char] = False

class Paddle():
    def __init__(self, canvas, tag, color="red", x=0, y=0):
        self.canvas = canvas
        self.tag = tag
        self.x = x
        self.y = y
        self.color = color
        self.redraw()

    def move_up(self):
        self.y = max(self.y -2, 0)

    def move_down(self):
        self.y = min(self.y + 2, 400)

    def redraw(self):
        x0 = self.x - 10
        x1 = self.x + 10
        y0 = self.y - 20
        y1 = self.y + 20
        self.canvas.delete(self.tag)
        self.canvas.create_rectangle(x0,y0,x1,y1,tags=self.tag, fill=self.color)

if __name__ == "__main__":
    p = Playfield()
    p.start()

I'm deciding to piece together a python script

This is NOT the python forum!

PaulS:
This is NOT the python forum!

oh....ok. Oops! Thanks PaulS. Mis-interpreted. I thought it was a programming questions area, as the link says 'using arduino -> programming questions'. Here, I'm using arduino, and my goal is to use python script compiled into an EXE file using pyinstaller - to make my arduino robots move around using a computer keyboard (via a ch340T/nrf24L01+ USB dongle connected to my PC). Not language programming questions obviously.

It's a double underscore. The init one is like a class constructor in C++. It tells it to run that code whenever an instance of the class is created.

name and main have to do with where the code was called from. Maybe someone who knows more about Python than me can answer that one better.

I thought it was a programming questions area. Not language programming questions obviously.

It is a programming questions area. Arduino programming! Not PC programming. There is an Interfacing with software on the PC section, but that isn't even appropriate, because your question has nothing to do with the Arduino or with interfacing it with some software on the PC.

Delta_G:
It's a double underscore. The init one is like a class constructor in C++. It tells it to run that code whenever an instance of the class is created.

name and main have to do with where the code was called from. Maybe someone who knows more about Python than me can answer that one better.

Hi Delta!! Thanks very much! What you indicated really helps tremendously, as I had no idea what that was before. This starts me off and gives me leads. Thanks for that Delta.

PaulS:
It is a programming questions area. Arduino programming! Not PC programming. There is an Interfacing with software on the PC section, but that isn't even appropriate, because your question has nothing to do with the Arduino or with interfacing it with some software on the PC.

Hi Paul! Sorry about that. You were right. I only just found out now. I think this probably needs to go to project guidance. My earlier assumption about the 'programming questions' wasn't right. I know now though. I requested a mod to move my post to project guidance now. Thanks!

You'd probably get a lot better answers if you asked on a python forum instead. What you're doing is like asking on the Chevrolet forum about your Ford. Sure, someone may know something here. But you'd be much better off going where that's what they really do.

Delta_G:
You'd probably get a lot better answers if you asked on a python forum instead. What you're doing is like asking on the Chevrolet forum about your Ford. Sure, someone may know something here. But you'd be much better off going where that's what they really do.

Sounds good Delta_G! Although, I was figuring that there would be people out there that use python to control their arduinos, or assumed that there'd be python experts in the programming area - as I initially assumed this was a general programming area (for all languages, but importantly working with arduino; but Paul is right - this area turns out to be programming arduino boards themselves). Thanks for starting me off though. That was great.

When I entered python "main" into duck-duck-go I got links that seem to give the answer straightaway.

...R

Robin2:
When I entered python "main" into duck-duck-go I got links that seem to give the answer straightaway.

...R

Awesome! Thanks for leading me to this duckduckgo software robin2. I'll download that one and try it out. Much appreciated.

Southpark:
Awesome! Thanks for leading me to this duckduckgo software robin2. I'll download that one and try it out. Much appreciated.

If I was a believer I'd probably say "Gawd help us". It's an alternative to Google! You don't download it.

I'm sure Google is every bit as effective - I just don't normally use it.

...R

Robin2:
If I was a believer I'd probably say "Gawd help us". It's an alternative to Google! You don't download it.

I'm sure Google is every bit as effective - I just don't normally use it.

...R

aha! I had assumed you had some other special python IDE software that offered some back-tracking, leading to other variables connected with the system's code.

This link here has nice information about the line of code I was asking about: ....click here..... (for those that come across the same question and this thread).