前言
起因是由于教师节学院让用代码给老师送上祝福,当时在学用户界面,刚好就找了几个土土的动画集成在一起了。
主要功能
打开一个按键的窗口,当其运行完后,要关闭该窗口,一共有18种不同的祝福展示,如下图部分所示,
代码示例
import tkinter
from tkinter import *
from turtle import *
from tkinter import Toplevel, PhotoImage
from PIL import Image, ImageTk
from time import sleep
import turtle
import turtle as t
from random import random,randint
import colorsys
a, b = -1100, 280
k=0
class Application:
def __init__(self, root):
super().__init__()
self.root=root
self.font = ("Arial", 14, "bold")
self.createWidget()
#定义按键
def createWidget(self):
self.btn01 = Button(self.root, text = '教师节快乐!', bg = 'red', fg="black",font = self.font, command = self.on_button_press_01)
self.btn02 = Button(self.root, text = '教师节快乐!', bg = 'red', fg="black", font = self.font, command = self.on_button_press_02)
self.btn03 = Button(self.root, text = '教师节快乐!', bg = 'red', fg="black", font = self.font, command = self.on_button_press_03)
self.btn04 = Button(self.root, text = '教师节快乐!', bg = 'red', fg="black", font = self.font, command = self.on_button_press_04)
self.btn05 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_05)
self.btn06 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_06)
self.btn07 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_07)
self.btn08 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_08)
self.btn09 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_09)
self.btn10 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_10)
self.btn11 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_11)
self.btn12 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_12)
self.btn13 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_13)
self.btn14 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_14)
self.btn15 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_15)
self.btn16 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_16)
self.btn17 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_17)
self.btn18 = Button(self.root, text='教师节快乐!', bg = 'red', fg="black", font=self.font, command=self.on_button_press_18)
self.btn19 = Button(self.root, text='教师节快乐!', bg='red', fg="black", font=self.font,
command=self.on_button_press_19)
self.btn01.place(x=450, y=170)
self.btn02.place(x=300, y=100)
self.btn03.place(x=600, y=100)
self.btn04.place(x=200, y=157)
self.btn05.place(x=700, y=157)
self.btn06.place(x=150, y=207)
self.btn07.place(x=750, y=207)
self.btn08.place(x=150, y=264)
self.btn09.place(x=750, y=264)
self.btn10.place(x=150, y=321)
self.btn11.place(x=750, y=321)
self.btn12.place(x=200, y=378)
self.btn13.place(x=700, y=378)
self.btn14.place(x=250, y=435)
self.btn15.place(x=650, y=435)
self.btn16.place(x=300, y=492)
self.btn17.place(x=600, y=492)
self.btn18.place(x=450, y=562)
self.btn19.place(x=450, y=346)
#向日葵图片
def on_button_press_01(self):
top = Toplevel()
top.title("感恩教师")
img = Image.open('image01.jpg')
img = img.resize((500, 500), Image.LANCZOS) # 使用LANCZOS过滤器进行高质量缩放
photo = ImageTk.PhotoImage(img)
# 在窗口中放置图片
label = tkinter.Label(top, image=photo)
label.image = photo # 保持对photo的引用,防止被垃圾回收
label.pack()
#感恩教师图片
def on_button_press_02(self):
top = Toplevel()
top.title("感恩教师")
img = Image.open('image02.jpg')
img = img.resize((500, 500), Image.LANCZOS) # 使用LANCZOS过滤器进行高质量缩放
photo = ImageTk.PhotoImage(img)
# 在窗口中放置图片
label = tkinter.Label(top, image=photo)
label.image = photo # 保持对photo的引用,防止被垃圾回收
label.pack()
#祝福教师图片
def on_button_press_03(self):
top = Toplevel()
top.title("感恩教师")
img = Image.open('image08.jpg')
img = img.resize((500, 500), Image.LANCZOS) # 使用LANCZOS过滤器进行高质量缩放
photo = ImageTk.PhotoImage(img)
# 在窗口中放置图片
label = tkinter.Label(top, image=photo)
label.image = photo # 保持对photo的引用,防止被垃圾回收
label.pack()
# 感恩教师图片
def on_button_press_04(self):
top = Toplevel()
top.title("感恩教师")
img = Image.open('image04.jpg')
img = img.resize((500, 500), Image.LANCZOS) # 使用LANCZOS过滤器进行高质量缩放
photo = ImageTk.PhotoImage(img)
# 在窗口中放置图片
label = tkinter.Label(top, image=photo)
label.image = photo # 保持对photo的引用,防止被垃圾回收
label.pack()
# 感恩教师图片
def on_button_press_05(self):
top = Toplevel()
top.title("感恩教师")
img = Image.open('image09.png')
img = img.resize((500, 500), Image.LANCZOS) # 使用LANCZOS过滤器进行高质量缩放
photo = ImageTk.PhotoImage(img)
# 在窗口中放置图片
label = tkinter.Label(top, image=photo)
label.image = photo # 保持对photo的引用,防止被垃圾回收
label.pack()
#一朵动态玫瑰花
def on_button_press_06(self):
reset()
reset()
title("玫瑰花")
setup(1000, 800, 0, 0)
speed(0.000001)
penup()
seth(90)
fd(340)
seth(0)
pendown()
speed(5)
begin_fill()
fillcolor('red')
circle(50, 30)
for i in range(10):
fd(1)
left(10)
circle(40, 40)
for i in range(6):
fd(1)
left(3)
circle(80, 40)
for i in range(20):
fd(0.5)
left(5)
circle(80, 45)
for i in range(10):
fd(2)
left(1)
circle(80, 25)
for i in range(20):
fd(1)
left(4)
circle(50, 50)
sleep(0.1)
circle(120, 55)
speed(0)
seth(-90)
fd(70)
right(150)
fd(20)
left(140)
circle(140, 90)
left(30)
circle(160, 100)
left(130)
fd(25)
penup()
right(150)
circle(40, 80)
pendown()
left(115)
fd(60)
penup()
left(180)
fd(60)
pendown()
end_fill()
right(120)
circle(-50, 50)
circle(-20, 90)
speed(1)
fd(75)
speed(0)
circle(90, 110)
penup()
left(162)
fd(185)
left(170)
pendown()
circle(200, 10)
circle(100, 40)
circle(-52, 115)
left(20)
circle(100, 20)
circle(300, 20)
speed(1)
fd(250)
penup()
speed(0)
left(180)
fd(250)
circle(-300, 7)
right(80)
circle(200, 5)
pendown()
left(60)
begin_fill()
fillcolor('green')
circle(-80, 100)
right(90)
fd(10)
left(20)
circle(-63, 127)
end_fill()
penup()
left(50)
fd(20)
left(180)
pendown()
circle(200, 25)
penup()
right(150)
fd(180)
right(40)
pendown()
begin_fill()
fillcolor('green')
circle(-100, 80)
right(150)
fd(10)
left(60)
circle(-80, 98)
end_fill()
penup()
left(60)
fd(13)
left(180)
pendown()
speed(1)
circle(-200, 23)
t.up()
t.goto(50, -300)
t.down()
t.write("To: 智慧与温柔并存的老师们", move=True, align="left", font=("楷体", 20, "normal"))
t.done()
exitonclick()
#樱花树,花繁叶茂
def drawTree(self,length, level):
toplevel = 8 # 一共递归6层
angle = 30
rangle = 15
turtle.title("花繁叶茂")
turtle.left(angle) # 绘制左枝
turtle.color("black")
turtle.forward(length)
if level == toplevel: # 叶子
turtle.color("pink")
turtle.circle(2, 360)
if level < toplevel: # 在左枝退回去之前递归
self.drawTree(length - 10, level + 1)
turtle.back(length)
turtle.right(angle + rangle) # 绘制右枝
turtle.color("black")
turtle.forward(length)
if level == toplevel: # 叶子
turtle.color("pink")
turtle.circle(2, 360)
if level < toplevel: # 在右枝退回去之前递归
self.drawTree(length - 10, level + 1)
turtle.color("black")
turtle.back(length)
turtle.left(rangle)
def on_button_press_07(self):
reset()
reset()
turtle.left(90)
turtle.penup()
turtle.back(300)
turtle.pendown()
turtle.forward(100)
turtle.speed('fastest')
self.drawTree(80, 1)
turtle.up()
turtle.goto(20, -200)
turtle.down()
turtle.write("老师用他们的知识和爱心培育学生,"+'\n'+
"像树一样为学生提供滋养和支持;"+'\n'+
"同时,老师也像树一样具有奉献、"+'\n'+
"坚韧、多样性和包容性等品质。", move=True, align="left", font=("楷体", 20, "normal"))
turtle.done()
#赞美诗词动态展示
def w(self,str_, b):
global a
bgcolor(randint(0, 255), randint(0, 255), randint(0, 255)) # 随机生成RGB值, 每次调用函数改变背景颜色
for i in range(len(str_)):
up()
goto(a + 70 * i, b)
down()
size = randint(18, 60) # 随机字体大小
color(randint(0, 255), randint(0, 255), randint(0, 255)) # 随机字体颜色
write(str_[i], align="center", font=("楷体", size))
def on_button_press_08(self):
reset()
reset()
title("诗词动态展示")
str_ = """
落红不是无情物,化作春泥更护花。
春蚕到死丝方尽,蜡炬成灰泪始干。
新竹高于旧竹枝,全凭老干为扶持。
令公桃李满天下,何用堂前更种花。
摇落深知宋玉悲,风流儒雅亦吾师。
道吾好者是吾贼,道吾恶者是吾师。
采得百花成蜜后,为谁辛苦为谁甜。
绿野堂开占物华,路人指道令公家。
""".split("。")
setup(1400, 700) # 设置窗口大小
colormode(255) # 使用的颜色模式, 整数还是小数
up()
goto(a, b)
bgcolor("black")
for k in range(4):
for i in range(14):
self.w(str_[i + 14 * k], b - 100 * i)
reset() # 清屏
for i in range(14):
self.w(str_[i + 14 * 4], b - 100 * i)
up()
down()
ht()
bye()
# 赞美诗词动态展示
def on_button_press_09(self):
reset()
reset()
title("诗词动态展示")
str_ = """
下年再有新生者,十丈龙孙绕凤池。
遥想吾师行道处,天香桂子落纷纷。
忽遇文殊开慧眼,他年应记老师心。
师住稽亭高处寺,斜廊曲阁倚云开。
上帝照临忠义胆,老师付授文章脉。
王师未报收东郡,城阙秋生画角哀。
元老侑坐须眉古,虎臣立侍冠剑长。
欲为圣明除弊事,肯将衰朽惜残年。
""".split("。")
setup(1400, 700) # 设置窗口大小
colormode(255) # 使用的颜色模式, 整数还是小数
up()
goto(a, b)
bgcolor("black")
for k in range(4):
for i in range(14):
self.w(str_[i + 14 * k], b - 100 * i)
reset() # 清屏
for i in range(14):
self.w(str_[i + 14 * 4], b - 100 * i)
up()
down()
ht()
bye()
#太阳
def on_button_press_10(self):
reset()
reset()
names = ["Alice", "Bob", "Charlie", "David", "Eva"]
# 初始化turtle
screen = turtle.Screen()
screen.title("太阳")
screen.bgcolor("blue") # 设置背景颜色为黑色
sun = turtle.Turtle()
sun.speed(1) # 设置绘图速度
sun.color("red") # 设置太阳的颜色
sun.begin_fill()
sun.circle(150) # 绘制一个半径为100的圆,代表太阳
sun.end_fill()
# 使用名字来影响光线的颜色
for name in names:
light = turtle.Turtle()
light.speed(1)
light.penup()
hue = (hash(name) % 360) / 360.0
r, g, b = colorsys.hsv_to_rgb(hue, 1, 1)
turtle.up()
turtle.goto(20, -200)
turtle.down()
turtle.write("老师如同太阳," + '\n' +
"用无尽的光芒照亮学生的心灵," + '\n' +
"引领他们走向知识的光明之路。" , move=True, align="left", font=("楷体", 20, "normal"))
turtle.done()
screen.bye()
#月亮
def on_button_press_11(self):
reset()
reset()
names = ["Alice", "Bob", "Charlie", "David", "Eva"]
# 初始化turtle
screen = turtle.Screen()
screen.title("月亮")
screen.bgcolor("black") # 设置背景颜色为黑色
sun = turtle.Turtle()
sun.speed(1) # 设置绘图速度
sun.color("yellow") # 设置太阳的颜色
sun.begin_fill()
sun.circle(150) # 绘制一个半径为100的圆,代表太阳
sun.end_fill()
# 使用名字来影响光线的颜色
for name in names:
light = turtle.Turtle()
light.speed(1)
light.penup()
hue = (hash(name) % 360) / 360.0
r, g, b = colorsys.hsv_to_rgb(hue, 1, 1)
turtle.up()
turtle.goto(20, -200)
turtle.down()
turtle.write("老师是夜空中最亮的月亮," + '\n' +
"照亮学生前行的道路," + '\n' +
"给予温暖与指引。", move=True, align="left", font=("楷体", 20, "normal"))
turtle.done()
screen.bye()
#叮当猫
def on_button_press_12(self):
reset()
reset()
# t.speed(5)
t.title("叮当猫")
t.pensize(8)
t.hideturtle()
t.screensize(500, 500, bg='white')
# 猫脸
t.fillcolor('#00A1E8')
t.begin_fill()
t.circle(120)
t.end_fill()
t.pensize(3)
t.fillcolor('white')
t.begin_fill()
t.circle(100)
t.end_fill()
t.pu()
t.home()
t.goto(0, 134)
t.pd()
t.pensize(4)
t.fillcolor("#EA0014")
t.begin_fill()
t.circle(18)
t.end_fill()
t.pu()
t.goto(7, 155)
t.pensize(2)
t.color('white', 'white')
t.pd()
t.begin_fill()
t.circle(4)
t.end_fill()
t.pu()
t.goto(-30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
t.pu()
t.goto(30, 160)
t.pensize(4)
t.pd()
t.color('black', 'white')
t.begin_fill()
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
t.pu()
t.goto(-38, 190)
t.pensize(8)
t.pd()
t.right(-30)
t.forward(15)
t.right(70)
t.forward(15)
t.pu()
t.goto(15, 185)
t.pensize(4)
t.pd()
t.color('black', 'black')
t.begin_fill()
t.circle(13)
t.end_fill()
t.pu()
t.goto(13, 190)
t.pensize(2)
t.pd()
t.color('white', 'white')
t.begin_fill()
t.circle(5)
t.end_fill()
t.pu()
t.home()
t.goto(0, 134)
t.pensize(4)
t.pencolor('black')
t.pd()
t.right(90)
t.forward(40)
t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(10)
t.forward(80)
t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(6)
t.forward(80)
t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(0)
t.forward(80)
# 左边的胡子
t.pu()
t.home()
t.goto(0, 124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(170)
t.forward(80)
t.pu()
t.home()
t.goto(0, 114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(174)
t.forward(80)
t.pu()
t.home()
t.goto(0, 104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(180)
t.forward(80)
t.pu()
t.goto(-70, 70)
t.pd()
t.color('black', 'red')
t.pensize(6)
t.seth(-60)
t.begin_fill()
t.circle(80, 40)
t.circle(80, 80)
t.end_fill()
t.pu()
t.home()
t.goto(-80, 70)
t.pd()
t.forward(160)
t.pu()
t.home()
t.goto(-50, 50)
t.pd()
t.pensize(1)
t.fillcolor("#eb6e1a")
t.seth(40)
t.begin_fill()
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(40)
t.circle(-40, 40)
t.circle(-40, 40)
t.seth(220)
t.circle(-80, 40)
t.circle(-80, 40)
t.end_fill()
# 领带
t.pu()
t.goto(-70, 12)
t.pensize(14)
t.pencolor('red')
t.pd()
t.seth(-20)
t.circle(200, 30)
t.circle(200, 10)
# 铃铛
t.pu()
t.goto(0, -46)
t.pd()
t.pensize(3)
t.color("black", '#f8d102')
t.begin_fill()
t.circle(25)
t.end_fill()
t.pu()
t.goto(-5, -40)
t.pd()
t.pensize(2)
t.color("black", '#79675d')
t.begin_fill()
t.circle(5)
t.end_fill()
t.pensize(3)
t.right(115)
t.forward(7)
t.up()
t.goto(20, -200)
t.down()
t.write("老师就像叮当猫一样,"+ '\n' +
"拥有无尽的智慧与创意," + '\n' +
"总能从神奇的‘口袋’中掏出解决难题的方法,"+ '\n' +
"让学生们的世界更加丰富多彩。" , move=True, align="left", font=("楷体", 20, "normal"))
t.done()
t.mainloop()
#玫瑰花
def on_button_press_13(self):
reset()
reset()
title("玫瑰花")
setup(1000, 800, 0, 0)
speed(0.000001)
penup()
seth(90)
fd(340)
seth(0)
pendown()
speed(5)
begin_fill()
fillcolor('red')
circle(50, 30)
for i in range(10):
fd(1)
left(10)
circle(40, 40)
for i in range(6):
fd(1)
left(3)
circle(80, 40)
for i in range(20):
fd(0.5)
left(5)
circle(80, 45)
for i in range(10):
fd(2)
left(1)
circle(80, 25)
for i in range(20):
fd(1)
left(4)
circle(50, 50)
sleep(0.1)
circle(120, 55)
speed(0)
seth(-90)
fd(70)
right(150)
fd(20)
left(140)
circle(140, 90)
left(30)
circle(160, 100)
left(130)
fd(25)
penup()
right(150)
circle(40, 80)
pendown()
left(115)
fd(60)
penup()
left(180)
fd(60)
pendown()
end_fill()
right(120)
circle(-50, 50)
circle(-20, 90)
speed(1)
fd(75)
speed(0)
circle(90, 110)
penup()
left(162)
fd(185)
left(170)
pendown()
circle(200, 10)
circle(100, 40)
circle(-52, 115)
left(20)
circle(100, 20)
circle(300, 20)
speed(1)
fd(250)
penup()
speed(0)
left(180)
fd(250)
circle(-300, 7)
right(80)
circle(200, 5)
pendown()
left(60)
begin_fill()
fillcolor('green')
circle(-80, 100)
right(90)
fd(10)
left(20)
circle(-63, 127)
end_fill()
penup()
left(50)
fd(20)
left(180)
pendown()
circle(200, 25)
penup()
right(150)
fd(180)
right(40)
pendown()
begin_fill()
fillcolor('green')
circle(-100, 80)
right(150)
fd(10)
left(60)
circle(-80, 98)
end_fill()
penup()
left(60)
fd(13)
left(180)
pendown()
speed(1)
circle(-200, 23)
t.up()
t.goto(50, -200)
t.down()
t.write("To: 智慧与温柔并存的老师们", move=True, align="left", font=("楷体", 20, "normal"))
t.done()
exitonclick()
#小狮子
def hair(self): # 画头发
t.penup()
t.goto(-50, 150)
t.pendown()
t.fillcolor('#a2774d')
t.begin_fill()
for j in range(10): # 重复执行10次
t.setheading(60 - (j * 36)) # 每次调整初始角度
t.circle(-50, 120) # 画120度的弧
t.end_fill()
def face(self): # 画脸
t.penup()
t.goto(0, 100)
t.pendown()
t.fillcolor('#f2ae20')
t.begin_fill()
t.setheading(180)
t.circle(85)
t.end_fill()
# 下巴
t.circle(85, 120)
t.fillcolor('white')
t.begin_fill()
t.circle(85, 120)
t.setheading(135)
t.circle(100, 95)
t.end_fill()
def ears(self,dir): # 画眼睛,dir用来设置方向,左右眼对称
t.penup()
t.goto((0 - dir) * 30, 90)
t.setheading(90)
t.pendown()
t.fillcolor('#f2ae20')
t.begin_fill()
t.circle(dir * 30)
t.end_fill()
t.penup()
t.goto((0 - dir) * 40, 85)
t.setheading(90)
t.pendown()
t.fillcolor('white')
t.begin_fill()
t.circle(dir * 17)
t.end_fill()
def nose(self): # 画鼻子
t.penup()
t.goto(20, 0)
t.setheading(90)
t.pendown()
t.fillcolor('#a2774d')
t.begin_fill()
t.circle(20)
t.end_fill()
def eye(self,dir): # 画耳朵,dir用来设置方向,左右耳对称
t.penup()
t.goto((0 - dir) * 30, 20)
t.setheading(0)
t.pendown()
t.fillcolor('black')
t.begin_fill()
t.circle(10)
t.end_fill()
def mouth(self): # 画嘴巴
t.penup()
t.goto(0, 0)
t.setheading(-90)
t.pendown()
t.forward(50)
t.setheading(0)
t.circle(80, 30)
t.penup()
t.goto(0, -50)
t.setheading(180)
t.pendown()
t.circle(-80, 30)
def on_button_press_14(self):
reset()
reset()
t.title("小狮子")
self.hair()
self.ears(1)
self.ears(-1)
self.face()
self.eye(1)
self.eye(-1)
self.mouth()
self.nose()
t.penup() # 提起画笔,避免移动时绘制线条
t.goto(20, -200) # 移动到距离画布左上角100像素,向下50像素的位置
t.write("在某些文化中,"+ '\n' +
"狮子被视为勇敢和力量的象征,"+ '\n' +
"这与老师培养学生勇敢面对挑战、"+ '\n' +
"追求卓越的目标相契合。", font=("Arial", 18, "normal"))
t.pendown()
t.hideturtle()
t.done()
#星空
def on_button_press_15(self):
reset()
reset()
global k
screen = Screen()
width, height = 800, 600
screen.setup(width, height)
screen.title("简约版星空")
screen.bgcolor("black")
screen.mode("logo")
screen.delay(0) # 这里要设为0,否则很卡
t = Turtle(visible=False, shape='circle')
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.setheading(-90)
t.goto(width / 2, randint(-height / 2, height / 2))
stars = []
for i in range(200):
star = t.clone()
s = random() / 3
star.shapesize(s, s)
star.speed(int(s * 10))
star.setx(width / 2 + randint(1, width))
star.sety(randint(-height / 2, height / 2))
star.showturtle()
stars.append(star)
while k<1000:
k+=1
for star in stars:
star.setx(star.xcor() - 3 * star.speed())
if star.xcor() < -width / 2:
star.hideturtle()
star.setx(width / 2 + randint(1, width))
star.sety(randint(-height / 2, height / 2))
star.showturtle()
t.penup() # 提起画笔,避免移动时绘制线条
t.goto(20, -200) # 移动到距离画布左上角100像素,向下50像素的位置
t.write("老师们不仅传授知识," + '\n' +
"还鼓励学生保持好奇心和求知欲," + '\n' +
"去探索知识的深度和广度," + '\n' +
"就像天文学家探索星空的奥秘一样。",
font=("Arial", 18, "normal"))
t.pendown()
t.hideturtle()
screen.bye()
# 赞美诗词
def on_button_press_16(self):
self.C = tkinter.Toplevel()
self.C.title('祝福语')
self.font_17 = ("宋体", 22, "bold")
self.a = StringVar()
self.lab_a = Label(self.C, text='纵使人不见、声不闻,思念相伴;' + '\n' +
'纵使风不动、水不流,短信常依;' + '\n' +
'诚挚的祝福,不常有,却意义非凡;' + '\n' +
'快乐的心声,轻轻地呼唤:老师,节日快乐!', bg='white', fg="blue", font=self.font_17)
self.entry_a = Entry(self.C, textvariable=self.a, width=30)
self.lab_a.grid(row=1, column=0, sticky=E, pady=15)
self.C.mainloop()
#赞美诗词
def on_button_press_17(self):
self.T = tkinter.Toplevel()
self.T.title('祝福语')
self.font_17 = ("宋体", 22, "bold")
self.a = StringVar()
self.lab_a = Label(self.T,text='你紧握知识的舵盘,载我们驶入智慧的海洋。'+'\n'+
'你倾吐鼓励的话语,伴我们走进梦想的国度。'+'\n'+
'你开启思考的大门,带我们抵达成功的彼岸。'+'\n'+
'老师,节日快乐!',bg='white',fg="red", font=self.font_17)
self.entry_a = Entry(self.T, textvariable=self.a, width=30)
self.lab_a.grid(row=1, column=0, sticky=E, pady=15)
self.T.mainloop()
# 动态樱花树
def tree(self,branchLen, t):
sleep(0.0001)
if branchLen > 3:
if 8 <= branchLen <= 12:
if randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 3)
elif branchLen < 8:
if randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branchLen / 10) # 6
t.forward(branchLen)
a = 1.5 * random()
t.right(20 * a)
b = 1.5 * random()
self.tree(branchLen - 10 * b, t)
t.left(40 * a)
self.tree(branchLen - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branchLen)
t.down()
def petal(self,m, t):
for i in range(m):
a = 200 - 400 * random()
b = 10 - 20 * random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral') # 淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
def on_button_press_18(self):
reset()
reset()
t18=turtle.Turtle()
w18 = turtle.Screen()
w18.title("动态樱花树")
t18.hideturtle() # 隐藏画笔
t18.getscreen().tracer(5, 0)
w18.screensize(bg='wheat') # wheat小麦
t18.left(90)
t18.up()
t18.backward(150)
t18.down()
t18.color('sienna')
# 画樱花的躯干
self.tree(60, t18)
# 掉落的花瓣
self.petal(200, t18)
w18.exitonclick()
w18.bye()
#发射爱心
def on_button_press_19(self):
reset()
reset()
setup(1000, 700) # 设置窗口大小
t.title("发射爱心")
t.pensize(2)
t.color('red', 'pink')
t.up()
t.goto(-120, 100)
t.down()
t.speed("fastest")
t.circle(100)
t.right(90)
t.forward(180)
t.right(30)
t.forward(100)
t.left(120)
t.up()
t.goto(-120, 100 - 180)
t.down()
t.forward(100)
t.right(120)
t.forward(100)
t.left(120)
t.up()
t.goto(-120, 100 - 60)
t.down()
t.forward(100)
t.left(60)
t.forward(100)
t.up()
t.goto(-120, 100 - 90)
t.down()
t.right(60)
t.forward(100)
t.right(60)
t.forward(100)
t.left(60)
t.up()
t.goto(-120 - 50, 100 + 130)
t.down()
t.right(90)
t.forward(50)
t.up()
t.goto(-120 + 40, 100 + 130)
t.down()
t.forward(50)
t.left(90)
t.up()
t.goto(250, -80)
t.down()
t.left(150)
t.begin_fill()
t.speed("fastest")
t.forward(51 * 1)
t.speed("fastest")
for i in range(150):
t.forward(1)
t.right(0.3)
t.speed("fastest")
for i in range(210):
t.forward(1)
t.right(0.786)
t.left(120)
t.speed("fastest")
for i in range(210):
t.forward(1)
t.right(0.786)
t.speed("fastest")
for i in range(150):
t.forward(1)
t.right(0.3)
t.speed("fastest")
t.forward(51 * 1)
t.end_fill()
t.up()
t.goto(80, -300)
t.down()
t.write("To: 智慧与温柔并存的老师们", move=True, align="left", font=("楷体", 20, "normal"))
t.done()
root = tkinter.Tk()
root.title("祝老师教师节快乐")
root.geometry("1100x700")
app=Application(root)
root.mainloop()
问题
但是目前遇到如下报错还没能修正,但不影响正常使用,按键没反应二次点击即可。如果有能够解决的大佬,欢迎留言哦!