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.
Related
I want to make a old-fashioned rocket blastoff game.
But, I want to use the Windows CMD font for the text I will put on the screen.
I have the font here:
https://www.dafont.com/windows-command-prompt.font
Now I need to write the code.
How can I load the font into Tkinter and put it on a Label?
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.
I want the GIF to play in the turtle screen. But it is showing only the still image. Please fix this, Any help will be appreciated...
Here is my code:
import turtle
import os
# resources :
bgpic = r"C:\Users\intel\Desktop\xBDT7.gif"
win = turtle.Screen()
win.addshape(bgpic)
sh = turtle.Turtle()
sh.shape(bgpic)
# Shuting the window down :
turtle.mainloop()
From reading the turtle documentation, here, I don't see anything that says animated gifs can be played within turtle. You are free to read the documentation yourself, but It appears that although to use an image as a shape or background in turtle, it needs to be a .gif file, you cannot actually playback animated gifs in turtle.
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!
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.
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()
using Ruby or Python, does someone know how to draw on the screen, covering up any other window? Kind of like, press a key, and the program will show current weather or stock quote on the screen (using the whole screen as the canvas), and then press the key again, and everything restores to the same as before? (like Mac OS X's dash board).
You could use the systems dashboard (desktop widgets, or whatever it's called) API. In order to do that you need bindings to it for Python or Ruby.
Alternatively you could use some generic gui toolkit or application framework and just create a frameless window with transparent background. Then you need to be sure that the chosen toolkit supports 'always-on-top' options on your desired platform(s).
If you are on windows you can directly draw to desktop dc(device context) using win32api
e.g. just for fun try this :)
>>> import win32ui
>>> import win32gui
>>> hdc = win32ui.CreateDCFromHandle( win32gui.GetDC( 0 ) )
>>> hdc.DrawText("Wow it works", (100, 100, 200, 200))
>>> hdc.LineTo(500,500)
but that won't be very useful ,as not erasable
best bet would be to use a transparent window or window with a cutout region (atleast on windows that is possible)
or even if you can't draw transparent on some system you can grab the current screen and display it as background of you window that would give a transparent effect
I would recommend PyGame.