1.画玫瑰花
import turtle
import time
turtle.speed(5) #画笔移动的速度,数值越大速度越快
# 设置初始位置
turtle.penup() #提起画笔,移动画笔但并不会绘制图形
turtle.left(90) #逆时针转动画笔90度
turtle.fd(200)
turtle.pendown() #放下画笔,移动画笔即开始绘制
turtle.right(90)
#设置画笔的大小
turtle.pensize(2)
# 花蕊
turtle.fillcolor("red") #填充颜色
turtle.begin_fill() #开始填充
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150,80)
turtle.left(50)
turtle.circle(150,90)
turtle.end_fill() #结束填充
# 花瓣1
turtle.left(150)
turtle.circle(-90,70)
turtle.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turtle.circle(-90,40)
# 花瓣2
turtle.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83)
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80,90)
turtle.left(90)
turtle.circle(80,90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,50) #画一个圆 200 是半径,50 是弧度
#不让自动退出,放在程序的最后一行
#不然画画结束后会自动退出
turtle.done()
效果图:
2.画生日蛋糕
import turtle as t
import math as m
import random as r
import pygame
file='Birthday.mp3'
pygame.mixer.init()
track=pygame.mixer.music.load(file)
#()内的2是播放次数(n>0),n=0时播放1次,-1是特殊值,会循环播放,0代表从第0秒开始播放
pygame.mixer.music.play(0,0)
# 不显示绘画过程,直接绘制成功
t.tracer(False)
# 快速绘画
t.speed(10)
t.delay(0)
def drawX(a, i):
angle = m.radians(i)
return a * m.cos(angle)
def drawY(b, i):
angle = m.radians(i)
return b * m.sin(angle)
# 设置背景颜色,窗口位置以及大小
t.bgcolor("#d3dae8")
t.setup(1000, 800)
t.penup()
t.goto(150, 0)
t.pendown()
# 1
t.pencolor("white")
t.begin_fill()
for i in range(360):
x = drawX(150, i)
y = drawY(60, i)
t.goto(x, y)
t.fillcolor("#fef5f7")
t.end_fill()
# 2
t.begin_fill()
for i in range(180):
x = drawX(150, -i)
y = drawY(70, -i)
t.goto(x, y)
for i in range(180, 360):
x = drawX(150, i)
y = drawY(60, i)
t.goto(x, y)
t.fillcolor("#f2d7dd")
t.end_fill()
# 3
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
for i in range(360):
x = drawX(120, i)
y = drawY(48, i)
t.goto(x, y)
t.fillcolor("#cbd9f9")
t.end_fill()
# 4
t.begin_fill()
t.pencolor("#fee48c")
for i in range(540):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.goto(-120, 0)
t.fillcolor("#cbd9f9")
t.end_fill()
# 5
t.pu()
t.goto(120, 70)
t.pd()
t.pencolor("#fff0f3")
t.begin_fill()
for i in range(360):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
# 6
t.pu()
t.goto(110, 70)
t.pd()
t.pencolor("#fff9fb")
t.begin_fill()
for i in range(360):
x = drawX(110, i)
y = drawY(44, i) + 70
t.goto(x, y)
t.fillcolor("#fff9fb")
t.end_fill()
# 7
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
t.pencolor("#ffa79d")
for i in range(180):
x = drawX(120, -i)
y = drawY(48, -i) + 10
t.goto(x, y)
t.goto(-120, 0)
for i in range(180, 360):
x = drawX(120, i)
y = drawY(48, i)
t.goto(x, y)
t.fillcolor("#ffa79d")
t.end_fill()
# 8
t.pu()
t.goto(120, 70)
t.pd()
t.begin_fill()
t.pensize(4)
t.pencolor("#fff0f3")
for i in range(1800):
x = drawX(120, 0.1 * i)
y = drawY(-18, i) + 10
t.goto(x, y)
t.goto(-120, 70)
t.pensize(1)
for i in range(180, 360):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
# 9
t.pu()
t.goto(80, 70)
t.pd()
t.begin_fill()
t.pencolor("#6f3732")
t.goto(80, 120)
for i in range(180):
x = drawX(80, i)
y = drawY(32, i) + 120
t.goto(x, y)
t.goto(-80, 70)
for i in range(180, 360):
x = drawX(80, i)
y = drawY(32, i) + 70
t.goto(x, y)
t.fillcolor("#6f3732")
t.end_fill()
# 10
t.pu()
t.goto(80, 120)
t.pd()
t.pencolor("#ffaaa0")
t.begin_fill()
for i in range(360):
x = drawX(80, i)
y = drawY(32, i) + 120
t.goto(x, y)
t.fillcolor("#ffaaa0")
t.end_fill()
# 11
t.pu()
t.goto(70, 120)
t.pd()
t.pencolor("#ffc3be")
t.begin_fill()
for i in range(360):
x = drawX(70, i)
y = drawY(28, i) + 120
t.goto(x, y)
t.fillcolor("#ffc3be")
t.end_fill()
# 12
t.pu()
t.goto(80, 120)
t.pd()
t.begin_fill()
t.pensize(3)
t.pencolor("#ffaaa0")
for i in range(1800):
x = drawX(80, 0.1 * i)
y = drawY(-12, i) + 80
t.goto(x, y)
t.goto(-80, 120)
t.pensize(1)
for i in range(180, 360):
x = drawX(80, i)
y = drawY(32, i) + 120
t.goto(x, y)
t.fillcolor("#ffaaa0")
t.end_fill()
# 13
t.pu()
t.goto(64, 120)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 60
y = drawY(1, i) + 120
t.goto(x, y)
t.goto(64, 170)
for i in range(540):
x = drawX(4, i) + 60
y = drawY(1, i) + 170
t.goto(x, y)
t.goto(56, 120)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(64, 120 + 10 * i)
t.pu()
t.goto(56, 120 + 10 * i)
t.pd()
t.pu()
t.goto(60, 170)
t.pd()
t.goto(60, 180)
t.pensize(1)
#
t.pu()
t.goto(64, 190)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 60
y = drawY(10, i) + 190
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 14
t.pu()
t.goto(-56, 120)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 60
y = drawY(1, i) + 120
t.goto(x, y)
t.goto(-56, 170)
for i in range(540):
x = drawX(4, i) - 60
y = drawY(1, i) + 170
t.goto(x, y)
t.goto(-64, 120)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(-56, 120 + 10 * i)
t.pu()
t.goto(-64, 120 + 10 * i)
t.pd()
t.pu()
t.goto(-60, 170)
t.pd()
t.goto(-60, 180)
t.pensize(1)
#
t.pu()
t.goto(-56, 190)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 60
y = drawY(10, i) + 190
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 15
t.pu()
t.goto(0, 130)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i)
y = drawY(1, i) + 130
t.goto(x, y)
t.goto(4, 180)
for i in range(540):
x = drawX(4, i)
y = drawY(1, i) + 180
t.goto(x, y)
t.goto(-4, 130)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(4, 130 + 10 * i)
t.pu()
t.goto(-4, 130 + 10 * i)
t.pd()
t.pu()
t.goto(0, 180)
t.pd()
t.goto(0, 190)
t.pensize(1)
#
t.pu()
t.goto(4, 200)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i)
y = drawY(10, i) + 200
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 16
t.pu()
t.goto(30, 110)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 30
y = drawY(1, i) + 110
t.goto(x, y)
t.goto(34, 160)
for i in range(540):
x = drawX(4, i) + 30
y = drawY(1, i) + 160
t.goto(x, y)
t.goto(26, 110)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(34, 110 + 10 * i)
t.pu()
t.goto(26, 110 + 10 * i)
t.pd()
t.pu()
t.goto(30, 160)
t.pd()
t.goto(30, 170)
t.pensize(1)
#
t.pu()
t.goto(34, 180)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 30
y = drawY(10, i) + 180
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
# 17
t.pu()
t.goto(-30, 110)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 30
y = drawY(1, i) + 110
t.goto(x, y)
t.goto(-26, 160)
for i in range(540):
x = drawX(4, i) - 30
y = drawY(1, i) + 160
t.goto(x, y)
t.goto(-34, 110)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(-26, 110 + 10 * i)
t.pu()
t.goto(-34, 110 + 10 * i)
t.pd()
t.pu()
t.goto(-30, 160)
t.pd()
t.goto(-30, 170)
t.pensize(1)
#
t.pu()
t.goto(-26, 180)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 30
y = drawY(10, i) + 180
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
###随机
color = ["#e28cb9", "#805a8c", "#eaa989", "#6e90b7", "#b8b68f", "#e174b5", "#cf737c", "#7c8782"]
for i in range(80):
t.pu()
x = r.randint(-120, 120)
y = r.randint(-25, 30)
t.goto(x, y)
t.pd()
t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(40):
t.pu()
x = r.randint(-90, 90)
y = r.randint(-35, 10)
t.goto(x, y)
t.pd()
t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(40):
t.pu()
x = r.randint(-80, 80)
y = r.randint(60, 90)
t.goto(x, y)
t.pd()
t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(30):
t.pu()
x = r.randint(-50, 50)
y = r.randint(45, 70)
t.goto(x, y)
t.pd()
t.dot(r.randint(2, 5), color[r.randint(0, 7)])
for i in range(50):
t.pu()
x = r.randint(-500, 500)
y = r.randint(120, 300)
t.goto(x, y)
t.pd()
t.dot(r.randint(3, 5), color[r.randint(0, 7)])
t.seth(90)
t.pu()
t.goto(0, 0)
t.fd(210)
t.left(90)
t.fd(170)
t.pd()
t.write("Happy Birthday", font=("Curlz MT", 20))
for i in range(50):
t.pu()
x = r.randint(-500, 500)
y = r.randint(120, 300)
t.goto(x, y)
t.pd()
t.dot(r.randint(2, 5), color[r.randint(0, 7)])
t.seth(20)
t.pu()
t.goto(0, 0)
t.fd(10)
t.left(90)
t.fd(100)
t.pd()
t.write("生日快乐!", font=("华文行楷", 10))
t.done()
效果图:
3.表白程序
from tkinter import * #_all_ = [a,b]
from tkinter import messagebox
from PIL import ImageTk
def closeWindow():
messagebox.showinfo(title="警告",message = "不许关闭,好好回答")
return
#点击喜欢触发的方法
def Love():
#Toplevel独立的顶级窗口,和父级标题一样
love = Toplevel(window)
love.geometry("360x200+540+360")
love.title("好巧,我也是")
label = Label(love,text="巧了,我也喜欢你",font =("微软雅黑",20))
label.pack()
label1 = Label(love,text="认识一下,加个微信呗",font =("微软雅黑",20))
label1.pack()
entry = Entry(love,font = ("微软雅黑",15))
entry.pack()
btn = Button(love,text = "确定",width = 10 , height = 1,command = close_all)
btn.pack()
love.protocol("WM_DELETE_WINDOW",closelove)
def closelove():
return
#关闭所有的窗口 注意,如果父级窗口关了,下面的所有窗口均会关闭
def close_all():
#destory 销毁
window.destroy()
#关闭不喜欢框的X时
def closenolove():
messagebox.showinfo("再考虑一下","再考虑一下呗")
return
disLove()
#点击不喜欢触发的事件
def disLove():
no_love = Toplevel(window)
no_love.geometry("300x90+540+360")
no_love.title("再考虑考虑")
label = Label(no_love,text = "再考虑考虑呗!",font = ("微软雅黑",25))
label.pack()
btn = Button(no_love,text = "好的",width = 10 , height = 1,command = no_love.destroy)
btn.pack()
no_love.protocol("WM_DELETE_WINDOW",closenolove)
# 创建窗口
window =Tk() #类的实例化,创建窗口,window仅仅是个变量
# 窗口标题
window.title("你喜欢我吗?")
# 窗口的大小 运用小写的x来连接
window.geometry("380x400")
#窗口位置(距离屏幕左上角) 运用+来连接
window.geometry("+500+240") # geometry意为几何
#上述可以写成window.geometry("380x200+500+245"),其中+是用来连接的
#用户关闭窗口触发的事件
window.protocol("WM_DELETE_WINDOW",closeWindow)
# 标签控件,一般第一个参数均是父级窗口 ,这里传参为window fg设置颜色
label = Label(window, text = "Hey,小姐姐", font = ("微软雅黑",15), fg="black")
# 定位 grid(网格式) pack(包的方式) place(用的最少的一种,根据位置)
label.grid(row=0,column =0) #默认值为 0 0
label_1 = Label(window,text = "喜欢我吗?",font = ("微软雅黑",25))
label_1.grid(row=1,column = 1,sticky = E) #sticky为对齐方式 N上S下W左E右
# 显示图片
photo = ImageTk.PhotoImage(file='Rose.jpg')
imageLable = Label(window,image = photo)
#column 组件所跨越的列数
imageLable.grid(row=2,columnspan =2) #跨列操作
# ques_image = ImageTk.PhotoImage(file='./Image/cache/{}'.format(image_name.group()))
#按钮控件 点击触发command事件
btn = Button(window,text="喜欢",width = 15,height=1,command = Love)
btn.grid(row = 3,column = 0,sticky = W)
btn1 =Button(window,text="不喜欢",command = disLove)
btn1 .grid(row = 3,column = 1,sticky = E)
#显示窗口 消息循环
window .mainloop()
素材: