我下载并安装了python 3.6.1并且我检查了。它已正确安装和所有。我甚至使用Homebrew再次安装它以防万一。 但是当我跑去' python -V'或者' python --version',它给出了' python 2.7.10'。 我知道2.7.10它预装了MacOs(我在Mac上运行最新版本的Sierra)但是如何将其更改为运行3.6.1。
真正的问题是,当我编码时,会丢失一堆Turtle(和更多)函数和模块(因为它们只在python 3中添加)。我需要在我的程序中使用这些函数才能使它运行良好。
如果我没有运行' python -V',而是“python3 -V'”,它会给出' python 3.6.1'。 但是当我编码时,它不是正确的版本。此外,如果这很有用,我使用Visual Studio代码进行编程。
这是我的整个计划:
import time
import datetime
from turtle import Turtle, Screen, mainloop
screen = Screen()
turtle1 = Turtle()
screen = turtle1.getscreen()
goPressed = False
currentWatts=0
solarPanelCost=10
Waittimevar2=0
Allsecs1=0
Allsecs2=0
Allsecs3=0
monthvar=0
ship_rotation=0
last_ship_angle=0
def wait_Mins():
for i in range(0,1):
global Waittimevar2
global Allsecs1
global Allsecs2
global Allsecs3
global monthvar
monthvar = int(datetime.datetime.now().strftime("%C"))+int(datetime.datetime.now().strftime("%d"))
Allsecs1 = (int(datetime.datetime.now().strftime("%S")))+(int(datetime.datetime.now().strftime("%M"))*60)+(int(datetime.datetime.now().strftime("%H"))*60*60)+(int(datetime.datetime.now().strftime("%d"))*24*60*60)+(int(datetime.datetime.now().strftime("%m"))*monthvar*24*60*60)+(int(datetime.datetime.now().strftime("%Y"))*12*monthvar*24*60*60)
time.sleep(1.1)
while Allsecs3 < Waittimevar2:
print ("\n"*100)
print ("")
monthvar = int(datetime.datetime.now().strftime("%C"))+int(datetime.datetime.now().strftime("%d"))
Allsecs2=(int(datetime.datetime.now().strftime("%S")))+(int(datetime.datetime.now().strftime("%M"))*60)+(int(datetime.datetime.now().strftime("%H"))*60*60)+(int(datetime.datetime.now().strftime("%d"))*24*60*60)+(int(datetime.datetime.now().strftime("%m"))*monthvar*24*60*60)+(int(datetime.datetime.now().strftime("%Y"))*12*monthvar*24*60*60)
Allsecs3 = Allsecs2 - Allsecs1
percentage=float(float(Allsecs3)/float(Waittimevar2))*100
if percentage < 100:
print ("{}%".format(percentage))
else:
print ("100%")
time.sleep(0.09)
Waittimevar1=0
'''
Waittimevar2=5.123456789
wait_Mins()
print ("Loading...")
time.sleep(2)
print ("booting ship...")
time.sleep(2)
Waittimevar2=9.123456789
wait_Mins()
print("\n"*100)
time.sleep(1.5)
Waittimevar2=29.123456789
wait_Mins()
print("loading libraries...")
time.sleep(3)
Waittimevar2=1.123456789
wait_Mins()
print("Low power.")
time.sleep(1)
print("To make electricity, click the spacebar after clicking on Python Turtle Graphics (The rocket thing that will open in 9.5 seconds).\nDO NOT CLOSE THE PYTHON WINDOW or the game will close!\nPlease fullscreen Python Graphics.\nThe game will be played there.\nTo see Watts, check the terminal.")
time.sleep(1)
print("\nIf you see a scroll bar on your python graphics screen, fullscreen it.")
time.sleep(10)
'''
turtle1.speed(0)
screen.colormode(255)
def printbackground():
turtle1.penup()
turtle1.fillcolor((30, 30, 30))
turtle1.goto(0,-5000)
turtle1.pendown()
turtle1.begin_fill()
turtle1.circle(10000)
turtle1.end_fill()
def printship(angle,previous_angle):
turtle1.penup()
turtle1.home()
turtle1.left(previous_angle)
turtle1.fd(100)
turtle1.width(8)
turtle1.pencolor((30, 30, 30))
turtle1.pendown()
turtle1.seth(0)
turtle1.fd(1)
turtle1.penup()
turtle1.home()
turtle1.pencolor("white")
turtle1.left(angle)
turtle1.fd(100)
turtle1.seth(0)
turtle1.width(4)
turtle1.pendown()
turtle1.seth(0)
turtle1.fd(1)
turtle1.left(90)
turtle1.fd(1)
turtle1.left(90)
turtle1.fd(1)
turtle1.left(90)
turtle1.fd(1)
turtle1.left(90)
def onaclicked():
global currentWatts
currentWatts+=1
print (currentWatts)
def printplanet():
turtle1.penup()
turtle1.pencolor("red")
turtle1.home()
turtle1.pendown()
turtle1.width(75)
turtle1.left(90)
turtle1.forward(1)
turtle1.left(90)
turtle1.forward(1)
turtle1.left(90)
turtle1.forward(1)
turtle1.left(90)
turtle1.forward(1)
def shipmovement():
global ship_rotation
global last_ship_angle
print ("1")
printship(ship_rotation,last_ship_angle)
last_ship_angle=ship_rotation
ship_rotation+=2
def printsidemenu():
#Left menu:
turtle1.penup()
turtle1.pencolor((25, 25, 30))
turtle1.home()
turtle1.seth(90)
turtle1.fd(500)
turtle1.left(90)
turtle1.fd(1050)
turtle1.left(90)
turtle1.width(1700)
turtle1.pendown()
turtle1.fd(1000)
turtle1.penup()
#Right menu:
turtle1.home()
turtle1.seth(90)
turtle1.fd(500)
turtle1.right(90)
turtle1.fd(1050)
turtle1.right(90)
turtle1.width(1700)
turtle1.pendown()
turtle1.fd(1000)
turtle1.penup()
#Left menu borderline:
turtle1.home()
turtle1.pencolor((155, 155, 155))
turtle1.goto(-200, 500)
turtle1.width(1.5)
turtle1.pendown()
turtle1.seth(270)
turtle1.fd(1000)
turtle1.penup()
#Right menu borderline:
turtle1.home()
turtle1.pencolor((155, 155, 155))
turtle1.goto(200,500)
turtle1.width(1.5)
turtle1.pendown()
turtle1.seth(270)
turtle1.fd(1000)
turtle1.penup()
def printsideoptions(level):
#Solar panel:
turtle1.penup()
turtle1.home()
turtle1.goto(-500, 350)
turtle1.pendown()
turtle1.pencolor("white")
turtle1.write("Solar panel", align="left", font=("Source Code Pro", 16, "normal"))
turtle1.penup()
turtle1.goto(-500, 330)
turtle1.pendown()
turtle1.write(str(solarPanelCost)+" Watts. Press 1 to purchase.", align="left",font=("Source Code Pro", 11, "bold"))
def printtext(text,fontsize,locationX,locationY):
turtle1.penup()
turtle1.goto(locationX, locationY)
turtle1.pencolor("white")
turtle1.write(text, align="center", font=("Source Code Pro", fontsize, "normal"))
screen.onkey(onaclicked, "space")
turtle1.setup(width=1920, height=1080)
screen.listen()
screen.screensize(960,540)
turtle1.hideturtle()
printbackground()
printsidemenu()
printplanet()
printsideoptions(1)
printtitle=printtext("In Space",40,0,400)
printtitle
screen.ontimer(shipmovement, 1000)
screen.exitonclick()
当我运行这样的代码时,这是错误消息:
Sophies-iMac-2:python Cedric$ python InSpace.py
Traceback (most recent call last):
File "InSpace.py", line 208, in <module>
turtle1.setup(width=1920, height=1080)
AttributeError: 'Turtle' object has no attribute 'setup'
答案 0 :(得分:1)
由于您说您使用IDE进行编程,我认为它具有要使用的默认Python解释器的设置。它会覆盖环境变量,以确保解释器是IDE已配置为默认值的解释器。尝试更改MSVS设置。
答案 1 :(得分:0)
检查命令which python
的结果。在我的电脑上我有
$ which python
/usr/local/bin/python
在我的具体情况下,我使用brew
安装了python。如果我检查可执行文件,我看到:
/usr/local/bin/python -> ../Cellar/python/2.7.13/bin/python
python3可执行文件是:
/usr/local/bin/python3 -> ../Cellar/python3/3.6.1/bin/python3
您可以使用命令/usr/local/bin/python
使ln
成为您要使用的python版本的新链接。
sudo rm /usr/local/bin/python
sudo ln -s /usr/local/Cellar/python3/3.6.1/bin/python3 /usr/local/bin/python
现在可执行文件是正确的:
/usr/local/bin/python -> /usr/local/Cellar/python3/3.6.1/bin/python3
如果我执行版本:
$ python --version
Python 3.6.1
要使代码正常工作,请更改以下行:
turtle1.setup(width=1920, height=1080)
要
turtle1.screen.setup(width=1920, height=1080)
答案 2 :(得分:0)
答案 3 :(得分:0)
但我最想工作的是'screen.ontimer(shipmovement, 1000)'
这应该被视为一个单独的SO问题,但这里有一个用于移动你的船的MCVE:
from turtle import Turtle, Screen
def printbackground():
turtle1.penup()
turtle1.goto(0, -5000)
turtle1.fillcolor(30, 30, 30)
turtle1.begin_fill()
turtle1.circle(10000)
turtle1.end_fill()
def printship(angle):
turtle1.hideturtle()
turtle1.penup()
turtle1.home()
turtle1.color("white")
turtle1.left(angle)
turtle1.fd(100)
turtle1.seth(0)
turtle1.showturtle()
def shipmovement():
global ship_rotation
printship(ship_rotation)
ship_rotation += 2
screen.ontimer(shipmovement, 1000)
ship_rotation = 0
turtle1 = Turtle(shape='circle')
turtle1.shapesize(4 / 20)
turtle1.speed('fastest')
screen = Screen()
screen.setup(width=1920, height=1080)
screen.colormode(255)
printbackground()
screen.ontimer(shipmovement, 1000)
screen.exitonclick()
要注意的关键点是screen.ontimer()
是一次性的,你必须继续这样做 - 它不会自动重复。
一些一般性指导原则:
不要将一只乌龟(turtle1
)用于一切。分配
单独的海龟。如果这艘船是它自己的乌龟,你没必要
保持(重新)设置它的颜色等。使用单独的海龟进行书写
在屏幕上 - 如果为每一片分配一只乌龟
固定,更新文本,然后你可以简单地undo()
来删除
在写新文章之前的最后一个文本。
移动乌龟然后使用乌龟擦除和重绘更容易。 创建一个自定义形状的乌龟作为你的船,然后移动它 乌龟,没有必要的擦除和重新定位。
我认为你误解了它们之间的区别
screen.setup()
和screen.screensize()
。一个是为了
设置用户看到的窗口大小,另一个定义
画布的背景有多大,您可以滚动到该窗口。
重新阅读文档。