三款融合Tkinter与Turtle的创意表白程序
1. Tkinter与Turtle的融合基础
Tkinter是Python的标准GUI库,而Turtle则是Python的经典绘图模块。将两者结合使用,可以创造出既有交互界面又有动态绘图效果的程序。这种组合特别适合制作创意表白应用,因为它能同时提供用户输入和视觉呈现的能力。
要在Tkinter窗口中嵌入Turtle画布,我们需要使用TurtleScreen和RawTurtle这两个类。下面是一个最简单的示例代码:
import tkinter as tk from turtle import TurtleScreen, RawTurtle root = tk.Tk() root.title("表白程序") # 创建Turtle画布 canvas = tk.Canvas(root, width=500, height=500) canvas.pack() # 将画布转换为TurtleScreen screen = TurtleScreen(canvas) turtle = RawTurtle(screen) # 绘制一个简单的爱心 turtle.color('red') turtle.begin_fill() turtle.left(50) turtle.forward(100) turtle.circle(40, 180) turtle.right(90) turtle.circle(40, 180) turtle.forward(100) turtle.end_fill() root.mainloop()这段代码创建了一个基本的Tkinter窗口,并在其中嵌入了Turtle绘图区域。我们首先创建了一个Canvas组件,然后将其转换为TurtleScreen,最后在这个屏幕上创建了一个Turtle对象进行绘图。
2. 动态爱心轨迹程序
动态爱心轨迹是一个既美观又有互动性的表白方式。我们可以让爱心按照特定轨迹移动,同时添加一些交互元素。
2.1 基础爱心动画
首先,我们来实现一个基本的爱心动画效果:
import tkinter as tk from turtle import TurtleScreen, RawTurtle import math def create_heart_animation(): root = tk.Tk() root.title("动态爱心") canvas = tk.Canvas(root, width=600, height=600, bg='black') canvas.pack() screen = TurtleScreen(canvas) turtle = RawTurtle(screen) turtle.hideturtle() turtle.speed(0) turtle.color('pink') def draw_heart(t, size): t.begin_fill() t.left(50) t.forward(size) t.circle(size/2.5, 180) t.right(90) t.circle(size/2.5, 180) t.forward(size) t.end_fill() t.left(40) for i in range(36): turtle.penup() turtle.goto(0,0) turtle.setheading(0) turtle.pendown() draw_heart(turtle, 100 + i*2) turtle.right(10) root.mainloop() create_heart_animation()这个程序会在黑色背景上绘制一个旋转放大的爱心图案,形成一种动态效果。我们通过循环不断改变爱心的大小和角度,创造出动画感。
2.2 添加交互元素
为了让表白更有互动性,我们可以添加一些按钮和输入框:
def interactive_heart(): root = tk.Tk() root.title("互动爱心") # 控制面板 control_frame = tk.Frame(root) control_frame.pack(side=tk.TOP, fill=tk.X) tk.Label(control_frame, text="输入表白语:").pack(side=tk.LEFT) message_entry = tk.Entry(control_frame, width=30) message_entry.pack(side=tk.LEFT) def show_message(): message = message_entry.get() turtle.clear() turtle.penup() turtle.goto(0, -150) turtle.color('white') turtle.write(message, align="center", font=("Arial", 16, "bold")) tk.Button(control_frame, text="显示", command=show_message).pack(side=tk.LEFT) # 绘图区域 canvas = tk.Canvas(root, width=600, height=500, bg='black') canvas.pack() screen = TurtleScreen(canvas) turtle = RawTurtle(screen) turtle.hideturtle() turtle.speed(0) def draw_heart(): turtle.clear() turtle.color('red') turtle.begin_fill() turtle.left(50) turtle.forward(100) turtle.circle(40, 180) turtle.right(90) turtle.circle(40, 180) turtle.forward(100) turtle.end_fill() turtle.left(40) draw_heart() root.mainloop() interactive_heart()这个版本添加了一个输入框和按钮,用户可以在输入框中输入表白语,点击按钮后,表白语会显示在爱心下方。这种交互方式让表白更加个性化。
3. 可交互绘图表白板
一个更高级的应用是创建一个可以自由绘制的表白板,让用户能够亲手绘制表白内容。
3.1 基础绘图功能
def drawing_board(): root = tk.Tk() root.title("绘图表白板") canvas = tk.Canvas(root, width=800, height=600, bg='white') canvas.pack() screen = TurtleScreen(canvas) turtle = RawTurtle(screen) turtle.speed(0) # 控制面板 control_frame = tk.Frame(root) control_frame.pack(side=tk.TOP, fill=tk.X) colors = ['red', 'blue', 'green', 'black', 'pink'] for color in colors: btn = tk.Button(control_frame, bg=color, width=3, command=lambda c=color: turtle.color(c)) btn.pack(side=tk.LEFT) tk.Button(control_frame, text="清除", command=turtle.clear).pack(side=tk.LEFT) # 绑定鼠标事件 def drag_handler(x, y): turtle.ondrag(None) # 防止递归 turtle.goto(x, y) turtle.ondrag(drag_handler) turtle.ondrag(drag_handler) root.mainloop() drawing_board()这个程序创建了一个简单的绘图板,用户可以选择不同颜色的画笔,在画布上自由绘制。清除按钮可以一键清空画布。
3.2 添加表白模板
为了让表白更有针对性,我们可以添加一些预设的表白模板:
def template_drawing_board(): root = tk.Tk() root.title("模板绘图表白板") canvas = tk.Canvas(root, width=800, height=600, bg='white') canvas.pack() screen = TurtleScreen(canvas) turtle = RawTurtle(screen) turtle.speed(0) # 控制面板 control_frame = tk.Frame(root) control_frame.pack(side=tk.TOP, fill=tk.X) # 颜色选择 colors = ['red', 'blue', 'green', 'black', 'pink'] for color in colors: btn = tk.Button(control_frame, bg=color, width=3, command=lambda c=color: turtle.color(c)) btn.pack(side=tk.LEFT) tk.Button(control_frame, text="清除", command=turtle.clear).pack(side=tk.LEFT) # 模板选择 template_frame = tk.Frame(root) template_frame.pack(side=tk.TOP, fill=tk.X) def draw_heart(): turtle.clear() turtle.color('red') turtle.begin_fill() turtle.left(50) turtle.forward(100) turtle.circle(40, 180) turtle.right(90) turtle.circle(40, 180) turtle.forward(100) turtle.end_fill() def draw_flower(): turtle.clear() turtle.color('pink') for _ in range(36): turtle.forward(100) turtle.right(45) turtle.forward(30) turtle.right(90) turtle.forward(30) turtle.right(45) turtle.forward(100) turtle.right(110) tk.Button(template_frame, text="爱心", command=draw_heart).pack(side=tk.LEFT) tk.Button(template_frame, text="花朵", command=draw_flower).pack(side=tk.LEFT) # 绑定鼠标事件 def drag_handler(x, y): turtle.ondrag(None) turtle.goto(x, y) turtle.ondrag(drag_handler) turtle.ondrag(drag_handler) root.mainloop() template_drawing_board()这个版本增加了"爱心"和"花朵"两个模板按钮,点击后会自动绘制相应的图案,用户可以在这些基础上进行二次创作。
4. 高级表白程序:记忆游戏式表白
最后,我们来创建一个更有创意的记忆游戏式表白程序。用户需要通过完成一个小游戏来解锁表白信息。
def memory_game(): root = tk.Tk() root.title("记忆游戏表白") canvas = tk.Canvas(root, width=600, height=600, bg='black') canvas.pack() screen = TurtleScreen(canvas) turtle = RawTurtle(screen) turtle.hideturtle() turtle.speed(0) # 游戏状态 sequence = [] user_sequence = [] level = 1 # 颜色定义 colors = ['red', 'blue', 'green', 'yellow'] color_positions = [(-100, 100), (100, 100), (-100, -100), (100, -100)] # 绘制游戏板 def draw_board(): turtle.clear() turtle.penup() for i, (x, y) in enumerate(color_positions): turtle.goto(x, y) turtle.color('gray', colors[i]) turtle.begin_fill() turtle.circle(50) turtle.end_fill() # 闪烁颜色 def flash_color(index): x, y = color_positions[index] turtle.penup() turtle.goto(x, y) turtle.color('white', colors[index]) turtle.begin_fill() turtle.circle(50) turtle.end_fill() turtle.update() root.after(500, draw_board) # 游戏逻辑 def next_level(): nonlocal sequence, user_sequence, level sequence.append(tk.randint(0, 3)) user_sequence = [] turtle.penup() turtle.goto(0, 0) turtle.color('white') turtle.write(f"等级 {level}", align="center", font=("Arial", 24, "bold")) turtle.update() root.after(1000, play_sequence) def play_sequence(index=0): if index < len(sequence): flash_color(sequence[index]) root.after(1000, lambda: play_sequence(index + 1)) else: turtle.clear() draw_board() def color_clicked(index): user_sequence.append(index) flash_color(index) if user_sequence[-1] != sequence[len(user_sequence)-1]: game_over() elif len(user_sequence) == len(sequence): level_complete() def level_complete(): nonlocal level level += 1 if level <= 5: # 总共5关 turtle.penup() turtle.goto(0, 0) turtle.color('white') turtle.write("成功!", align="center", font=("Arial", 24, "bold")) turtle.update() root.after(1000, next_level) else: game_win() def game_over(): turtle.clear() turtle.penup() turtle.goto(0, 0) turtle.color('white') turtle.write("游戏结束", align="center", font=("Arial", 24, "bold")) turtle.update() def game_win(): turtle.clear() turtle.penup() turtle.goto(0, 0) turtle.color('pink') turtle.write("我喜欢你!", align="center", font=("Arial", 36, "bold")) # 绘制爱心 turtle.goto(0, -100) turtle.color('red') turtle.begin_fill() turtle.left(50) turtle.forward(100) turtle.circle(40, 180) turtle.right(90) turtle.circle(40, 180) turtle.forward(100) turtle.end_fill() turtle.update() # 设置点击区域 for i, (x, y) in enumerate(color_positions): canvas.create_rectangle(x-60, y-60, x+60, y+60, outline='', fill='', tags=f"color_{i}") canvas.tag_bind(f"color_{i}", "<Button-1>", lambda e, idx=i: color_clicked(idx)) # 开始游戏 next_level() root.mainloop() memory_game()这个程序创建了一个记忆游戏,用户需要记住并重复颜色序列。通过所有关卡后,会显示"我喜欢你!"的表白信息和爱心图案。这种互动方式让表白过程更有趣味性和仪式感。
