北大陈斌-Python语言基础与应用D10图形界面和绘图

图形用户界面(GUI)

需安装easygui模块,可以显示各种框与用户交互
安装方式:添加链接描述

easygui常用函数
easygui.egdemo()跳出演示功能的窗体,包含各类函数说明
easygui.msgbox()消息窗口
easygui.choicebox()按钮选项,为用户提供可选择的列表,使用序列(元组或列表)作为选项,choices=[…]
easygui.textbox()显示文本,用于显示文本内容,比msgbox显示区域要大,text参数可以是字符串、列表或元组类型
easygui.passwordbox()与enterbox()类似,但输入密码以“***”形式显示
easygui.fileopenbox()打开文件,用户任意指定文件

海龟作图

turtle module:从LOGO语言借鉴而来
属性:位置、方向、画笔(颜色、线条宽度)
指令
1、画笔运动命令:前/后移动,左/右转动,作画速度等
2、画笔控制命令:抬起/放下画笔、画笔宽度、颜色、填充颜色

绘制多边形

import turtle
p = turtle.Pen()
p.pencolor('blue')
p.pensize(5)
p.forward(100)  # 最初画笔朝正右
p.left(120)
p.forward(100)
p.left(120)
p.forward(100)
p.left(120)

t = turtle.Turtle()
#w = turtle.Screen()
t.pencolor('blue')
t.pensize(5)
for i in range(4):
    t.forward(100)
    t.right(90)
turtle.done()

绘制树图形
调用递归二叉树

# 从右往左,先画树干,再画右树枝,最后画左树枝
import turtle
def tree(branchLen , t):
    if branchLen > 5:  # 树干长度最小规模
        t.forward(branchLen)  # 画树干
        t.right(20)
        tree(branchLen - 15, t)  # 减小规模,树干减15,再调用一遍tree函数,直到branchLen=0,不满足if,跳出最里面的一层tree函数
        t.left(40)
        tree(branchLen - 15, t)
        t.right(20)
        t.backward(branchLen)   # 海龟回到原来的位置

# 主函数调用递归函数
def main():
    t = turtle.Turtle()  # 生成海龟
    myWin = turtle.Screen()
    t.left(90)
    t.up() # 抬笔
    t.backward(100) # 海龟在图上先后退100个像素
    t.down()
    t.color("green")
    tree(75,t)
    myWin.exitonclick()

main()

绘制分形图
曼德勃罗集、龙形曲线、Hilbert曲线(介于一维和二维)、谢尔宾斯基三角形
谢尔宾斯基三角形

import turtle as t

t.hideturtle() # 隐藏turtle画笔

'''返回a,b两点之间的中点坐标'''
def get_midp(a, b):
    ax, ay = a
    bx, by = b
    return (ax + bx) / 2, (ay + by) / 2

'''绘制三角形并填充颜色'''
def draw_triangle(color, a, b, c):
    ax, ay = a
    bx, by = b
    cx, cy = c
    t.fillcolor(color)
    t.penup()
    t.goto(ax, ay)
    t.pendown()
    t.begin_fill()
    t.goto(bx, by)
    t.goto(cx, cy)
    t.goto(ax, ay)
    t.penup()
    t.end_fill()

'''绘制谢宾斯基三角形定义三角形往下划分的层数depth和三个顶点坐标p'''
def sierpinski(depth,p):
    colorbar = ['green', 'yellow', 'red', 'orange', 'blue', 'black']
    a, b, c = p
    draw_triangle(colorbar[depth], a, b, c)
    if depth > 0:  # 最小绘制规模
        sierpinski(depth - 1, [a, get_midp(a, b), get_midp(c, a)])
        sierpinski(depth - 1, [get_midp(a, b), b, get_midp(b, c)])
        sierpinski(depth - 1, [get_midp(c, a), get_midp(b, c), c])

p = [[-200, -100], [0, 200], [200, -100]]
sierpinski(5, p)
t.done()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值