简体   繁体   中英

Python Turtle - Disable Window Resize

Is there a way to disable the window resizing in the Turtle module? EG - Disable the maximize and minimize button and disable the ability to drag the window out or in. Thanks!

There's another way of doing it which is a little more 'hacky' but works well for projects that are already written using TurtleScreen and not a RawTurtle . It is actually a one-liner:

screen = turtle.Screen()
# ...
screen.cv._rootwindow.resizable(False, False)

This accesses the root window of the scrollable canvas object that turtle creates and calls the resizable method on it. This is not documented, though - so it might produce unexpected behavior.

As a general remark: Whenever you want to use functionality of tkinter in a turtle program and you cannot find a turtle method for it - just check turtle 's sources , figure out how turtle abstracts away the tkinter object (like the canvas in this case) and use the appropriate method on that object directly. Probably doesn't work all the time - but mostly you'll be able to achieve what you want.

Python turtle is built atop tkinter. When you run the turtle module standalone , it creates a tkinter window, layers it with a scrollable canvas and wraps in a screen object that provides lots of niceties for working with the turtle. But you can instead run the turtle module embedded ie build whatever kind of tkinter window you want and run turtle inside it.

Here's a very simple example of a window with a turtle drawing that's not resizeable:

from tkinter import *
from turtle import RawTurtle

root = Tk()
root.resizable(False, False)

canvas = Canvas(root)
canvas.pack()

turtle = RawTurtle(canvas)

turtle.circle(10)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM