Python – turtle.Screen().setup() 方法
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
turtle.Screen().setup() 方法:
此方法用于设置主窗口的大小和位置。
Syntax : turtle.Screen().setup(width=0.5, height=0.75, startx=None, starty=None)
Parameters: This method has following parameters:
- width: as integer a size in pixels, as float a fraction of the screen. Default is 50% of screen.
- height: as integer the height in pixels, as float a fraction of the screen. Default is 75% of screen.
- startx: if positive, starting position in pixels from the left edge of the screen, if negative from the right edge. Default, startx=None is to center window horizontally.
- starty: if positive, starting position in pixels from the top edge of the screen, if negative from the bottom edge. Default, starty=None is to center window vertically.
以下是上述方法的实现以及一些示例:
示例 1:更改窗口的配置。
Python3
# import turtle package
import turtle
# making turtle object
sc = turtle.Screen()
# setup the screen size
sc.setup(400,400)
# set the background color
sc.bgcolor("blue")
# This code is contributed
# by Deepanshu Rustagi.
Python3
# import turtle package
import turtle
# making turtle object
sc = turtle.Screen()
# set the screen size 400x400 pixels
# set the screen position by
# startx to 50
# starty to-200
sc.setup(400, 400, startx = 50,
starty = -200)
# set the background color
sc.bgcolor("blue")
# This code is contributed
# by Deepanshu Rustagi.
输出 :
示例 2:通过在 setup() 方法中设置 'startx' 和 'starty' 来更改窗口的位置。
Python3
# import turtle package
import turtle
# making turtle object
sc = turtle.Screen()
# set the screen size 400x400 pixels
# set the screen position by
# startx to 50
# starty to-200
sc.setup(400, 400, startx = 50,
starty = -200)
# set the background color
sc.bgcolor("blue")
# This code is contributed
# by Deepanshu Rustagi.
输出 :