零基础学Python图文版 by Craig Richardson

读入字符串并输出

# 读入字符串输入:input
name = input("what's your name ?") # tom
print("Hi,",name) #  ---> 有空格
print("Hi,{}".format(name))
# what's your name ?>? tom
# Hi, tom
# Hi,tom

画楼梯

import turtle
# pycharm 中直接运行turtle 会有问题,可以从命令行调用脚本
# turtle文档: https://docs.python.org/zh-cn/3/library/turtle.html

# 画楼梯
screen_high_wide=600
window_high_wide=screen_high_wide+50

# 默认位置为(0,0),在屏幕中间,可以看作完整直角坐标系图形
# turtle.setup(width=_CFG['width'], height=_CFG['height'], startx=_CFG['leftright'], starty=_CFG['topbottom'])


turtle.screensize(screen_high_wide,screen_high_wide) # 设置画布大小,建议小于窗口大小以避免出现滚动条
turtle.setup(window_high_wide,window_high_wide) # 设置主窗口的大小和位置

# 跳转到左上角
turtle.penup()
turtle.goto(-screen_high_wide/2,screen_high_wide/2)
turtle.pendown()

# 设置楼梯宽度,高度,个数
every_wide=30
every_high=20
steps = screen_high_wide//every_wide

print("楼梯数:{}".format(steps))
for i in range(steps):
    turtle.forward(every_wide)
    turtle.right(90)
    turtle.forward(every_high)
    turtle.left(90)

turtle.exitonclick() # 该语句让程序等待用户点击界面后才能够退出

画直角的螺旋

import turtle
# pycharm 中直接运行turtle 会有问题,可以从命令行调用脚本
# turtle文档: https://docs.python.org/zh-cn/3/library/turtle.html

# 从屏幕中间画螺旋形
screen_high_wide=600
window_high_wide=screen_high_wide+50

# 默认位置为(0,0),在屏幕中间,可以看作完整直角坐标系图形
# turtle.setup(width=_CFG['width'], height=_CFG['height'], startx=_CFG['leftright'], starty=_CFG['topbottom'])

turtle.screensize(screen_high_wide,screen_high_wide) # 设置画布大小,建议小于窗口大小以避免出现滚动条
turtle.setup(window_high_wide,window_high_wide) # 设置主窗口的大小和位置

# |x| + wide, |y| + wide 不能超过 screen_high_wide/2
x=0
y=0
wide = 5
wide_step = 10  # 螺旋增长的步长

while True:
    x,y=turtle.pos()
    if abs(x) + wide_step>=screen_high_wide/2:
        break # 跳出while循环
    if abs(y) + wide_step>=screen_high_wide/2:
        break
    turtle.fd(wide)
    turtle.left(90) # 逆时针画螺旋
    wide +=wide_step

turtle.exitonclick() # 该语句让程序等待用户点击界面后才能够退出

画同心圆

import turtle
import math
# pycharm 中直接运行turtle 会有问题,可以从命令行调用脚本
# turtle文档: https://docs.python.org/zh-cn/3/library/turtle.html

# 从屏幕中间画螺旋形
screen_high_wide=600
window_high_wide=screen_high_wide+50

# 默认位置为(0,0),在屏幕中间,可以看作完整直角坐标系图形
# turtle.setup(width=_CFG['width'], height=_CFG['height'], startx=_CFG['leftright'], starty=_CFG['topbottom'])

turtle.screensize(screen_high_wide,screen_high_wide) # 设置画布大小,建议小于窗口大小以避免出现滚动条
turtle.setup(window_high_wide,window_high_wide) # 设置主窗口的大小和位置

# |x| + wide, |y| + wide 不能超过 screen_high_wide/2
x=0
y=0
r = 5 # 第一个圆的半径
wide_step = 30  # 圆的半径增长的步长

def circle(r):
    # 画半径为r的圆
    turtle.penup()
    turtle.goto(0,r)
    turtle.pendown()
    zc = 2 * math.pi * r
    step = zc / 360
    for i in range(360):
        turtle.fd(step)
        turtle.right(1)

