使背景图片适应画布大小import time from tkinter import * import turtle # 导入turtle模块 import time class ShowWelcome(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.createPage() def createPage(self): canva = Canvas(self.root, width=500, height=500) # 设定画布 canva.pack() theScreen = turtle.TurtleScreen(canva) # 设定turtle屏幕 path = turtle.RawTurtle(theScreen) # 设定画笔 # 添加背景 bg_image = PhotoImage(file="background.png") canva.create_image(0, 0, anchor=NW, image=bg_image) path.color('#87CEFA') # 设定画笔颜色 path.left(180) path.up() # 抬笔 path.forward(80) path.down() # 落笔 path.write('欢迎进入本系统', font=('宋体', 18)) path.up() path.forward(100) path.down() path.left(90) path.forward(100) path.left(90) path.forward(350) time.sleep(3) canva.pack_forget()
时间: 2024-04-08 17:33:27 浏览: 92
要使背景图片适应画布大小,可以使用`canva.create_image()`方法时设置`width`和`height`参数为画布的宽度和高度。以下是修改后的代码:
```python
import time
from tkinter import *
import turtle # 导入turtle模块
import time
class ShowWelcome(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master # 定义内部变量root
self.createPage()
def createPage(self):
canva = Canvas(self.root, width=500, height=500) # 设定画布
canva.pack()
theScreen = turtle.TurtleScreen(canva) # 设定turtle屏幕
path = turtle.RawTurtle(theScreen) # 设定画笔
# 添加背景
bg_image = PhotoImage(file="background.png")
canva.create_image(0, 0, anchor=NW, image=bg_image, width=500, height=500)
path.color('#87CEFA') # 设定画笔颜色
path.left(180)
path.up() # 抬笔
path.forward(80)
path.down() # 落笔
path.write('欢迎进入本系统', font=('宋体', 18))
path.up()
path.forward(100)
path.down()
path.left(90)
path.forward(100)
path.left(90)
path.forward(350)
time.sleep(3)
canva.pack_forget()
```
在`canva.create_image()`方法中添加了`width=500`和`height=500`参数来适应画布的大小。请确保将`width`和`height`参数的值替换为你的画布实际的宽度和高度。
阅读全文