全屏我的Python龟画布而不修改大小

时间:2018-05-25 03:10:31

标签: python turtle-graphics

我这里有一个代码,用于设置640x480的乌龟画布。我希望它在不改变宽度和高度的情况下全屏显示。这甚至可能吗?像Tkinter状态缩放?这是我的代码。

import turtle
import tkinter as tk

ui = tk.Tk()
ui.state('zoomed') #zoom the tkinter
canvas = tk.Canvas(master = ui, width = 640, height = 480) #set canvas size in tkinter
canvas.pack()
t = turtle.RawTurtle(canvas)

1 个答案:

答案 0 :(得分:2)

您可以使用setworldcoordinates()设置虚拟坐标,以维持640 x 480小说,无论实际窗口大小如何:

import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas

width, height = 640, 480

root = tk.Tk()

# We're not scrolling but ScrolledCanvas has useful features
canvas = ScrolledCanvas(root)
canvas.pack(fill=tk.BOTH, expand=tk.YES)

screen = TurtleScreen(canvas)
root.state('zoomed')  # when you call this matters, be careful
screen.setworldcoordinates(-width / 2, -height / 2, width / 2 - 1, height / 2 - 1)

turtle = RawTurtle(screen)

turtle.penup()
turtle.sety(-230)
turtle.pendown()

turtle.circle(230)  # circle that nearly fills our virtual screen

screen.mainloop()

但是,当您将一个形状矩形映射到另一个形状矩形并丢失原始宽高比时,您的圆圈可能看起来像椭圆形。如果您愿意增加其中一个尺寸以保持宽高比,请使用此计算扩充上述代码:

# ...
root.state('zoomed')  # when you call this matters, be careful

window_width, window_height = screen.window_width(), screen.window_height()

if window_width / width < window_height / height:
    height = window_height / (window_width / width)
else:
    width = window_width / (window_height / height)

screen.setworldcoordinates(-width / 2, -height / 2, width / 2 - 1, height / 2 - 1)
# ...

你的圈子应该是圆形的。