Section 2 :Why have to init and self everything ?

BEHOLD, how I struggled for these "things" of GUI~

That part is just the record of my learning at random,unsystematic, full of emotion, it's obviously no necessary to read it, unless you want to have fun.

1.Everything about CLASS.

I know nothing about class, so fist step: Read the python.org 9.3. A First Look at Classes to know basic concepts:

(1) def CLASS

Generally speaking,CLASS is the sum of abstract concepts of the series of common features and behavior of things.

Class is the abstract template, and an instance is a concrete "object" created from a class.

so far is ok.

(2) To know attribute reference

If you enter '.' behind the name of CLASS, IDE will imagine the attribute which we def,that's the attribute reference. The attribute will be referred by all the objects.

WHY?

I found the answer in THINKPYTHON 2e - 3.2 Math functions

To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.

After created the class, use the standard syntax as "object.new_atrr(something you want to add )" to assign values to the object that returns a new instance of the class.

e.g.object.new_atrr(apple_for_Japan.local_logo = 富士大苹果)

A new instance of the class is called Instance Attribute.

(3) If the attribute of class to be assigned again, does it affect the attribute references of class?

I ran the TestA.py to verify the actual response:

class TestA:`
    attr = 1 # def the attribute of class`
    obj_a = TestA()`
    TestA.attr = 42 # assign`
print(obj_a.attr)

42

42

Ok,now I know the answer.

(4) What if the attribute of class and instance get the same heading,which one should be the attribute reference ?

Try:

class TestA:

attr = 1 #

def _init_(self):

self.attr = 42 # the same heading as the attr

obj_a = TestA()

print(obj_a.attr)

>>1

??

The answer should be 42?!

According to the book, the following attribute reference should be the attribute of instance. The instance attribute precedence is higher than the class ; but why is my class attribute masking the instance attribute?

Ok, I can't stop here,I'll be back later……

###Step 2. __init_ ?What the hell is that?!

why we have to 'initialize' the base class part ?

why we have to write like this:

def __init__(self, name, score):

self.name = name

self.score = score

Why self is everywhere?

WHY??

What will happen when **init** something? Does that mean the system to clear the Attribute of the object?

I am quite confused about this point,and it's killing me while reading the docs.

Check the Python's documentation again:

> The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__().

I thought it means:

>我们通过实现__init__()方法来初始化对象。当一个对象被创建,Python首先创建一个空对象,然后为那个新对象调用__init__()方法。

And I read [liaoxuefeng.com:](http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319117128404c7dd0cf0e3c4d88acc8fe4d2c163625000\)

> 由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。通过定义一个特殊的__init__方法,在创建实例的时候,就把name,score等属性绑上去:

class Student(object):

def __init__(self, name, score):

self.name = name

self.score = score

> __init__方法的第一个参数永远是self,表示创建的实例本身,因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。有了__init__方法,在创建实例的时候,就不能传入空的参数了,必须传入与__init__方法匹配的参数。和普通的函数相比,在类中定义的函数只有一点不同,就是第一个参数永远是实例变量self,并且,调用时,不用传递该参数。除此之外,类的方法和普通函数没有什么区别,所以,你仍然可以用默认参数、可变参数、关键字参数和命名关键字参数。

ok,althought I don't actually know what liaoxuefeng told, just try:

class fruit:

formula = ['vc','sugar','water','carbohydrate']

def eat(self,how_many)

AND the below I saw in the python.org doc:

>If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self, [args...]).

explicitly???

I know the Suggestions in "The Zen of Python":**Explicit is better than implicit.**

But I still don't understand the **init**.

I need some help,so I asked the programmer friend,he said:

>初始化一个实例,会默认把第一个参数赋予给name,第二个参数给score,比如 Jia A(John, 100),就说明实例A.Name = John,A.score = 100,相当于这样写程序知道把第一个参数给name, 第二个给score。也就是知道他的地址。

Repect again?

已经上了一天班筋疲力尽只想赶紧回家的程序猿用他关爱智障般的关切眼神看了我一会,说:

> GO to read the chapter one and two of the C++ Textbook,any edition is ok,the primer one for the collager is just fine.Sinece you are a Master of Art,**MAYBE** you can understand what you see in the book.OR,you can just buy the book named :YES!Just coding like a kid.

I will take the second option!

Anyway, I have to jumpped clear of init.There is no time for me to cry, I have to move on!

If you need self.anything, then I will **self.**,even I don't know what it actually mean.


##Today(20170128), while I am writing this note

I know what happened to theabove, what does __init__() mean:

__init__() 这个方法函数通常用来创建对象的实例变量并执行任何其他一次性处理。

The syntax for invoking a method is different from the syntax for calling a function.

And something else:

除了必写的self参数之外,"_init()"同样可以有自己的参数,同时也不需要这样obj.init()方式来调用(因为是自动执行),而是在实例化的时候往类后面的括号中放进参数,相应的所有参数都会传递到这个特殊的 init_()方法中,和函数的参数用法完全相同。

##STEP 3:Tkinter

Tkinter provides 3 different mechanisms for arranging widgets:grid /pack/ place.

what's the different? which one is the most easiest way ?

No no no,I don't ask for the best or the BEST FIT, I just want to finish ASAP.

###1.What is GUI?

>Python is an object-oriented programming language, which means that it provides features that support object-oriented programming.

SO GUI is important.

To creat a GUI, I have to know:

  1. widget: the elements that make up a GUI, including:

Frame:A container, often invisible, that contains other widgets.

Button:A widget, containing text or an image, that performs an action when pressed.

Entry:A region where users can type text: text or ????

Canvas and Scrollbar will be skipped, **I just want to do it as simple as possible.**

  1. option:A value that controls the appearance or function of a widget.

  2. event:A user action, like a mouse click or key press, that causes a GUI to respond.

  3. pack:To arrange and display the elements of a GUI.

At first I chose **pack** not **grid**, then I found I have to write so much code like these:

queryButton = tk.Button(app,text="Query",width = XXX, height=XX,command=query)

queryButton.pa ......

and then I crushed, then shifting to **grid**(That's other miserable story~).

5.Binding:An association between a widget, an event, and an event handler.

6.The event handler is called when the event occurs in the widget,but I don't know how to realize the function, to make it happen, how to relate Tkinter and CLASS.

just following the ***[A Status Bar Class (File: tkSimpleStatusBar.py)](http://effbot.org/tkinterbook/tkinter-application-windows.htm\)\*\*\* example, I got this:

A Status Bar Class (File: tkSimpleStatusBar.py)

class StatusBar(Frame):

def __init__(self, master):

Frame.__init__(self, master)

self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)

self.label.pack(fill=X)

def set(self, format, *args):

self.label.config(text=format % args)

self.label.update_idletasks()

> The set method works like C’s printf function; it takes a format string, possibly followed by a set of arguments (a drawback is that if you wish to print an arbitrary string, you must do that as set(“%s”, string)). Also note that this method calls the update_idletasks method, to make sure pending draw operations (like the status bar update) are carried out immediately.

results matching ""

    No results matching ""