Turtle is not running - python-3.x

import turtle
t = turtle.Turtle()
t.right(100)
t.done()
That is my code, but there is no result after run. Why this problem is arising and what can I do now?

t.right(100)
this rotates the turtle by 100 degrees
t.done()
is not a function. All your code is doing essentially is creating a canvas and putting nothing on it. From here, you would need to know about t.pendown() and other functions to make it work this link will be helpful in getting started with the python turtle.

Related

Get_transform not rotating image

I have been making a game in pyglet for quite a while, and I encountered an error that I cannot fix. I am trying to rotate an image using image.get_transform(rotate=deg), however get the error
AssertionError: Only 90 degree rotations are supported.
I do not know how to fix this, and the degrees are always between -90 and 90, so I do not see why this is not working. The part of the code that does not work is here:
deg=round(game.getAngle(x,y))
print(deg)
self.sprite=self.sprite.get_transform(rotate=deg)
the getAngle function is as such:
def getAngle(self,x,y):
return math.degrees(math.atan(y/x))
Any help would be appretiated
I am not sure which package you are using, but an alternative might be using rotate from scipy.image
Have a look at the docs here
The code would essentially boil down to using:
from scipy import ndimage
rotated_image = scipy.ndimage.rotate(old_image, angle)

Maximize figures before saving

