turtle-graphics

How to draw an ellipse in Python turtle graphics other than stamping?

不问归期 提交于 2020-01-03 03:54:05
问题 I am trying to draw a letter "O" with Python turtle graphics. To cue the drawing of the "O", the function for it is invoked with a key press. Here is what I have so far: def draw_O(): # Draw an O penup() forward(letter_height/4) pendown() forward(letter_width/2) circle(letter_height/4, 90) forward(letter_height/2) circle(letter_height/4, 90) forward(letter_width/2) circle(letter_height/4, 90) forward(letter_height/2) circle(letter_height/4, 90) forward(letter_width/2) penup() forward(space

Drawing polygon with n number of sides in Python 3.2

浪子不回头ぞ 提交于 2020-01-01 08:48:22
问题 I have to write a program in Python that reads the value n and draws a polygon of n sides on the screen. I can use either the turtle graphics module or graphics.py module. I know how to draw a polygon when n = the number of points you input and then click n times on the screen, but I'm having some trouble getting an idea on how to transform a number of sides into a polygon. Here's the code I have for the polygon with n number of points: def newPolygon(self,cmd): p = eval(input("how many

Turtle does not run more than once in jupyter notebook

不羁的心 提交于 2019-12-31 05:50:50
问题 I am trying to run some turtle code in jupyter notebook. When I run the code once, the code runs fine. However, if I run the code again, Python Turtle Graphics freezes and closes (aka python turtle graphics is not responding) and the jupyter notebook's kernel crashes I have tried to switch notebooks, reinstalling jupyter and restarting the kernel, but none of them worked. Here is the turtle code: import turtle pen = turtle.Pen() window = turtle.Screen() pen.color("light blue") pen.shape(

Python Turtle, draw text with on screen with larger font

僤鯓⒐⒋嵵緔 提交于 2019-12-30 06:05:31
问题 I'm using python turtle's write method to write text on the screen like this: turtle.write("messi fan") The size of the font is too small. How can I increase the size of font? 回答1: Use the optional font argument to turtle.write(), from the docs: turtle.write( arg , move=False , align="left" , font=("Arial", 8, "normal") ) Parameters: arg – object to be written to the TurtleScreen move – True/False align – one of the strings “left”, “center” or right” font – a triple (fontname, fontsize,

Turtle gives error: AttributeError: 'Turtle' object has no attribute 'onkeyrelease'

我的未来我决定 提交于 2019-12-25 09:29:14
问题 I am trying to add 1 every time I release a key: from turtle import * import turtle turtle1 = Turtle() screen = turtle1.getscreen() goPressed = False imported Turtle... currentWatts=0 def onaclicked(): global currentWatts currentWatts+=1 print (currentWatts) defined my function to be run when the key: 1, is released turtle.onkeyrelease(onaclicked, "1") for some reason onkeyrelease isn't there even though I imported Turtle and checked in Python documentation. It SHOULD work, shouldn't it? Did

How do I access a dictionary from a function to be used in another function?

*爱你&永不变心* 提交于 2019-12-25 04:49:10
问题 For an assignment I have to take a string as an input and write it as a file. Then, a function takes the string from the file and puts each word in a dictionary, with the value being the amount of times that word appears in the string. The words will then be printed in a "tower" (similar to a word cloud) with the size of each word based on the amount of times the word appears in the string. These are the two important functions: def word_freq_dict(): # function to count the amount of times a

Python: Passing coordinates from text file to Turtle

老子叫甜甜 提交于 2019-12-25 03:45:49
问题 I have a test file that has coordinates on it my aim it to create a function that takes the text file and turns it into coordinates to plot in turtle to draw an image: river, 5 500, 500 -500, 360 400, 500 shadow, 4 500, 300 5, 500 300, 400 so far I have the following f =open("coordinates.txt", "r") for line in f: line=line.split(",") data=[] if line: data.append([i.strip() for i in line]) After running I get the following: [['river', '5']] [['500', '500']] [['-500', 360]] [['400', '500']] [['

Cannot get colors right in the function

喜你入骨 提交于 2019-12-25 03:23:06
问题 from turtle import * reset() setworldcoordinates(0, 500, 600, 0) def rectangle(a, b, c, d, pen_color, fill_color, pensize): up() setpos (a,b) down() fd (c-a) left(90) fd(d-b) left (90) fd (c-a) left (90) fd (d-b) left(90) p = pen_color.strip("pen_color=") pen_color(p) s = fill_color.strip("fill_color=") fill_color(värv2) a = pensize.strip("pensize=") a = int(a) pensize(a) rectangle(10,10,200,100, pen_color="red", fill_color="blue", pensize=3) Problem is that I cannot get those colors in the

Saving a large turtle graphics image from the entire canvas, not just the window

泄露秘密 提交于 2019-12-25 01:22:20
问题 After looking for answers all around, I find lots of help and guidance on how to save a file either using turtle screen or tkinter canvas mixed with the turtle module. I'm drawing a large image and the windows size is fine and scrollable, letting me see the entire image. But when I try to save the image to postscript, I only get the window view, not the entire canvas. Cannot find out how to proceed, any tips? There might be an answer in How can I convert canvas content to an image? but I

Avoid RecursionError in turtle paint code

£可爱£侵袭症+ 提交于 2019-12-25 01:00:17
问题 I'm creating a simple program that uses turtle in a tkinter canvas to allow the user to draw with the mouse. The drawing function seems to work fine however after you draw for a bit the program stops and calls a RecursionError. from turtle import * import tkinter as tk box=tk.Tk() canv=ScrolledCanvas(box) canv.pack() screen=TurtleScreen(canv) p=RawTurtle(screen) p.speed(0) def draw(event): p.goto(event.x-256,185-event.y) #The numbers are here because otherwise the turtle draws off center.