问题
Is there a way to disable the window resizing in the Turtle module? E.G - Disable the maximize and minimize button and disable the ability to drag the window out or in. Thanks!
回答1:
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.
回答2:
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 i.e. 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()
来源:https://stackoverflow.com/questions/48629444/python-turtle-disable-window-resize