Anaconda软件学习Python笔记1——使用turtle画表格

听说镜像下载快中科大镜像网站:http://mirrors.ustc.edu.cn/

官网:https://www.anaconda.com/download/#windows

我用IDM在官网下载也有2-3Mb的,啦啦啦啦

然后按提示安装咯,集成了很多东东

我其实是想用jupyter来的,怎么用呢?\

turtle模块学习

我在《像个计算机科学家一样思考Python》——学习笔记1https://blog.csdn.net/qq_42807924/article/details/82812274中创新的写了一个可以打印r行c列的+-|符号组成的表格,欢迎围观

现在挑战使用海龟来实现同样的功能

1.画一个表格单元

import turtle as t#引入海龟模块

t.screensize(500,500,"white")设置窗口大小,颜色
t.reset()#复位
t.penup()#抬笔
t.goto(-200,200)#移动笔到坐标点,(0,0)在中心
t.pendown()#下笔
for i in range(0,4):
    t.forward(40)#画一段
    t.right(90)#右转

t.hideturtle()#把海龟藏起来

照这样子,我们画表格的思路其实和我们用笔去画的步骤一样吧,先画横线,再画竖线,那不就是表格了吗

 那么我们考虑如何把画布作等分吧,比如四等分

import turtle as t

w=1080
h=720
c=2
r=2
u=v=0

t.setup(w,h)
t.reset()
t.screensize(w,h,"white")
t.clear()
kuang=h/c
chang=w/r
#坐标系转换
x=u-w/2
y=v+h/2
def x_h(u):
    return(u-w/2)
def y_h(v):
    return(v+h/2)


def huaheng(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.forward(w)
    t.penup()
    pass

def huashu(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.right(90)
    t.forward(h)
    t.penup()
    
    
huaheng(x_h(0),y_h(0)-kuang)
    
huashu(x_h(0)+chang,y_h(0))

结果

 那么在后面加上几个循环就搞定了!啦啦啦啦啦啦啦

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 24 13:41:08 2018

@author: Acer
"""

import turtle as t

w=1080
h=720
c=5
r=4
u=v=0

t.setup(w,h)
t.reset()
t.screensize(w,h,"white")
t.clear()
kuang=h/c
chang=w/r
#坐标系转换
x=u-w/2
y=v+h/2
def x_h(u):
    return(u-w/2)
def y_h(v):
    return(v+h/2)


def huaheng(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.forward(w)
    t.penup()
    pass

def huashu(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.right(90)
    t.forward(h)
    t.left(90)
    t.penup()
 
  
'''   
huaheng(x_h(0),y_h(0)-kuang)
    
huashu(x_h(0)+chang,y_h(0))
'''
for i in range(0,c-1):
    x=x_h(0)
    y=y-kuang
    huaheng(x,y)
for j in range(0,r-1):
    x=x+chang    
    y=y_h(0)
    print(x,y)
    huashu(x,y)
    
 

最终代码: 

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 24 13:41:08 2018

@author:吾本虚无
"""

import turtle as t
print("依次输入w,h,c,r,并以','隔开")
w,h,c,r=map(int,input().split(','))
print("窗体长%d,高%d,分割为行%d,列%d"%(w,h,c,r))

u=v=0

t.setup(w,h)
t.reset()
t.screensize(w,h,"white")
t.clear()
t.speed(10)
kuang=h/c
chang=w/r
#坐标系转换
x=u-w/2
y=v+h/2
def x_h(u):
    return(u-w/2)
def y_h(v):
    return(v+h/2)


def huaheng(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.forward(w)
    t.penup()
    pass

def huashu(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.right(90)
    t.forward(h)
    t.left(90)
    t.penup()
 
  
'''   
huaheng(x_h(0),y_h(0)-kuang)
    
huashu(x_h(0)+chang,y_h(0))
'''
for i in range(0,c-1):
    x=x_h(0)
    y=y-kuang
    huaheng(x,y)
for j in range(0,r-1):
    x=x+chang    
    y=y_h(0)    
    huashu(x,y)
    
 

猜你喜欢

转载自blog.csdn.net/qq_42807924/article/details/82828073
今日推荐