程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

10 Python with turtle tkinter PIL begin our story

編輯:Python

----------Tanabata is coming,Whether you are a person,I wish you all happiness-------------

效果:

 

完整代碼獲取,and modify free help:

點擊鏈接加入群聊【懶笑翻-Python學習交流】:https://jq.qq.com/?_wv=1027&k=gBTObhx8

完整代碼實現:

"""
* @Author: xiaofang
* @Date: Create in 14:57 2022/8/3
* @Description: the beginning of our story
"""
"""
It is recommended to name the photo of the two as couple.png in this code3With the help of pictures,通過調用showImg方法,You can also change the name where the method is called.
"""
from turtle import *
import tkinter as tk
from PIL import Image, ImageTk
font = ("華文行楷", 17, "normal") # 正文格式
title = ("華文行楷", 30, "normal") # 標題格式
render = {}
color = 'pink'
# 載入圖片
def showImg(face, name, side=tk.TOP, rate=1.0):
global render
load = Image.open('{}.png'.format(name))
size = (int(load.size[0] * rate), int(load.size[1] * rate))
load = load.resize(size)
render[name] = ImageTk.PhotoImage(load)
img = tk.Label(face, image=render[name]).pack(side=side)
# 啟動頁面
class Init():
def __init__(self, master):
self.master = master
self.master.config(width=600, height=400)
self.face = tk.Frame(self.master)
self.face.pack()
text = '\n\n\nAre you ready to start our story baby\n\n'
tk.Label(self.face, text=text, font=title).pack()
tk.Button(self.face, text="開啟我們的故事", font=font, command=self.next, bg=color).pack()
def next(self):
self.face.destroy()
StoryBegin(self.master)
# 正文頁
# 開始頁
class basedesk():
def __init__(self, master):
self.root = master
self.root.config()
self.root.title('開啟我們的故事')
self.root.geometry('600x450')
self.root.resizable(False, False)
Init(self.root)
# 開始頁
# 故事開始
class StoryBegin():
def __init__(self, master):
self.master = master
self.master.config(width=600, height=400)
self.face = tk.Frame(self.master)
self.face.pack()
text = '\n2020年年5月20日\n我們的故事開始\n' \
'Remember the official announcement we sent??\n' \
'Also we see a movie together photos'
tk.Label(self.face, text=text, font=font).pack()
tk.Button(self.face, text="繼續我們的故事", command=self.next, bg=color).pack(side=tk.BOTTOM)
showImg(self.face, 'couple', rate=1.5)
def next(self):
self.face.destroy()
Milktea(self.master)
# 測試頁1
class Milktea():
def __init__(self, master):
self.master = master
self.master.config(width=600, height=400)
self.face = tk.Frame(self.master)
self.face.pack()
text = '\nStart the quiz~\n' \
'Remember my favorite color??'
tk.Label(self.face, text=text, font=font).pack()
self.namestr = tk.StringVar()
tk.Entry(self.face, textvariable=self.namestr, font=font).pack(side=tk.TOP)
tk.Button(self.face, text="確定", command=self.judge, bg=color).pack(side=tk.TOP)
def judge(self):
if self.namestr.get() != '藍色':
tk.messagebox.askokcancel(title='警告', message='回答錯誤!\n提示:Enter two words~')
else:
self.story_continue()
def story_continue(self):
text = '\n回答正確~\n' \
'we all love this color\n' \
'You said the blue\n' \
'Gotta be light blue\n' \
'Of course I know I be fond of you all remember\n'
tk.Label(self.face, text=text, font=font).pack()
tk.Button(self.face, text="繼續我們的故事", command=self.next, bg=color).pack(side=tk.TOP)
def next(self):
self.face.destroy()
Christmas(self.master)
# 測試頁2-1
class Christmas():
def __init__(self, master):
self.master = master
self.master.config(width=600, height=400)
self.face = tk.Frame(self.master)
self.face.pack()
text = '\n根據圖片,Can you tell what day it is?'
tk.Label(self.face, text=text, font=font).pack()
self.namestr = tk.StringVar()
tk.Entry(self.face, textvariable=self.namestr, font=font).pack(side=tk.TOP)
tk.Button(self.face, text="確定", command=self.judge, bg=color).pack(side=tk.TOP)
showImg(self.face, 'couple', rate=1.5)
def judge(self):
if self.namestr.get() != '20200520': # time modification here
tk.messagebox.askokcancel(title='警告', message='回答錯誤!\n提示:input format such as:20200520')
else:
self.next()
def next(self):
self.face.destroy()
Christmas2(self.master)
# 測試頁2-2
class Christmas2():
def __init__(self, master):
self.master = master
self.master.config(width=600, height=400)
self.face = tk.Frame(self.master)
self.face.pack()
text = '\n回答正確~\n是2020年05月20日\n' \
'Of course, is the first day we are together!'
tk.Label(self.face, text=text, font=font).pack()
text = 'We also tick,\n' \
'to be together forever'
tk.Label(self.face, text=text, font=font).pack()
tk.Button(self.face, text="繼續我們的故事", bg=color, command=self.next).pack(side=tk.TOP)
def next(self):
self.face.destroy()
Heart(self.master)
# 結尾頁
class Heart():
def __init__(self, master):
self.master = master
self.master.config()
self.canvas = tk.Canvas(self.master, width=600, height=450)
self.canvas.pack()
self.tu = RawTurtle(self.canvas)
self.tu.hideturtle()
self.tu.home()
self.tu.clear()
self.tu.speed(0)
self.tu.screen.delay(0)
self.person(0, 50)
s = '親愛的寶貝:\n' \
' today is not a special day,\n' \
' No day needs special meaning,\n' \
' because i love you all the time,\n' \
' You don't need to be at a certain node to reveal your heart.'
self.word(s, -100, -150)
def go_to(self, x, y):
self.tu.penup()
self.tu.goto(x, y)
self.tu.down()
def ring(self, a, b, c, d):
for i in range(a):
self.tu.forward(b)
if d == 'right':
self.tu.right(c)
else:
self.tu.left(c)
def heart(self, x, y, size):
self.go_to(x, y)
self.tu.left(150)
self.tu.begin_fill()
self.tu.forward(51 * size)
self.ring(150, size, 0.3, 'right')
self.ring(210, size, 0.786, 'right')
self.tu.left(120)
self.ring(210, size, 0.786, 'right')
self.ring(150, size, 0.3, 'right')
self.tu.forward(51 * size)
self.tu.end_fill()
def person(self, xbias, ybias):
# 頭部
self.tu.color('black')
self.go_to(-228, 72 - ybias)
self.tu.pensize(3)
self.tu.left(150)
self.ring(350, 1, 0.8, 'right')
# 手臂
self.tu.left(150)
self.tu.forward(70)
self.tu.left(90)
self.tu.forward(10)
self.ring(200, 0.1, 0.9, 'right')
self.tu.forward(10)
self.tu.left(90)
self.tu.forward(20)
self.ring(200, 0.1, 0.9, 'right')
self.tu.forward(10)
self.tu.left(90)
self.ring(200, 0.2, 0.9, 'right')
self.tu.left(100)
self.tu.left
self.tu.forward(80)
# 身體
self.go_to(-228, 72 - ybias)
self.tu.left(40)
self.tu.forward(40)
self.ring(120, 0.2, 0.9, 'left')
self.go_to(-219, 52 - ybias)
self.tu.right(95)
self.tu.forward(80)
self.tu.right(85)
self.ring(205, 0.1, 0.9, 'left')
self.tu.forward(40)
self.tu.left(90)
self.tu.forward(10)
self.ring(200, 0.1, 0.9, 'right')
self.tu.forward(10)
self.tu.left(90)
self.tu.forward(40)
self.ring(205, 0.1, 0.9, 'left')
self.tu.right(92)
self.tu.forward(90)
# 左眼
self.go_to(-217, 155 - ybias)
self.tu.fillcolor('black')
self.tu.begin_fill()
self.tu.circle(5)
self.tu.end_fill()
# 右眼
self.go_to(-169, 158 - ybias)
self.tu.fillcolor('black')
self.tu.begin_fill()
self.tu.circle(5)
self.tu.end_fill()
# 微笑
self.go_to(-210, 132 - ybias)
self.tu.right(180)
self.ring(200, 0.2, 0.9, 'left')
# 腮紅
self.tu.color('#ffa0a0')
self.tu.pensize(5)
self.tu.left(170)
self.go_to(-235, 135 - ybias)
self.tu.forward(11)
self.go_to(-225, 135 - ybias)
self.tu.forward(11)
self.go_to(-155, 140 - ybias)
self.tu.forward(11)
self.go_to(-165, 140 - ybias)
self.tu.forward(11)
# 比心
self.tu.setheading(0)
self.heart(-35, 135 - ybias, 0.10)
self.tu.setheading(0)
self.heart(5, 150 - ybias, 0.13)
self.tu.setheading(0)
self.heart(52, 165 - ybias, 0.15)
def word(self, s, x, y):
self.go_to(x, y)
self.tu.write(s, align="left", font=("華文行楷", 20, "normal"))
# 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:732481539
# 尋找有志同道合的小伙伴,互幫互助,群裡還有不錯的視頻學習教程和PDF電子書!
if __name__ == '__main__':
root = tk.Tk()
basedesk(root)
root.mainloop()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved