I am making a calculator with turtle-graphics. I want the turtle to type something when I press a button. The code is below:
import turtle
wn = turtle.Screen()
wn.title("rob it")
wn.bgcolor("black")
wn.setup(width=500, height=600)
CURSOR_SIZE = 20
FONT_SIZE = 12
FONT = ('Arial', FONT_SIZE, 'bold')
button = turtle.Turtle()
button.hideturtle()
button.shape('circle')
button.fillcolor('red')
button.penup()
button.goto(150, 650)
button.write("Click me!", align='center', font='arial')
button.sety(150 + + FONT_SIZE)
button.onclick('type_0')
button.showturtle()
#I think that's where the error occurs
def type_0(x, y):
turtle.penup
turtle.color('white')
turtle.goto(150, 650)
turtle.pendown
turtle.write('hello!', font='arial', align='center')
turtle.hideturtle
while True:
wn.update()
But I am getting this error. I have tried everything and I cannot fix it. Any solutions? For some reason I am not able to call the type_0 function.
So I am typing this because I don't know what other details to add and I am new to python so I will appreciate any comment or help.
The following is an implementation which will work but not an optimal/elegant solution. Okay to start with. As you learn more you can make your code much more elegant. One of the way is to use a class with methods. Also, please note that x,y arguments are not used in your function.
import turtle
wn = turtle.Screen()
wn.title("rob it")
wn.bgcolor("black")
wn.setup(width=500, height=600)
CURSOR_SIZE = 20
FONT_SIZE = 12
FONT = ('Arial', FONT_SIZE, 'bold')
button = turtle.Turtle()
button.hideturtle()
button.shape('circle')
button.fillcolor('red')
button.penup()
button.goto(150, 650)
button.write("Click me!", align='center', font='arial')
button.sety(150 + + FONT_SIZE)
def type_0(x, y,turtle_in_fn=button ):
turtle_in_fn.penup
turtle_in_fn.color('white')
turtle_in_fn.goto(150, 150)
turtle_in_fn.pendown
turtle_in_fn.write('hello!', font='arial', align='center')
turtle_in_fn.hideturtle
# Note that type_0 is a function. type_0 should not be within single quotes ''
button.onclick(type_0)
button.showturtle()
while True:
wn.update()
Related
For a school project I have to make a program that randomly selects a song from a file and inputs the artist's name and song title and the player has to fill in the blank from the artist name and song title in order to earn points.
One of the requirements for the project is that it randomly selects the file from the folder for the song so I've tried using the pickle module and turtle module - for the tab and somewhere to output the program if that makes sense and ive worded it correctly.
Unfortunately nothing seems to be many things. The biggest I'm having is because I've got the songs in the code it overlaps them in the tab when its been run and I want the program to only show one song at a time and hide the rest until the user has guessed
Here's my code:
import turtle
import os
import pickle
import random
#Screem
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Main Screen")
#Border
border = turtle.Turtle()
border.speed(0)
border.penup()
border.color("white")
border.setposition(-300, -300)
border.pensize(3)
border.pendown()
for side in range(4):
border.fd(600)
border.lt(90)
border.hideturtle()
song1 = turtle.Turtle()
song1.color("white")
song1.penup()
song1.speed(0)
song1.setposition(-200, -200)
song1string = ("_ost malone _n god")
song1.write(song1string, False, align="Left", font=("Arial", "14", "normal"))
song1string2 = ("_minem _ap _od")
song1.write(song1string2, False, align="Left", font=("Arial", "14", "normal"))
song1.hideturtle()
song1.clear()
songs = []
songs.append(song1string)
songs.append(song1string2)
pickle.dump(songs, open("songs.py", "wb"))
songs = pickle.load(open("songs.py", "rb"))
final_song = random.choice("songs.py")
screen.mainloop()
Frankly I don't understand exactly what your code is doing, nor why you're even using pickle. Regardless, maybe the following changes with # ALL CAP comments indicating where they were amde will help:
import turtle
import os
import pickle
import random
#Screem
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Main Screen")
#Border
border = turtle.Turtle()
border.speed(0)
border.penup()
border.color("white")
border.setposition(-300, -300)
border.pensize(3)
border.pendown()
for side in range(4):
border.fd(600)
border.lt(90)
border.hideturtle()
song1 = turtle.Turtle()
song1.color("white")
song1.penup()
song1.speed(0)
song1.setposition(-200, -200)
song1string = ("_ost malone _n god")
song1.write(song1string, False, align="Left", font=("Arial", "14", "normal"))
song1.setposition(-200, -180) # ADDED SO NEXT ONE IS ON DIFFERENT LINE
song1string2 = ("_minem _ap _od")
song1.write(song1string2, False, align="Left", font=("Arial", "14", "normal"))
song1.hideturtle()
#song1.clear() # DISABLED
songs = []
songs.append(song1string)
songs.append(song1string2)
pickle.dump(songs, open("songs.pkl", "wb")) # Changed file name.
songs = pickle.load(open("songs.pkl", "rb")) # Changed file name.
final_song = random.choice(songs) # CHANGED TO SELECT ONE FROM UNPICKLED LIST.
print('final_song: {!r}'.format(final_song)) # ADDED
screen.mainloop()
I am trying to create a turtle based, text adventure game with an interactive GUI. But I ran into a problem. In the image attached, you will see a ">" and then after that, I would like the user to be able to type a command, and submit it to determine the path of a game. I am relatively new to python, so I am not familiar with external libraries or modules that could help with this.
import os
import turtle
#SCREEN
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Untitled Game")
#Box
box = turtle.Turtle()
box.color("white")
box.speed(0)
box.penup()
box.setposition(-500, -200)
box.pensize(5)
box.pendown()
box.fd(900)
#TEXT IN TEXT BOX
text = turtle.Turtle()
text.speed(0)
text.color("white")
text.penup()
text.setposition(-350, 300)
text.write("This is a test", False, align="left", font=("Helvetica", 25, "normal"))
text.hideturtle()
#TEXT ARROW
arrow = turtle.Turtle()
arrow.speed(0)
arrow.color("white")
arrow.penup()
arrow.setposition(-360, -300)
arrow.pendown()
arrow.pensize(4)
arrow.write(">", False, align="left", font=("Helvetica", 60, "normal"))
screen.mainloop()
What it looks like right now
I am attempting to create a quick turtle display using Tkinter, but some odd things are happening.
First two turtle windows are being created, (one blank, one with the turtles), secondly, any attempt of turning the tracer off is not working.
This might be a simple fix but at the moment I cannot find it.
Any help would be appreciated,
Below is the code:
import tkinter as tk
import turtle
window = tk.Tk()
window.title('Top 10\'s')
def loadingscreen():
canvas = tk.Canvas(master = window, width = 500, height = 500)
canvas.pack()
arc1 = turtle.RawTurtle(canvas)
arc2 = turtle.RawTurtle(canvas)
#clean up the turtles and release the window
def cleanup():
turtle.tracer(True)
arc1.ht()
arc2.ht()
turtle.done()
#animate the turtles
def moveTurtles(rangevar,radius,extent,decrease):
for distance in range(rangevar):
arc1.circle(-radius,extent = extent)
arc2.circle(-radius,extent = extent)
radius -= decrease
#Set the turtle
def setTurtle(turt,x,y,heading,pensize,color):
turt.pu()
turt.goto(x,y)
turt.pd()
turt.seth(heading)
turt.pensize(pensize)
turt.pencolor(color)
#draw on the canvas
def draw():
#set variables
rangevar = 200
radius = 200
decrease = 1
extent = 2
#setup and draw the outline
turtle.tracer(False)
setTurtle(arc1,0,200,0,40,'grey')
setTurtle(arc2,14,-165,180,40,'grey')
moveTurtles(rangevar,radius,extent,decrease)
#setup and animate the logo
turtle.tracer(True)
setTurtle(arc1,0,200,0,20,'black')
setTurtle(arc2,14,-165,180,20,'black')
moveTurtles(rangevar,radius,extent,decrease)
#main program
def main():
turtle.tracer(False)
arc1.speed(0)
arc2.speed(0)
draw()
cleanup()
if __name__ == "__main__":
try:
main()
except:
print("An error occurred!!")
loadingscreen()
Essentially I am creating a Tk window, then a canvas, then two turtles, and then animating these turtles
My guess is you're trying to call turtle screen methods without actually having a turtle screen. When turtle is embedded in tkinter like this, you can overlay a Canvas with a TurtleScreen instance which will provide some, but not all, of the screen features of the standalone turtle:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen
def cleanup():
""" hide the turtles """
arc1.hideturtle()
arc2.hideturtle()
def moveTurtles(rangevar, radius, extent, decrease):
""" animate the turtles """
for _ in range(rangevar):
arc1.circle(-radius, extent=extent)
arc2.circle(-radius, extent=extent)
radius -= decrease
def setTurtle(turtle, x, y, heading, pensize, color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(heading)
turtle.pensize(pensize)
turtle.pencolor(color)
def draw():
# set variables
rangevar = 200
radius = 200
decrease = 1
extent = 2
screen.tracer(False) # turn off animation while drawing outline
# setup and draw the outline
setTurtle(arc1, 0, 200, 0, 40, 'grey')
setTurtle(arc2, 14, -165, 180, 40, 'grey')
moveTurtles(rangevar, radius, extent, decrease)
screen.tracer(True) # turn animation back on for the following
# setup and animate the logo
setTurtle(arc1, 0, 200, 0, 20, 'black')
setTurtle(arc2, 14, -165, 180, 20, 'black')
moveTurtles(rangevar, radius, extent, decrease)
# main program
window = tk.Tk()
window.title("Top 10's")
canvas = tk.Canvas(master=window, width=500, height=500)
canvas.pack()
screen = TurtleScreen(canvas)
arc1 = RawTurtle(screen)
arc1.speed('fastest')
arc2 = RawTurtle(screen)
arc2.speed('fastest')
draw()
cleanup()
Another suggestion: don't mess with tracer() until after everything else is working and then (re)read it's documentation carefully.
I just have the answer for the two windows that are being created: the one with the turtles is obvious, and the blank one is for the main root window that you define (window = tk.Tk()). If you want it not to appear at all, you can add the following line right after its definition:
window.withdraw()
I found this solution here and here.
I've been trying to tkinters .Entry command and use the user input to put into turtle, but I keep getting an error:
In my case, I am trying to ask the user for a color that they want to use in turtle.
My code:
import tkinter
from turtle import Turtle
#Create and Format window
w = tkinter.Tk()
w.title("Getting To Know You")
w.geometry("400x200")
#Favorite Color
lbl3= tkinter.Label(w, text = "What's your favorite color?")
lbl3.grid(row = 10 , column = 2)
olor = tkinter.Entry(w)
olor.grid(row = 12, column = 2)
t = Turtle()
t.begin_fill()
t.color(olor)
shape = int (input ("What is your favorite shape?"))
w.mainloop()
My recommendation is you work within turtle completely and not drop down to the tkinter level:
from turtle import Turtle, Screen
# Create and Format window
screen = Screen()
screen.setup(400, 200)
screen.title("Getting To Know You")
# Favorite Color
color = screen.textinput("Choose a color", "What's your favorite color?")
turtle = Turtle()
turtle.color(color)
turtle.begin_fill()
turtle.circle(25)
turtle.end_fill()
turtle.hideturtle()
screen.mainloop()
If you must do this from tkinter, you need to read more about tkinter elements like Entry to know their capabilities. You also need to read more about turtle as it is invoked differently when embedded inside of a tkinter window. Here's a rough approximation of what you're trying to do:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas
root = tk.Tk()
root.geometry("400x200")
root.title("Getting To Know You")
def draw_circle():
turtle.color(color_entry.get())
turtle.begin_fill()
turtle.circle(25)
turtle.end_fill()
# Favorite Color
tk.Label(root, text="What's your favorite color?").pack()
color_entry = tk.Entry(root)
color_entry.pack()
tk.Button(root, text='Draw Circle', command=draw_circle).pack()
canvas = ScrolledCanvas(root)
canvas.pack(fill=tk.BOTH, expand=tk.YES)
screen = TurtleScreen(canvas)
turtle = RawTurtle(screen, visible=False)
screen.mainloop()
I am working on an assignment for school. It's simple, involving file io and turtle graphics, and I am having trouble figuring out why the last line appears disconnected from the rest like this: picture of output
import turtle
def gfxFileIO():
t1 = turtle.Turtle()
window = turtle.Screen()
f=open("file.txt","r")
count = 1
t1.penup()
t1.rt(270)
t1.fd(250)
t1.rt(270)
t1.fd(300)
t1.rt(270)
t1.write("Your file: ", font=("Arial", 16, "normal"))
t1.fd(45)
for line in f:
t1.write(str(count) + ". " + str(line), font=("Arial", 16, "normal"))
t1.fd(16)
count += 1
window.exitonclick()
Text file had extra line breaks. Thank you user DYZ
As posted, this code doesn't run. window is a local variable of gfxFileIO() but is used globally. The one function, gfxFileIO() is never called. Now that the data input problem has been resolved, let's rethink the example code:
from turtle import Turtle, Screen
FONT_SIZE = 16
FONT = ("Arial", FONT_SIZE, "normal")
def gfxFileIO(turtle, file_name):
file = open(file_name)
turtle.penup()
turtle.rt(270)
turtle.fd(250)
turtle.rt(270)
turtle.fd(300)
turtle.rt(270)
turtle.write("Your file: ", font=FONT)
turtle.fd(FONT_SIZE * 3) # double space
count = 1
for line in file:
turtle.write("{}. {}".format(count, line), font=FONT)
turtle.fd(FONT_SIZE)
count += 1
yertle = Turtle()
gfxFileIO(yertle, "file.txt")
screen = Screen()
screen.exitonclick()