The question about how to do maximize a window before saving has been asked several times and has several questions (still no one is portable, though), How to maximize a plt.show() window using Python
and How do you change the size of figures drawn with matplotlib?
I created a small function to maximize a figure window before saving the plots. It works with QT5Agg backend.
import matplotlib.pyplot as plt
def maximize_figure_window(figures=None, tight=True):
"""
Maximize all the open figure windows or the ones indicated in figures
Parameters
----------
figures: (list) figure numbers
tight : (bool) if True, applies the tight_layout option
:return:
"""
if figures is None:
figures = plt.get_fignums()
for fig in figures:
plt.figure(fig)
manager = plt.get_current_fig_manager()
manager.window.showMaximized()
if tight is True:
plt.tight_layout()
Problems:
I have to wait for the windows to be actually maximized before using the plt.savefig() command, otherwise it is saved with as not maximized. This is a problem if I simply want to use the above function in a script
(minor problems:)
2. I have to use the above function twice in order to get the tight_layout option working, i.e. the first time tight=True has no effect.
The solution is not portable. Of course I can add all the possible backend I might use, but that's kind of ugly.
Questions:
how to make the script wait for the windows to be maximized? I don't want to use time.sleep(tot_seconds) because tot_seconds would be kind of arbitrary and makes the function even less portable
how to solve problem 2 ? I guess it is related to problem 1.
is there a portable solution to "maximize all the open windows" problem?
-- Edit --
For problem 3. #DavidG suggestion sounds good. I use tkinter to automatically get width and height, convert them to inches, and use them in fig.set_size_inches or directly during the figure creation via fig = plt.figure(figsize=(width, height)).
So a more portable solution is, for example.
import tkinter as tk
import matplotlib.pyplot as plt
def maximize_figure(figure=None):
root = tk.Tk()
width = root.winfo_screenmmwidth() / 25.4
height = root.winfo_screenmmheight() / 25.4
if figure is not None:
plt.figure(figure).set_size_inches(width, height)
return width, height
where I allow the figure to be None so that I can use the function to just retrieve width and height and use them later.
Problem 1 is still there, though.
I use maximize_figure() in a plot function that I created (let's say my_plot_func()) but still the saved figure doesn't have the right dimensions when saved on file.
I also tried with time.sleep(5) in my_plot_func() right after the figure creation. Not working.
It works only if a manually run in the console maximize_figure() and then run my_plot_func(figure=maximized_figure) with the figure already maximized. Which means that dimension calculation and saving parameters are correct.
It does not work if I run in the console maximize_figure() and my_plot_func(figure=maximized_figure) altogether, i.e. with one call the the console! I really don't get why.
I also tried with a non-interactive backend like 'Agg', so that the figure doesn't get actually created on screen. Not working (wrong dimensions) no matter if I call the functions altogether or one after the other.
To summarize and clarify (problem 1):
by running these two pieces of code in console, figure gets saved correctly.
plt.close('all')
plt.switch_backend('Qt5Agg')
fig = plt.figure()
w, h = maximize_figure(fig.number)
followed by:
my_plot_func(out_file='filename.eps', figure=fig.number)
by running them together (like it would be in a script) figure is not saved correctly.
plt.close('all')
plt.switch_backend('Qt5Agg')
fig = plt.figure()
w, h = maximize_figure(fig.number)
my_plot_func(out_file='filename.eps', figure=fig.number)
Using
plt.switch_backend('Agg')
instead of
plt.switch_backend('Qt5Agg')
it does not work in both cases.

How do I increase the speed of RawTurtle?

I'm currently using an embedded turtle canvas in a tkinter window. While it's intuitive that all I need to do is set my turtle to turtle.RawTurtle(canvas), there are some functions that just don't work, and I can't figure out why.
t.clear();t.pu();t.speed(0);t.ht();t.tracer(0)
But I get the error:
AttributeError: 'RawTurtle' object has no attribute 'tracer'
Despite this, many other functions work, such as clear, penup, speed, and hideturtle.
Is there any way of disabling screen updates until the drawing is finished, then manually updating the canvas, with RawTurtle?
The tracer() method is a method of the turtle's screen, not the turtle itself. To get access to it, when embedded under a tkinter window, wrap the canvas in a turtle screen:
screen = turtle.TurtleScreen(canvas)
t = turtle.RawTurtle(screen)
which should give you access to the various screen methods. Then you should be able to use screen.tracer(0) to turn off drawing updates and screen.update() to show the finished drawing. A more complete example:
import tkinter as tk
import turtle
root = tk.Tk()
canvas = turtle.ScrolledCanvas(root)
canvas.pack(side=tk.LEFT)
screen = turtle.TurtleScreen(canvas)
t = turtle.RawTurtle(screen)
t.hideturtle()
# t.speed('fastest')
screen.tracer(0)
t.penup()
t.sety(-100)
t.pendown()
t.circle(100)
screen.update()
screen.mainloop()

Weird way of importing Python turtle works but I don't know why

I hope you will understand my question.
I noticed that I get the same results when I import turtle module in following two ways.
from turtle import Turtle
t=Turtle()
t.screen.bgcolor("black")
and also
import turtle
turtle.bgcolor("black")
I am confused about this, “from turtle import Turtle”.
According to what I know, it means “to import Turtle.py from turtle (folder / package)”. I may be wrong, you can help me out to understand better.
But I can’t find any Turtle.py module. It is only turtle.py I saw.
What's weird about it is that it works.
Can anyone tell me why?
I am using Python version 3.6
Python's turtle.py is unusual in that it presents both a function-based interface and an object-oriented interface. Depending on how you import it, you can work with one, or the other, or both.
Here, we are using the object-oriented interface to invoke the screen method bgcolor():
from turtle import Turtle
t = Turtle()
t.screen.bgcolor("black")
I usually write this as:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor("black")
t = Turtle()
as having direct access to the Screen object simplifies things. Using this style import, you cannot access the function-based interface.
When we do this simpler import, we have access to both the function-based interface and the object-oriented interface. Here, we're using the function bgcolor() to set the background color:
import turtle
turtle.bgcolor("black")
Using either the function-based or object-oriented interface to turtle.py is fine, but you can get yourself seriously confused when mixing the two.

Im having trouble trying to time the update of a tkinter label image

I am trying to make a basic 4 frame animation, I cannot use a tkinter canvas as I want it to use the art which I have drawn (the files). There is nothing wrong with the file type as I have tested it on its own. However the code seems to just remove the window for the 6 seconds and then show the final frame.
import time
import tkinter
window=tkinter.Tk()
frame1=tkinter.PhotoImage(file="file1.ppm")
frame2=tkinter.PhotoImage(file="file2.ppm")
frame3=tkinter.PhotoImage(file="file3.ppm")
frame4=tkinter.PhotoImage(file="file4.ppm")
image=tkinter.Label(window,image=frame1)
image.pack()
time.sleep(2)
image.configure(image=frame2)
time.sleep(2)
image.configure(image=frame3)
time.sleep(2)
image.configure(image=frame4)
I'm not sure whether it is the "time.sleep" or the "image.configure" that is the problem but I have tried to mess around with different types of timing methods which also seem to fail.
import tkinter
window=tkinter.Tk()
frame1=tkinter.PhotoImage(file="file1.ppm")
frame2=tkinter.PhotoImage(file="file2.ppm")
frame3=tkinter.PhotoImage(file="file3.ppm")
frame4=tkinter.PhotoImage(file="file4.ppm")
image=tkinter.Label(window,image=frame1)
image.pack()
def loop(n):
frame = [frame1, frame2, frame3, frame4][n]
window.after(2000, lambda : image.configure(image=frame))
window.after(2000, lambda : loop((n+1)%4))
loop(1)

Resources