Python 2.7 更改Python turtle画布上的画布滚动区域
我可以更改Python turtle画布上的滚动区域吗?我想让图形随着它移动,而不仅仅是坐标移动。我想要的外观是侧卷轴,屏幕的显示区域移动到屏幕中央 我尝试过使用Python 2.7 更改Python turtle画布上的画布滚动区域,python-2.7,tkinter,turtle-graphics,Python 2.7,Tkinter,Turtle Graphics,我可以更改Python turtle画布上的滚动区域吗?我想让图形随着它移动,而不仅仅是坐标移动。我想要的外观是侧卷轴,屏幕的显示区域移动到屏幕中央 我尝试过使用turtle.setWorldCoordinations(llx,lly,urx,ury),但从文档中可以看出,“这将执行一个屏幕.reset()”。我也看过了,但这涉及到滚动条,不能轻易地将海龟居中,并且画布空间有限。我要找的是: 移动显示区域以使海龟居中 同时移动图形 具有无限滚动区域 不显示滚动条 可以使用函数快速调用 我最好的
turtle.setWorldCoordinations(llx,lly,urx,ury)
,但从文档中可以看出,“这将执行一个屏幕.reset()
”。我也看过了,但这涉及到滚动条,不能轻易地将海龟居中,并且画布空间有限。我要找的是:
- 移动显示区域以使海龟居中
- 同时移动图形
- 具有无限滚动区域
- 不显示滚动条
- 可以使用函数快速调用 我最好的猜测是能够有一个无限的滚动画布,然后隐藏滚动条,并根据海龟的位置设置它们
canvas.xview
和canvas.yview
函数,但是一旦我定义了screen=turtle.TurtleScreen(canvas)
,它们似乎就不起作用了,而TurtleScreen没有xview
或yview
函数。我好像没办法让这件事成功
然后我找到了
turtle.scrolled canvas()
。这似乎很理想,只是它没有从程序中手动设置滚动的方法。我可以在turtle.ScrolledCanvas()上手动设置滚动吗?
?可以使用canvas.place()方法在不重置的情况下更改画布的位置。它会移动海龟和图纸,因此每次移动后都需要重新定位海龟
下一个代码使用左箭头和右箭头移动画布,并用空格绘制一个圆圈,同时将海龟保持在中心。不需要滚动画布,只需要一个非常大的标准画布:
import turtle
import Tkinter as tk
def keypress(event):
global xx, canvas, t, speed
ev = event.keysym
if ev == 'Left':
xx += speed
else:
xx -= speed
canvas.place(x=xx)
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
return None
def drawCircle(_):
global t
t.pendown()
t.fillcolor(0, 0, 1.0)
t.fill(True)
t.circle(100)
t.fill(False)
t.fillcolor(0, 1, 0)
t.penup()
# Set the main window
window = tk.Tk()
window.geometry('500x500')
window.resizable(False, False)
# Create the canvas. Width is larger than window
canvas = turtle.Canvas(window, width=2000, height=500)
xx = -500
canvas.place(x=xx, y=0)
# Bring the turtle
t = turtle.RawTurtle(canvas)
t.shape('turtle') # nicer look
t.speed(0)
t.penup()
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
# key binding
window.bind('<KeyPress-Left>', keypress)
window.bind('<KeyPress-Right>', keypress)
window.bind('<KeyPress-space>', drawCircle)
drawCircle(None)
speed = 3 # scrolling speed
window.mainloop()
导入海龟
将Tkinter作为tk导入
def按键(事件):
全球xx,画布,t,速度
ev=event.keysym
如果ev==‘左’:
xx+=速度
其他:
xx-=速度
canvas.place(x=xx)
t、 设置位置((-canvas.winfo_width()/4)-(xx+250),0)
一无所获
def drawCircle(u3;):
全球t
t、 彭敦()
t、 fillcolor(0,0,1.0)
t、 填充(真)
t、 圆圈(100)
t、 填充(假)
t、 fillcolor(0,1,0)
t、 彭普()
#设置主窗口
window=tk.tk()
窗口几何体('500x500')
窗口。可调整大小(False,False)
#创建画布。宽度比窗户大
画布=海龟。画布(窗口,宽度=2000,高度=500)
xx=-500
canvas.place(x=xx,y=0)
#把乌龟带来
t=海龟。生海龟(帆布)
t、 形状(“海龟”)#好看一点
t、 速度(0)
t、 彭普()
t、 设置位置((-canvas.winfo_width()/4)-(xx+250),0)
#密钥绑定
窗口绑定(“”,按键)
窗口绑定(“”,按键)
窗口绑定(“”,drawCircle)
drawCircle(无)
速度=3#滚动速度
window.mainloop()
要实现真正的无限滚动,需要每次使用所需的偏移量重新绘制画布中的每个项目,而不是实际移动或滚动画布。像这样的函数可以在静态背景下产生移动的错觉,但它会重置图形