全屏我的 Python 乌龟 Canvas 不修改大小

Full screen my Python turtle Canvas without modifying the size

我这里有一个代码可以将乌龟 canvas 设置为 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)

您可以使用 setworldcoordinates() 设置虚拟坐标,以保持 640 x 480 虚构,而不管实际 window 大小:

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)
# ...

你的圈子应该是圆形的。