I have a code here that sets the turtle canvas by 640x480. I want it to be in fullscreen without changing its width and height. Is that even possible? Like the Tkinter state zoomed? Here is my code.
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 Answer 1
You can set virtual coordinates with setworldcoordinates() to maintain the 640 x 480 fiction regardless of actual window size:
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()
However, your circle is likely to look like an oval as you've mapped one shape rectangle onto another and lost your original aspect ratio. If you're willing to increase one of your dimensions to perserve your aspect ratio, then augment the above code with this calculation:
# ...
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)
# ...
And your circle should be circular.