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)
Related
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'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()
I am trying to create an application for multiple screens however I so far cannot find a way to locate the secondary screens position (relative to the primary screen by x and y coordinates).
I prefer to use python or bash (via libraries/frameworks are fine). I also checked with xorg.conf and it doesn't reflect my current screen setup.
I am using Ubuntu 11.10 (default Gnome 2 I believe), using compiz as the window manager. So to repeat, my question is how to get the screen layout (coordinates relative to primary screen) of all the monitors preferably by python or bash.
Nevermind, I used Pyqt instead. Here is some code...
from PyQt4.QtGui import QApplication, QPixmap
desktop = QApplication.desktop()
screenRect = desktop.screenGeometry(1) #2nd monitor
print screenRect.x(), screenRect.y() #returns the x and y of that screen
Python binding solution
So, from here you can download the xrandr bindings for python: https://launchpad.net/python-xrandr
# Import the module
from xrandr import xrandr
# Get a screen object to work with
screen = xrandr.get_current_screen()
# Get the active output objects as a list
active_outputs = [o for o in screen.get_outputs() if o.is_active()]
This was as far as I got playing around a little. I hope it will get you started :-) I only have one screen connected right now...
Parsing data solution
The other solution, as I mentioned in my comment above is to parse the output of the command xrandr it looks like it should be pretty simple from just taking a glance at it...
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.