Vector requires all arguments as python floats and/or ints. I've found common idiom to their usage to be:ĭistance = tkMatho.tkDistance( initialx, initialy, event.x, event.y )ĭistanceInInches = tkMath.pixelsToInches( distance ) The result is a python float in the same units as was entered. All four arguments must be in python float or integer values, all in the same units (pixels, inches, cm or mm). TkDistance is a simple Pythagorean Theorem implementation. I prefer to call setup immediately after I create my root widget so I called my parameter 'root'.Įach of the six conversion functions requires its argument as a python float or integer value. setup() will use the widget to determine the conversion constants. Before any method in this class is called, it's necessary to call setup() and pass it a widget. sin ( direction ) return x + deltaX, y + deltaYĬlearly I didn't need to define a class since all the methods are static. cos ( direction ) deltaY = distance * math. This function returns the x and y of the end-point.''' import math deltaX = distance * math. Given a x,y point, direction in radians (0 is at 3 o'clock, 1/2 PI is straight down, etc.) and a distance (all as python float or int). pow ( deltaY, 2 ) ) return direction, distance def tkCartesian ( x, y, direction, distance ): '''Complementary to tkPolar(). atan2 ( deltaY, deltaX ) distance = math. The result is in the same units as the arguments.''' import math deltaX = math. All arguments should be in the same units (python float or int). coordToPixels ( coord2 ) def tkPolar ( x1, y1, x2, y2 ): '''Calculate the direction (in radians, 3 o'clock is 0, down is 1/2 PI, etc.) and distance (in pixels) between to points. coordToPixels ( coord2 ) def sub ( coord1, coord2 ): '''Subtract two tk measures - they need not be in the same units.''' return tkMath. coordToPixels ( coord2 ) def add ( coord1, coord2 ): '''Add two tk measures - they need not be in the same units.''' return tkMath. pointsToPixels ( float ( tkCoord ) ) else : return float ( tkCoord ) else : return tkCoord def compare ( coord1, coord2 ): '''Compare two tk measures - they need not be in the same units.''' return tkMath. mmToPixels ( float ( tkCoord ) ) elif tkCoord = 'p' : return tkMath. cmToPixels ( float ( tkCoord ) ) elif tkCoord = 'm' : return tkMath. inchesToPixels ( float ( tkCoord ) ) elif tkCoord = 'c' : return tkMath. Convert a tkCoord (string appended by 'i', 'c', 'm' or 'p') to pixels.''' if isinstance ( tkCoord, str ): if tkCoord = 'i' : return tkMath. PIXELS_PER_POINTS def toPixels ( tkCoord ): '''Convenience function for inches, cm, mm and pointsToPixels(). PIXELS_PER_MM def pointsToPixels ( points ): '''Convert points (python float or int) to pixels.''' return points * tkMath. PIXELS_PER_CM def mmToPixels ( mm ): '''Convert millimeters (python float or int) to pixels.''' return mm * tkMath. PIXELS_PER_INCH def cmToPixels ( cm ): '''Convert centimeters (python float or int) to pixels.''' return cm * tkMath. PIXELS_PER_POINT def inchesToPixels ( inches ): '''Convert inches (python float or int) to pixels.''' return inches * tkMath. PIXELS_PER_MM def pixelsToPoints ( pixels ): '''Convert pixels (python float or int) to points.''' return pixels / tkMath. PIXELS_PER_CM def pixelsToMM ( pixels ): '''Convert pixels (python float or int) to millimeters.''' return pixels / tkMath. PIXELS_PER_INCH def pixelsToCM ( pixels ): '''Convert pixels (python float or int) to centimeters.''' return pixels / tkMath. winfo_fpixels ( '1p' ) def pixelsToInches ( pixels ): '''Convert pixels (python float or int) to inches.''' return pixels / tkMath. Class tkMath ( object ): PIXELS_PER_INCH = 0 PIXELS_PER_CM = 0 PIXELS_PER_MM = 0 PIXELS_PER_POINT = 0 def setup ( root ): '''Must be called before any of the methods are used to initialize the conversion constants.''' tkMath.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |