Python_杂学系列_1.有趣的小程序

这篇博客分享了使用Python进行趣味编程的四个实例:利用tkinter库创建弹窗恶作剧,用turtle模块绘制爱心、哆啦A梦和小恐龙。代码详细展示了如何通过Python实现图形界面交互和图形绘制,适合初学者提升编程兴趣。
摘要由CSDN通过智能技术生成

系列文章目录


前言

这里有一些有趣的pythonxiao程序如:五角星,爱心,哆啦A梦,小猫等等(都是积累下来的)


1.搞怪的弹窗,自带的tk

import tkinter as tk
import random
import threading
import time
def dow():
    window = tk.Tk()
    width = window.winfo_screenwidth()
    height = window.winfo_screenheight()
    a = random.randrange(0, width)
    b = random.randrange(0, height)
    window.title('你是傻子')
    window.geometry("200x50" +"+" +str(a) +"+" +str(b))
    tk.Label(window,
    text='你就是个傻子!啦啦啦!',# 标签的文字
                bg='Red',# 背景颜色
                font=('楷体',17),# 字体和字体大小
                width=15,height=2  # 标签长宽
                ).pack()# 固定窗口位置
    window.mainloop()
threads = []
for i in range(100):# 需要的弹框数量
    t = threading.Thread(target=dow)
    threads.append(t)
    time.sleep(0.1)
    threads[i].start()

2.爱心

# -*- coding:utf-8 -*-
import turtle
import time
# 画爱心的顶部
def LittleHeart():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2)
# 输入表白的语句,默认I Love you
love = input('请输入表白语句,默认为输入为"I Love you": ')
# 输入署名或者赠谁,没有不执行
me = input('请输入您心上人的姓名或者昵称: ')
if love == '':
    love = 'I Love you'# 窗口大小
turtle.setup(width=800, height=500)# 颜色
turtle.color('red', 'pink')# 笔粗细
turtle.pensize(5)# 速度
turtle.speed(3)# 提笔
turtle.up()# 隐藏笔
turtle.hideturtle()# 去到的坐标,窗口中心为0,0
turtle.goto(0, -180)
turtle.showturtle()# 画上线
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)# 调用画爱心左边的顶部
LittleHeart()# 调用画爱右边的顶部
turtle.left(120)
LittleHeart()# 画下线
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()# 在心中写字 一次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('#CD5C5C', 'pink')# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# 
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('red', 'pink')
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
# 写署名
if me != '':
    turtle.color('black', 'pink')
    time.sleep(2)
    turtle.goto(180, -180)
    turtle.showturtle()
    turtle.write(me, font=(20,), align="center", move=True)
# 点击窗口关闭
window = turtle.Screen()
window.exitonclick()

3.哆啦A梦

# 哆啦A梦
import turtle
def flyTo(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def drawEye():
    turtle.tracer(False)
    a = 2.5
    for i in range(120):
        turtle.left(3)
        if 0 <= i < 30 or 60 <= i < 90:
            a -= 0.05
        else:
            a += 0.05
        turtle.fd(a)
    turtle.tracer(True)

def beard():
    """ 画胡子, 一共六根
    """
    # 左边第一根胡子
    flyTo(-37, 135)
    turtle.seth(165)
    turtle.fd(60)

    # 左边第二根胡子
    flyTo(-37, 125)
    turtle.seth(180)
    turtle.fd(60)

    # 左边第三根胡子
    flyTo(-37, 115)
    turtle.seth(193)
    turtle.fd(60)

    # 右边第一根胡子
    flyTo(37, 135)
    turtle.seth(15)
    turtle.fd(60)

    # 右边第二根胡子
    flyTo(37, 125)
    turtle.seth(0)
    turtle.fd(60)

    # 右边第三根胡子
    flyTo(37, 115)
    turtle.seth(-13)
    turtle.fd(60)


def drawRedScarf():
    """ 画围巾
    """
    turtle.fillcolor("red")  # 填充颜色
    turtle.begin_fill()
    turtle.seth(0)  # 朝向右

    turtle.fd(200)  # 前进10个单位
    turtle.circle(-5, 90)

    turtle.fd(10)
    turtle.circle(-5, 90)

    turtle.fd(207)
    turtle.circle(-5, 90)

    turtle.fd(10)
    turtle.circle(-5, 90)

    turtle.end_fill()

def drawMouse():
    flyTo(5, 148)
    turtle.seth(270)
    turtle.fd(100)
    turtle.seth(0)
    turtle.circle(120, 50)
    turtle.seth(230)
    turtle.circle(-120, 100)

def drawRedNose():
    flyTo(-10, 158)
    turtle.fillcolor("red")  # 填充颜色
    turtle.begin_fill()
    turtle.circle(20)
    turtle.end_fill()

def drawBlackdrawEye():
    turtle.seth(0)
    flyTo(-20, 195)
    turtle.fillcolor("#000000")  # 填充颜色
    turtle.begin_fill()
    turtle.circle(13)
    turtle.end_fill()
    turtle.pensize(6)
    flyTo(20, 205)
    turtle.seth(75)
    turtle.circle(-10, 150)
    turtle.pensize(3)
    flyTo(-17, 200)
    turtle.seth(0)
    turtle.fillcolor("#ffffff")
    turtle.begin_fill()
    turtle.circle(5)
    turtle.end_fill()
    flyTo(0, 0)

def drawFace():
    """
    """
    turtle.forward(183)  # 前行183个单位
    turtle.fillcolor("white")  # 填充颜色为白色
    turtle.begin_fill()  # 开始填充
    turtle.left(45)  # 左转45度
    turtle.circle(120, 100)  # 右边那半边脸
    turtle.seth(90)  # 朝向向上
    drawEye()  # 画右眼睛
    turtle.seth(180)  # 朝向左
    turtle.penup()  # 抬笔
    turtle.fd(60)  # 前行60
    turtle.pendown()  # 落笔
    turtle.seth(90)  # 朝向上
    drawEye()  # 画左眼睛
    turtle.penup()  # 抬笔
    turtle.seth(180)  # 朝向左
    turtle.fd(64)  # 前进64
    turtle.pendown()  # 落笔
    turtle.seth(215)  # 修改朝向
    turtle.circle(120, 100)  # 左边那半边脸
    turtle.end_fill()  #

def drawHead():
    """ 画了一个被切掉下半部分的圆
    """
    turtle.penup()  # 抬笔
    turtle.circle(150, 40)  # 画圆, 半径150,圆周角40
    turtle.pendown()  # 落笔
    turtle.fillcolor("#00a0de")  # 填充色
    turtle.begin_fill()  # 开始填充
    turtle.circle(150, 280)  # 画圆,半径150, 圆周角280
    turtle.end_fill()

def drawAll():
    drawHead()
    drawRedScarf()
    drawFace()
    drawRedNose()
    drawMouse()
    beard()
    flyTo(0, 0)
    turtle.seth(0)
    turtle.penup()
    turtle.circle(150, 50)
    turtle.pendown()
    turtle.seth(30)
    turtle.fd(40)
    turtle.seth(70)
    turtle.circle(-30, 270)
    turtle.fillcolor("#00a0de")
    turtle.begin_fill()
    turtle.seth(230)
    turtle.fd(80)
    turtle.seth(90)
    turtle.circle(1000, 1)
    turtle.seth(-89)
    turtle.circle(-1000, 10)
    turtle.seth(180)
    turtle.fd(70)
    turtle.seth(90)
    turtle.circle(30, 180)
    turtle.seth(180)
    turtle.fd(70)
    turtle.seth(100)
    turtle.circle(-1000, 9)
    turtle.seth(-86)
    turtle.circle(1000, 2)
    turtle.seth(230)
    turtle.fd(40)
    turtle.circle(-30, 230)
    turtle.seth(45)
    turtle.fd(81)
    turtle.seth(0)
    turtle.fd(203)
    turtle.circle(5, 90)
    turtle.fd(10)
    turtle.circle(5, 90)
    turtle.fd(7)
    turtle.seth(40)
    turtle.circle(150, 10)
    turtle.seth(30)
    turtle.fd(40)
    turtle.end_fill()

    # 左手
    turtle.seth(70)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.circle(-30)
    turtle.end_fill()

    # 脚
    flyTo(103.74, -182.59)
    turtle.seth(0)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.fd(15)
    turtle.circle(-15, 180)
    turtle.fd(90)
    turtle.circle(-15, 180)
    turtle.fd(10)
    turtle.end_fill()
    flyTo(-96.26, -182.59)
    turtle.seth(180)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.fd(15)
    turtle.circle(15, 180)
    turtle.fd(90)
    turtle.circle(15, 180)
    turtle.fd(10)
    turtle.end_fill()

    # 右手
    flyTo(-133.97, -91.81)
    turtle.seth(50)
    turtle.fillcolor("#FFFFFF")
    turtle.begin_fill()
    turtle.circle(30)
    turtle.end_fill()

    # 口袋
    flyTo(-103.42, 15.09)
    turtle.seth(0)
    turtle.fd(38)
    turtle.seth(230)
    turtle.begin_fill()
    turtle.circle(90, 260)
    turtle.end_fill()
    flyTo(5, -40)
    turtle.seth(0)
    turtle.fd(70)
    turtle.seth(-90)
    turtle.circle(-70, 180)
    turtle.seth(0)
    turtle.fd(70)

    # 铃铛
    flyTo(-103.42, 15.09)
    turtle.fd(90)
    turtle.seth(70)
    turtle.fillcolor("#ffd200")
    turtle.begin_fill()
    turtle.circle(-20)
    turtle.end_fill()
    turtle.seth(170)
    turtle.fillcolor("#ffd200")
    turtle.begin_fill()
    turtle.circle(-2, 180)
    turtle.seth(10)
    turtle.circle(-100, 22)
    turtle.circle(-2, 180)
    turtle.seth(180 - 10)
    turtle.circle(100, 22)
    turtle.end_fill()
    flyTo(-13.42, 15.09)
    turtle.seth(250)
    turtle.circle(20, 110)
    turtle.seth(90)
    turtle.fd(15)
    turtle.dot(10)
    flyTo(0, -150)
    drawBlackdrawEye()

def main():
    turtle.screensize(800, 6000, "#F0F0F0")
    turtle.pensize(3)
    turtle.speed(9)
    drawAll()

if __name__ == "__main__":
    main()
    turtle.mainloop()

4.画小恐龙

import turtle as t
#龙身
t.setup(1000,600)               # 设置画布的大小
t.speed(10)                     # 设置画笔速度为10
t.pensize(5)                    # 设置画笔大小
t.pencolor("SpringGreen4")      # 设置画笔颜色
t.penup()         # 提笔
t.goto(250,180)   # 画笔前往坐标(250,180)
t.begin_fill()    # 准备填充
t.pendown()       # 落笔
t.seth(120)       # 画笔角度为120°
t.circle(100,140) # 画一个半径为100,角度为140°的圆
t.seth(-96)
t.fd(120)         # 向前移动120
t.circle(-100,50);t.circle(-80,23);t.seth(176);t.fd(20);t.seth(180)
t.circle(-315,40);t.seth(270);t.circle(50,30);t.circle(10,3);t.seth(-60)
t.circle(180,40);t.circle(500,20);t.circle(750,8);t.circle(80,60);t.circle(70,30)
t.fd(90);t.circle(-80,30);t.seth(10);t.fd(60);t.seth(160);t.fd(90);t.seth(22)
t.fd(89);t.color("LightGreen")# 设置填充颜色#t.color('SpringGreen1')
t.end_fill() # 依据轮廓填充

#龙眼
t.pensize(5);t.pencolor("SpringGreen4");t.penup();t.goto(128,165);t.pendown()
t.begin_fill();t.seth(0);t.circle(20,360);t.color((1,1,1))    #填充颜色为白色;t.end_fill()
t.penup();t.goto(128,185);t.pendown();t.seth(0);t.pensize(15);t.pencolor((0,0,0));t.circle(1,360)
t.pensize(5);t.pencolor("SpringGreen4");t.penup();t.goto(177,175);t.pendown()
t.begin_fill();t.seth(0);t.circle(20,360);t.color((1,1,1));t.end_fill();t.penup()
t.goto(177,195);t.pendown();t.seth(0);t.pensize(15);t.pencolor((0,0,0));t.circle(1,360)
#龙爪
#上爪
t.penup();t.pencolor("Aquamarine4");t.goto(195,60);t.pensize(11);t.pendown()
t.seth(10);t.fd(34);t.penup();t.goto(208,66);t.pendown();t.pensize(9);t.seth(45)
t.fd(21);t.penup();t.goto(208,66);t.pendown();t.pensize(9);t.seth(-35);t.fd(21)
#下爪
t.penup();t.goto(171,20);t.pensize(11);t.pendown();t.seth(-3);t.fd(58);t.penup()
t.goto(213,22);t.pendown();t.pensize(9);t.seth(35);t.fd(18);t.penup();t.goto(213,20)
t.pendown();t.pensize(9);t.seth(-55);t.fd(20);t.penup();t.goto(171,20);t.pensize(14)
t.pendown();t.seth(-3);t.fd(39);t.penup();t.goto(195,60);t.pensize(14);t.pendown()
t.seth(10);t.fd(10)
#龙脚#左脚
t.penup();t.goto(71,-100);t.pensize(16);t.pendown();t.seth(-110);t.fd(40)
t.penup();t.goto(55,-140);t.pendown();t.pensize(11);t.seth(-150);t.fd(28)
t.penup();t.goto(55,-140);t.pendown();t.seth(-70);t.fd(23);t.penup();t.goto(55,-140)
t.pendown();t.seth(-20);t.pensize(10);t.fd(23);t.seth(-40);t.pensize(11);t.fd(8)
#右脚
t.penup();t.goto(142,-113);t.pensize(16);t.pendown();t.seth(-80);t.fd(23);t.penup()
t.goto(145,-133);t.pendown();t.pensize(11);t.seth(-120);t.fd(30);t.penup()
t.goto(145,-133);t.pendown();t.pensize(11);t.seth(-70);t.fd(27);t.penup();t.goto(145,-133)
t.pendown();t.pensize(10);t.seth(-27);t.fd(27);t.pensize(11);t.seth(-50);t.fd(8)
#牙齿#上牙
t.penup();t.goto(240,172);t.pendown();t.color("Yellow2");t.pensize(4);t.seth(-110)
t.fd(12);t.seth(120);t.fd(12);t.penup();t.goto(220,165);t.pendown();t.seth(-110)
t.fd(12);t.seth(130);t.fd(12);t.penup();t.goto(200,157);t.pendown();t.seth(-110)
t.fd(12);t.seth(140);t.fd(12);t.penup();t.goto(250,180);t.pensize(5);t.pencolor("SpringGreen4")
t.pendown();t.seth(22);t.bk(88)
#下牙
t.penup();t.goto(200,140);t.pendown();t.color("Yellow2");t.pensize(4);t.seth(45)
t.fd(12);t.seth(-90);t.fd(12);t.penup();t.goto(215,135);t.pendown();t.seth(45)
t.fd(12);t.seth(-90);t.fd(12);t.penup();t.goto(230,130);t.pendown();t.seth(45)
t.fd(12);t.seth(-90);t.fd(12);t.penup();t.goto(251,119);t.pensize(5)
t.pencolor("SpringGreen4");t.pendown();t.seth(160);t.fd(89)
#龙脊
t.penup();t.goto(120,220);t.pensize(5);t.pendown();t.pencolor("SeaGreen")
#自头而尾  14片
t.begin_fill();t.color('SeaGreen');t.seth(160);t.fd(40);t.seth(-60);t.fd(33);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(180);t.fd(40);t.seth(-60);t.fd(33);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(200);t.fd(40);t.seth(-50);t.fd(38);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(205);t.fd(40);t.seth(-50);t.fd(46);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(220);t.fd(40);t.seth(-50);t.fd(41.2);t.end_fill()#5
t.begin_fill();t.color('SeaGreen');t.seth(190);t.fd(40);t.seth(-50);t.fd(40.8);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(168);t.fd(44);t.seth(-89);t.fd(47);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(138);t.fd(33);t.seth(-120);t.fd(28);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(28);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(30);t.end_fill()#10
t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(24);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(118);t.fd(32);t.seth(-120);t.fd(24);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(110);t.fd(32);t.seth(-120);t.fd(24);t.end_fill()
t.begin_fill();t.color('SeaGreen');t.seth(110);t.fd(32);t.seth(-120);t.fd(22);t.end_fill()

#补充没上色的部分
t.penup();t.pensize(7);t.goto(-99,-49);t.pendown();t.seth(-1);t.fd(86);t.seth(0);t.fd(6);t.seth(40)
t.fd(30);t.seth(50);t.fd(48);t.seth(90);t.fd(15);t.done()

5.小小的心意

from turtle import *
pensize(20)
pencolor("red")
fillcolor("pink")
speed(5)
up()
goto(-30,100)
down()
begin_fill()
left(90)
circle(120,180)
circle(360,70)
left(38)
circle(360,70)
circle(120,180)
end_fill()
up()
goto(-100,-100)
down()

尝试

import turtle

##turtle.right(100)
turtle.screensize(400,200,"yellow");turtle.speed(10);turtle.color("red");turtle.circle(50)
turtle.circle(100);turtle.circle(-50);turtle.circle(-100);turtle.speed(8);turtle.goto(100,200)
turtle.color("black");turtle.forward(100);turtle.right(90);turtle.forward(100);turtle.right(90)
turtle.forward(200);turtle.right(90);turtle.forward(100);turtle.right(90);turtle.forward(100)
turtle.dot(5,"green");turtle.home();turtle.goto(-100,200);turtle.color("black");turtle.forward(100)
turtle.right(90);turtle.forward(100);turtle.right(90);turtle.forward(200);turtle.right(90)
turtle.forward(100);turtle.right(90);turtle.forward(100);turtle.dot(5,"green");turtle.home()
turtle.goto(-100,-200);turtle.color("black");turtle.forward(100);turtle.left(90);turtle.forward(100)
turtle.left(90);turtle.forward(200);turtle.left(90);turtle.forward(100);turtle.left(90)
turtle.forward(100);turtle.dot(5,"green");turtle.home();turtle.goto(100,-200);turtle.color("black")
turtle.forward(100);turtle.left(90);turtle.forward(100);turtle.left(90);turtle.forward(200)
turtle.left(90);turtle.forward(100);turtle.left(90);turtle.forward(100);turtle.dot(5,"green")
turtle.home()
turtle.mainloop()


总结

有待更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

joyyi9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值