Python_面向对象.装饰器.GUI

1.面向对象编程

Python支持面向对象编程

类(class):现实世界中一些事物的封装 (如:学生)
类:属性 (如:名字,成绩)

类对象
实例对象

引用:通过引用对类的属性和方法进行操作
实例化:创建一个类的具体实例对象 (如:学生张三)

2.装饰器(decorator)

Code:


#Python OO example

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def introduce(self):
        print("hi! I'm " + self.name)
        print("my grade is: " + str(self.grade))

    def improve(self, amount):
        self.grade = self.grade + amount

jim = Student("jim", 86)
jim.introduce()

jim.improve(10)
jim.introduce()

------------------------

 def add_candles(cake_func):
     def insert_candles():
         return cake_func() + " candles"
         # 传进来的函数结尾 + candles
     return insert_candles

 def make_cake():
     return "cake"

 gift_func = add_candles(make_cake)

 print(make_cake()) #cake 
 print(gift_func()) #cake candles

-------------------------------

 def add_candles(cake_func):
     def insert_candles():
         return cake_func() + " candles"
     return insert_candles

 def make_cake():
     return "cake"

 make_cake = add_candles(make_cake)

 print(make_cake())
 # print(gift_func)

--------------------------

 def add_candles(cake_func):
     def insert_candles():
         return cake_func() + " and candles"
     return insert_candles

  #加了装饰器,函数就变为add_candles()
  #在make_cake()函数主体上加一些小的装饰
 @add_candles 
 def make_cake():
     return "cake"

 make_cake = add_candles(make_cake)

 print(make_cake()) # cake and candles
 # print(gift_func)

3.GUI from tkinter

4.逻辑层

Code:

 #设置GUI
 root = Tk()
 w = Label(root, text = "Guess Number Game")
 w.pack()

 #欢迎消息
 mb.showinfo("Welcome", "Welcome to Guess Number Game")


 #处理信息
 number = 59

 while True:
 #让用户输入信息
     guess = dl.askinteger("Number", "What's your guess?")

     if guess == number:
         # New block starts here
         output = 'Bingo! you guessed it right, but you do not win any prizes!'
         mb.showinfo("Hint: ", output)
         break
         # New block ends here
     elif guess < number:
         output = 'No, the number is a  higer than that'
         mb.showinfo("Hint: ", output)
     else:
         output = 'No, the number is a  lower than that'
         mb.showinfo("Hint: ", output)

 print('Done')

创建简单网页

  1. 下载并安装python 2.7 32 bit

  2. 下载并安装easy_install windows installer (python 2.7 32bit)

  3. 安装 lpthw.web
    windows 命令行:
    C:\Python27\Scripts\easy_install lpthw.web

  4. 创建目录
    C:\Users\plg519\Maizi\TeachingPython\gotoweb\bin

  5. 目录下创建 app.py:

import web

urls = (
  '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        greeting = "Hello World"
        return greeting

if __name__ == "__main__":
    app.run()

6.Windows cmd 运行:
cd C:\Users\plg519\Maizi\TeachingPython\gotoweb\bin
C:\python27\python27 app.py

7.打开浏览器:localhost:8080

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值