turtle.delay(0)
while True:
    if r+wide_step>screen_high_wide/2:
        break
    circle(r)
    r+=wide_step

turtle.exitonclick() # 该语句让程序等待用户点击界面后才能够退出

带颜色填充的版本:

import random
import turtle
import math

# pycharm 中直接运行turtle 会有问题,可以从命令行调用脚本
# turtle文档: https://docs.python.org/zh-cn/3/library/turtle.html

# 从屏幕中间画螺旋形
screen_high_wide = 600
window_high_wide = screen_high_wide + 50

# 默认位置为(0,0),在屏幕中间,可以看作完整直角坐标系图形
# turtle.setup(width=_CFG['width'], height=_CFG['height'], startx=_CFG['leftright'], starty=_CFG['topbottom'])

turtle.screensize(screen_high_wide, screen_high_wide)  # 设置画布大小,建议小于窗口大小以避免出现滚动条
turtle.setup(window_high_wide, window_high_wide)  # 设置主窗口的大小和位置

# |x| + wide, |y| + wide 不能超过 screen_high_wide/2
x = 0
y = 0
r = screen_high_wide // 2  # 第一个圆的半径
wide_step = 10  # 圆的半径增长的步长
COLORS = ['snow', 'ghost white', 'white smoke', 'gainsboro', 'floral white', 'old lace',
          'linen', 'antique white', 'papaya whip', 'blanched almond', 'bisque', 'peach puff',
    'navajo white', 'lemon chiffon', 'mint cream', 'azure', 'alice blue', 'lavender',
    'lavender blush', 'misty rose', 'dark slate gray', 'dim gray', 'slate gray',
    'light slate gray', 'gray', 'light grey', 'midnight blue', 'navy', 'cornflower blue', 'dark slate blue',
    'slate blue', 'medium slate blue', 'light slate blue', 'medium blue', 'royal blue',  'blue',
    'dodger blue', 'deep sky blue', 'sky blue', 'light sky blue', 'steel blue', 'light steel blue',
    'light blue', 'powder blue', 'pale turquoise', 'dark turquoise', 'medium turquoise', 'turquoise',
    'cyan', 'light cyan', 'cadet blue', 'medium aquamarine', 'aquamarine', 'dark green', 'dark olive green',
    'dark sea green', 'sea green', 'medium sea green', 'light sea green', 'pale green', 'spring green',
    'lawn green', 'medium spring green', 'green yellow', 'lime green', 'yellow green',
    'forest green', 'olive drab', 'dark khaki', 'khaki', 'pale goldenrod', 'light goldenrod yellow',
    'light yellow', 'yellow', 'gold', 'light goldenrod', 'goldenrod', 'dark goldenrod', 'rosy brown',
    'indian red', 'saddle brown', 'sandy brown',
    'dark salmon', 'salmon', 'light salmon', 'orange', 'dark orange',
    'coral', 'light coral', 'tomato', 'orange red', 'red', 'hot pink', 'deep pink', 'pink', 'light pink',
    'pale violet red', 'maroon', 'medium violet red', 'violet red',
    'medium orchid', 'dark orchid', 'dark violet', 'blue violet', 'purple', 'medium purple',
    'thistle']


def circle(r):
    # 画半径为r的圆
    turtle.penup()
    turtle.goto(0, r)
    turtle.pendown()
    zc = 2 * math.pi * r
    step = zc / 360

    # 填充颜色
    color = random.choice(COLORS)
    turtle.fillcolor(color)
    turtle.color(color)
    turtle.begin_fill()

    for i in range(360):
        turtle.fd(step)
        turtle.right(1)

    turtle.end_fill()


turtle.delay(0)
turtle.hideturtle() # 隐藏箭头
while True:
    if r<0:
        break
    circle(r)
    r -= wide_step # 从外到内覆盖颜色



turtle.exitonclick()  # 该语句让程序等待用户点击界面后才能够退出

效果:

<p>零基础学Python图文版 by Craig Richardson</p>

后续:零基础学Python图文版 by Craig Richardson(2)

Tkinter

正文完
 
评论(没有评论)