Is it possible to make turtle.Screen() background transparent? - python

Is it possible to make turtle.Screen() background transparent?
If not then how to draw lines on screen?
I want to create program which gives me hints on pool game.

Since turtle is built atop tkinter, if we work with an embedded turtle, rather than standalone, then your ability to create a transparent turtle window is a function of your version of the Tk library and your operating system platform:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas
root = tk.Tk()
root.attributes("-alpha", 0.3)
canvas = ScrolledCanvas(root)
canvas.pack()
screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)
turtle.width(10)
turtle.penup()
turtle.sety(-100)
turtle.pendown()
turtle.circle(100)
screen.mainloop()
In other words, "maybe".

The essence of this answer is the same as #cdlane's answer: obtain a reference to the Tk root window, and set its transparency with myroot.attributes('-alpha', 0.3). cdlane's answer obtains the rootwindow by starting from Tk, and constructing all the necessary Tk objects before constructing a TurtleScreen and Turtle. This answer starts from the other end: it allows turtle to initialise the Tk objects it needs, and traverses object references to get from the turtle to the Tk root window.
Both answers then set the Tk root window's transparency. Which method is more useful depends on your usecase.
Code (made to be as similar to cdlane's as possible):
from turtle import *
turtle = getturtle()
# This does not have to come before the drawing code --
# you can alter window transparency at any moment.
root = (turtle
._screen
.getcanvas()
.winfo_toplevel())
root.attributes('-alpha', 0.3)
turtle.width(10)
turtle.penup()
turtle.sety(-100)
turtle.pendown()
turtle.circle(100)

Related

using the turtle module without opening the canvas (to obtain coordinates)

Is it possible to use the turtle module to obtain coordinates (turtle.pos() once at the desired locations) without it opening a canvas?
I know this probably seems silly and defeats the point of the turtle module, but it's actually a very useful way of getting the coordinates of some points I need.
Many thanks
Instead of running turtle standalone, you could run it embedded and withdraw the root window on startup:
import tkinter as tk
from turtle import RawTurtle
root = tk.Tk()
root.withdraw()
canvas = tk.Canvas(width=500, height=500)
turtle = RawTurtle(canvas)
turtle.sety(-1)
turtle.circle(1, extent=45)
print(turtle.position())

How do I set the background color for my Canvas screen in my turtle game?

I want to set a bgcolor for my Turtle game, but I don't know how to do that with the tkinter Canvas.
My turtle Code is combined with tkinter.
from tkinter import*
from turtle import Canvas, RawTurtle, TurtleScreen
root = Tk()
root.title("Pong Game")
canvas = Canvas(root)
canvas.config(width=1200, height=600)
canvas.pack()
screen = TurtleScreen(canvas)
I want to set my Background color to black.
If you know how to do it, or want to see more of my code, please tell me.
If I try to set my background color to black, it looks like this:
From the updated code you posted you're creating the RawTurtle object with the canvas instead of screen:
SchlägerL = RawTurtle(screen) # pass screen instead of canvas

Converting code from Python Turtle to Tkinter

I've been happily going along writing programs using Python Turtle Graphics, and occasionally super-charging that module with forays into tkinter when turtle wasn't quite up to the job. An example is shown below.
What would be the most direct translation into "non-terrible" pure tkinter code? Is it always recommended to use OOP with tkinter? If so what would my program look like using that paradigm? If not, how would it look with just procedural tkinter? I've seen so many different ways to work with tkinter and create roots and masters etc., it's all a bit confusing. I'd like to keep the simplicity of the the approach I have used as much a possible. As usual, any help much appreciated.
import turtle
import tkinter as tk
def do_stuff(a_turtle):
for color in ["red", "yellow", "green"]:
a_turtle.color(color)
a_turtle.right(120)
def press():
print("ouch")
if __name__ == "__main__":
screen = turtle.Screen()
screen.bgcolor("cyan")
canvas = screen.getcanvas()
button = tk.Button(canvas.master, text="Press me", command=press)
canvas.create_window(-200, -200, window=button)
my_lovely_turtle = turtle.Turtle(shape="turtle")
do_stuff(my_lovely_turtle)
turtle.done()

How can I create a button in turtle?

How to create a simple button in turtle, python, where if you click it, you can define it to print messages, or do other, more complex things.
You can embed turtle in tkinter, as #JoshuaNixon suggests in his comment, using tkinter buttons to control your turtle canvas. If you want to work within standalone turtle, I recommend using a turtle as a button as they can be coerced into any shape and/or color and have individual onclick event handlers so you don't have to figure out where the user clicked on the screen:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
FONT_SIZE = 12
FONT = ('Arial', FONT_SIZE, 'bold')
def draw_onclick(x, y):
turtle.dot(100, 'cyan')
button = Turtle()
button.hideturtle()
button.shape('circle')
button.fillcolor('red')
button.penup()
button.goto(150, 150)
button.write("Click me!", align='center', font=FONT)
button.sety(150 + CURSOR_SIZE + FONT_SIZE)
button.onclick(draw_onclick)
button.showturtle()
turtle = Turtle()
turtle.hideturtle()
screen = Screen()
screen.mainloop()
Note that Turtle.onclick() is different than Screen().onclick -- one only happens when clicking on a specific turtle instance whereas the other happens when clicking anywhere on the screen.
Since the Python Turtle Graphics Module is built on top of Tkinter, a Tkinter button should work on a turtle screen
from turtle import Screen
from tkinter import *
screen = Screen()
screen.setup(width=600, height=400)
def do_something():
print("Good bye")
canvas = screen.getcanvas()
button = Button(canvas.master, text="Exit", command=do_something)
button.pack()
button.place(x=300, y=100) # place the button anywhere on the screen
screen.exitonclick()
I haven't tried this out but this might work:
root = turtle.Screen()._root
btn = Button(root, text="This button exists in turtle")
btn.pack()
That should be it!
Note: Since turtle is based on tkinter the turtle.Screen() contains the tk() root
We are able to access that root and create a tkinter button and add it to it.
Edit: If you add a command parameter in pack you can make the button execute a function
To create a simple button, there might be other ways, but this is how I do it.
import turtle
def button(x,y):
if x < 50 and x > -50 and y < 50 and y > -50:
print(f"Your coordinates are: ({x}, {y}).")
turtle.onscreenclick(button, 1, add=False)
turtle.done()
To explain this, button is just a function, it has nothing to do with an actual button yet. The if statement in there basically takes the x,y variables that are its parameters, and checks whether they are between two numbers, in this case, coordinates.
The onscreenclick function takes three parameters. The first is a function with two parameters. Wherever you click on the turtle pop-up, it will take the x,y coordinates of where you clicked and inserts it into the function. The second is a number. This number refers to how you are going to click it (Ex. right-click, left-click, etc.) In most cases, it is 1 since 1 is left-click. Finally, the third parameter is necessary when you have multiple buttons. If you are creating a second, third, etc. button, and you want to create the new button without overwriting the previous button, you write add=True. If you want to make it so all previous buttons are canceled, you write True. So, finally, the code above would print the coordinates of where you clicked if they were both between -50 and 50.
You can do a lot of useful things with this function. You can create it as a temporary button to help you while writing with turtle, where the "whole screen" is a large button where it prints the x,y coordinates of where you clicked. This can be useful in getting the approximate coordinates of where you want your turtle to go next.
Or you could use it your actual code, to get information from the user or as part of a game.
All in all, this is a simple way to create a button just using turtle and no other modules and has great flexibility.
If there are any other ways, using or not using turtle, complex or simple, please post it below as an answer.
NOTE: You wouldn't be able to "see" the button by default. If you wanted, though, you could make a turtle draw the outline of the button or something.

Run a turtle program in full screen when initiated

I wrote a Python 3.7 program with Turtle Graphics. It is a simple program and works fine, but I want it to run full-screen when I initiate the program. How can I do this? There is no full-screen option in the Turtle documentation.
import turtle
from turtle import *
a = turtle.Turtle()
a.speed(10)
a.color('red', 'blue')
# a.begin_fill()
for i in range(90):
a.fd(200)
a.lt(169)
# a.end_fill()
turtle.done()
The width and height arguments to the setup() method take integer (pixel) and floating point (percentage of screen). By supplying 1.0 you'll get the largest window that turtle can make:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=1.0, height=1.0)
a = Turtle()
a.speed('fastest')
a.color('red', 'blue')
# a.begin_fill()
for _ in range(36):
a.forward(200)
a.left(170)
# a.end_fill()
screen.mainloop()
But this isn't necessarily the operating systems sense of Full Screen where the window overlays everything. It's just the largest window turtle can create given available screen real estate.

Categories

Resources