This one is pretty straightforward. How can I get a turtle.Turtle() to write in a custom font saved in the same folder? I've looked all over but have found no real answer. Thanks in advance!
Here's what I did just now on OSX:
Downloaded the TTF Pacifico font from FontSquirrel.com (just a random choice.)
Via the Finder, opened the 'pacifico' folder in Downloads
Clicked on Pacifico.ttf which caused this panel to open:
Selected Install Font on this panel. After a few tries, got the Font Validation panel in Font Book:
Clicked on the font check box (or 'Select all fonts') and then pressed Install Checked. After a few tries, including possibly logging out and logging in again, I was able to run the following:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.write("Pacifico Font", align='center', font=('Pacifico', 48))
screen.mainloop()
Which produced the window:
Which to my surprise, actually worked!
Python turtle is built atop tkinter, so the question is: "How can I use custom local fonts in Python tkinter?"
My understanding is there is no single answer, as tkinter depends on the operating system for fonts. So my solution was to install the font.
In the OSX case, the above steps installed it as a 'User' (personal) font, which I could remove once finished with it.
Related
Recently, I’ve been using pygame and found out when I create a window using pygame.display.set_mode((0, 0), pygame.FULLSCREEN), it's shown in the wrong resolution. I have a 2560 × 1600 monitor, but the window is at 1440 × 900 (It's still fullscreen, but the image quality has been reduced).
It's been shown in the docs for pygame.display, I believe that the problem is caused by stretching:
Some display environments have an option for automatically stretching all windows. When this option is enabled, this automatic stretching distorts the appearance of the pygame window. In the pygame examples directory, there is example code (prevent_display_stretching.py) which shows how to disable this automatic stretching of the pygame display on Microsoft Windows (Vista or newer required).
and this answer to a similar question, shown that in windows the problem can be solved using the code below:
ctypes.windll.user32.SetProcessDPIAware()
true_res = (windll.user32.GetSystemMetrics(0),windll.user32.GetSystemMetrics(1))
screen = pygame.display.set_mode(true_res,pygame.Fullscreen)
Sadly, I don't know how to do a similar job in macOS.
I've been able to work this problem out in the fullscreen mode using the code below:
screen = pygame.display.set_mode((2560, 1600), pygame.FULLSCREEN | pygame.SCALE)
Still don't know how to solve this under other circumstances, so I remain the question open.
you can maybe ask the user what his resolution is from a drop-down menu and save that variable in a text file then set the screen width and height to the contexts of that text file
I'm working with python's turtle module and want to load different fonts. I've read that turtle uses tkinter's fonts but some of these fonts that should be available are in fact not. Does anyone know how I can add fonts or get a list of available ones? I would especially like to write arabic, hebrew fonts.
Try this to list fonts.
import tkinter as tk
r = tk.Tk()
print(list(tk.font.families()))
Turtle is based on tkinter, so you list the tkinter fonts.
To add a font, first download the font you want and install it on your system.
Then you can use this font in your code as shown in the example.
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.write("Pacifico Font", align='center', font=('Pacifico', 48))
screen.mainloop()
I hope it helps you about adding a new font.
Reference: How can I use custom local fonts in Python Turtle?
Also, you can check this out too.
I've been trying to work through some beginner python turtle graphics turtorials in python 3.7 on Mac OS X El Capitan.
One of the things that seems annoying is that the turtle graphics window is opening up underneath the other windows.
I don't know if there is something wrong with my configuration, or whether it is typical of turtle graphics, but it would be nice if there an easy way to bring the window to the foreground. I have searched and found out that turtle graphics is based on tkinter, and found a couple of post on bringing tkinter windows to thr foreground.
I was able to get the following code to work with IDLE, but it doesn't work when running the program from the terminal.
import turtle
# works in IDLE, but not in OS X terminal
root = turtle.getscreen()._root
root.attributes("-topmost", true)
turtle.forward(100)
turelt.left(120)
I have tried looking through the turtle graphics documentation, but I didn't find any commands to bring a window to the foreground, and I am not fond of the idea of having to do a deep read through tkinter documentation just to be able to run a beginner turtle graphics tutorial.
That being said, knowing the right commands to get a tkinter-able reference to the turtle window, and knowing the correct tkinter commands would seem like an acceptable workaround.
Are there any basic turtle commands to bring the window to the foreground, or is there a way to get a reference to the root tkinter window in order to bring it to ther foreground??
Thanks in advance for any help or suggestions!!
Sincerely,
Bryan Pierce
I am using Visual Studio Code as my IDE and I am a bit of a beginner with Python so I decided to try out the turtle library built into Python to learn some of the syntax. However, when I tried running just a simple script to see if it would work, the window flashed open for less than a second then closed. I have tried using different extensions and re-downloading the python extension for VS Code. This is my code I'm trying to run:
import turtle
geoff = turtle.Turtle()
geoff.forward(100)
Please help as I really can't figure out why the window won't stay open. Thanks!
The screen flashed on and then closed because when the application is done, Python exits and thus so does the screen. This has nothing to do with VS Code or the Python extension and simply how applications work.
Probably the easiest way to keep the window open is add the following line at the very end:
input("Press any key to exit ...")
That way Python won't exit until you press a key in the terminal.
The easiest solution is to add the following line in your V.S. Code:-
turtle.done()
This would prevent the window (Python Turtle Graphics) from closing after running the code:)
You can use exitonclick() to avoid the window from shutting down.
import turtle
window = turtle.Screen()
geoff = turtle.Turtle()
geoff.forward(100)
window.exitonclick()
This way, the graphics window will shut only after you click.
You can create a canvas into turtle like a blank space to draw on. Use this code just to import the module an hold on the graphic window open -Pen It will work with Visual Studio Code, Spyder or Python IDLE
import turtle
window = turtle.Screen()
geoff = turtle.Turtle()
t = turtle.Pen()
window.exitonclick()
Quick Question.
How do I make Python Turtle Graphics screen pop-up directly on my screen and not in the background where I have to manually click on it to open it. I need to watch Python draw
To make your question Quick you need to post some code.
import turtle
myTurtle = turtle.Turtle()
myTurtle.circle(50)
turtle.getscreen()._root.mainloop()
starts on foreground for me (tried to run from komodo edit and command line on Ubuntu 64)