1

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)
asked May 25, 2018 at 3:10

1 Answer 1

2

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.

answered May 25, 2018 at 6:09
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Sir. It helped me a lot. God bless you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.