我有一个关于L系统的项目。我试图添加一个打印L系统的乌龟;但是,与文本字段不同,canvas不会展开。当我打印更多文本时,我的文本字段正在通过滚动条扩展。然而,乌龟被卡在画布上。我真的被卡住了。
frame2 = tki.Frame(frame, bg='yellow', width=810, height=510)
frame2.pack()
frame2.place(x=500,y=5)
cv = Canvas(frame2, width=2000, height=2000)
cv.place(x=0, y=0)
screen = turtle.TurtleScreen(cv)
t = turtle.RawTurtle(screen)
hbar=Scrollbar(frame2,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=cv.xview)
vbar=Scrollbar(frame2,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=cv.yview)
cv.config(width=800,height=500)
cv.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
cv.pack(side=LEFT,expand=True,fill=BOTH)
答案 0 :(得分:1)
If I understand your question correctly, why doesn't the ScrolledCavas
that the turtle library provides for this purpose work for you:
EDIT: I've started the canvas size at the frame size and added the reset()
method of ScrolledCanvas
to expand the canvas dynamically:
import tkinter as tk
from turtle import TurtleScreen, RawTurtle, ScrolledCanvas
size = 100
canvsize = 300
root = tk.Tk()
root.geometry('600x500')
frame = tk.Frame(root, width=300, height=300)
frame.pack()
frame.place(x=50, y=50)
canvas = ScrolledCanvas(frame, canvwidth=canvsize, canvheight=canvsize)
canvas.pack(side=tk.LEFT)
screen = TurtleScreen(canvas)
screen.bgcolor('yellow')
turtle = RawTurtle(screen, visible=False)
turtle.dot(size, "green")
def expand():
global canvsize, size
if size < 800:
size += 10
if size > canvsize:
canvsize += 100
canvas.reset(canvwidth=canvsize, canvheight=canvsize)
turtle.dot(size, "green")
screen.ontimer(expand, 100)
screen.ontimer(expand, 100)
screen.mainloop()