sardine2 / python

Life is short, I need python.
0 stars 0 forks source link

ch2 task #12

Open sardine2 opened 7 years ago

sardine2 commented 7 years ago

Why do I need self when I make init or other functions for classes? If you don't have self, then code like cheese = 'Frank' is ambiguous. That code isn't clear about whether you mean the instance's cheese attribute, or a local variable named cheese. With self.cheese = 'Frank' it's very clear you mean the instance attribute self.cheese.

廖雪峰

如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线,在Python中,实例的变量名如果以开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问,所以,我们把Student类改一改:

class Student(object):

    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score(self):
        print('%s: %s' % (self.__name, self.__score))

一个实例详解


The Tkinter Grid Geometry Manager


tutorial

sardine2 commented 7 years ago

object-oriented-p

what is .ttk ?? py2.0 or py3.0?


how to import tinter?

1.要么全导入:

from tkinter import *
root = Tk()

2.要么单独导入:

from tkinter import Tk
root = Tk()

3.要么只导入模块名称,然后引用时加模块名称做前缀:

import tkinter
root = tkinter.Tk()

就是说你得让python知道去哪里找这个对象。

sardine2 commented 7 years ago

from tkinter import*import tinter as tk 的区别?教练推荐后者。


What exactly does “import *” import?

The advantage of from X import * is that it allows you to be lazy. The problem of it is that it will bite you in the ass for lazy :)

sardine2 commented 7 years ago
  1. 在 ch2.py 基础上改写
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=root.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()
sardine2 commented 7 years ago

本来觉得大猫把ch1每个功能都写成函数已经够呛,没料到 fatfox2016 把 ch1 全部用 class 进行了封装,丧心病狂啊!! 然后又倒回去看大猫 ch1 作业,发现还是有看不懂的地方。。。然后巩固了一下 len() 关于 str 以及 list 的不同点。 唉,好惭愧,也好残酷。