How we get caret position in x,y pixel to window. i tried following method but it only gives column and row for given position.
windowCaretPosition = self.__editor.PositionToXY(self.__editor.GetInsertionPoint())
it returns tuple with column and row. but i need x,y point show to context menu near caret when key up
When the user clicks on the position where you want to show the context menu, get x and y from the event:
def _OnClick(self, evt):
x, y = evt.GetX(), evt.GetY()
Related
I am trying to automate some work using python code and the function "pyautogui" but I need to find a way to detect colours where the mouse is on the screen.
Anyone has any solutions?
Thanking in advance.
import pyautogui
while True:
x, y = pyautogui.position()
px = pyautogui.pixel(x, y)
print(px)
You can call pyautogui.pixel(x, y) to obtain the (red, green, blue) value of the pixel where the mouse is. On macOS unfortunately, the mouse cursor itself is included in the screenshot. You will have to call x, y = pyautogui.position() to obtain the x, y coordinates, then call pyautogui.moveRel(50, 0) to move the mouse cursor away, then call pyautogui.pixel(x, y) to get the color of the pixel of where the mouse cursor was.
This will hopefully be corrected on macOS in a future version of PyAutoGUI.
Write a function called connectTheDots that takes in a list of tuples as its input and an optional color input as well. The default color value should be black. Each tuple is a coordinate pair (x, y) for a turtle. The function will have the turtle trace out a picture by starting at the first coordinate and then moving to each coordinate in turn.
Your function should do the following:
a. Create a turtle, setting the turtle’s color and speed appropriately
b. Check if the input list is empty: if it is empty then nothing else should happen!
c. Without leaving a line behind, move the turtle to the first location given in the list. Then start leaving a line again. Note: recall how to pull values out of a list, and also know that the goto method can take a single (x, y) tuple as its input: myTurtle.goto( (25, 25) ) will move myTurtle to x = 25 and y = 25.
d. After the turtle is at the starting coordinate, move it to each coordinate in the list in turn.
This is what I have been able to do so far:
def connectTheDots(list1, color ="black"):
myTurtle = turtle.Turtle()
myTurtle.speed(1)
myTurtle.goto(list1[0])
for x,y in list1[1:]: #I'm unsure if this is correct
myTurtle.goto(x,y)
You have most of what you need but are probably making it more complicated than needed and are missing some small details.
For step "a" you need to explicitly set the color (you passed it in just fine). You are probably better off using a symbolic speed instead of a numeric one.
For step "b", if you have a proper for ... in loop, you don't need to explicitly check if the list is empty as the loop won't run if it is. Your splitting off the first item myTurtle.goto(list1[0]) works against you here as there may not be one, causing an IndexError.
For step "c" you need to add another command. Turtles start life in the center of the screen with their pens down. You need to raise the pen up after creating your turtle. But you don't need to explicitly move to the starting position, let your loop handle that.
The trick we'll use for step "c" and step "d" is to put the pen down after the goto() in the loop. The first time, this actually puts the pen down, after that, it's a harmless no-op:
import turtle
def connectTheDots(coordinates, color="black"):
myTurtle = turtle.Turtle()
myTurtle.speed("slowest")
myTurtle.color(color)
myTurtle.penup()
for coordinate in coordinates:
myTurtle.goto(coordinate)
myTurtle.pendown() # redundant after first iteration
dots = ((34, 56), (100, 240), (230, 105), (34, 56))
connectTheDots(dots, "green")
turtle.done()
If it bothers you that we're putting the pen down unnecessarily in the loop, then we can replace myTurtle.pendown() with:
if not myTurtle.isdown():
myTurtle.pendown()
This relates to the "gm" extension for node, http://aheckmann.github.io/gm/docs.html
I need to add some text centered around a bounding box (horizontally is enough). The function drawText() requires x,y coordinates, but there is no way to draw centered text.
I would otherwise need a function which can return the width of a text string in the font/size given, so I can calculate my starting x position in javascript, before calling drawText().
You can use the region and gravity functions this way:
gm(filePath)
.region(WIDTH, HEIGHT, X, Y)
.gravity('Center')
.fill(color)
.fontSize(textFontSize)
.font(font)
.drawText(0, 0, 'This text will be centered inside the region')
I'm trying to get the XY coordinates of a moving sprite in SmileBASIC, and I can't figure it out. I have the single variable returned from SPCHK, but when I print it, I get a single number '4' constantly as the sprite moves. How do I get each bit?
From the documentation:
Return Values for SPCHK
|b00| XY-coordinates (1), #CHKXY
|b01| Z-coordinates (2), #CHKZ
|b02| UV-coordinates (4), #CHKUV
|b03| Definition number (8), #CHKI
|b04| Rotation (16), #CHKR
|b05| Magnification XY (32), #CHKS
|b06| Display color (64), #CHKC
|b07| Variable (128), #CHKV
For each bit, a target is assigned (If 0 is assigned for all bits, animation is being stopped)
SPCHK only tells you which properties are currently being animated, not their values.
To get the actual position, you can use SPOFS id OUT x,y
Example:
SPSET 0,17
SPANIM 0,"XY",-10,100,100
WAIT 5
SPOFS 0 OUT X,Y
?X,Y 'should be 50,50
Right now I have an x by y array to hold integers that decide which tile to draw to the screen. (The integers choose which tile in my tile_arr to blit)
For better performance, I only want the ints that changed to be blit'ed again.
EXAMPLE 1:
For example right now I have something like:
tile_arr = [image1,image2,image3,image4]
arr = [[2,2,2],[2,2,2],[2,2,2]]
Then depending on what the user does, some values in arr might change, so lets say:
arr[0][0]=1
arr[2][1]=1
Which would give us the array:
arr=[[1,2,2],[2,2,2],[2,1,2]]
now when blitting to the screen, I would blit images from the tile_arr: image numbers 1,2,2 to the top row, 2,2,2, to the middle row, and 2,1,2 to the bottom row. When I blit the array, I use a screen.blit for each value or arr, that's nine blits. I would rather only do two blits. (Use screen.blit only twice)
EXAMPLE 2:
tile_arr = [green.bmp, red.bemp, blue.bmp]
feild_arr = [[0,0,0], [0,0,0], [0,0,0]]
Output:
G G G
G G G
G G G
User changes feild_arr to [[1,0,1], [0,2,0], [0,1,2]]
Output:
R G R
G B G
G R B
Now I only want to call sceen.blit() 5 times, leaving the 4 Green sqaures green, because nothing changed.
I thought of making another array, which would be just a copy of the first. Then run through it and compare to the new array to see what changed, but I think there is a better and faster way to this. Now the example is only 3x3 so making a duplicate array isn't too bad, but I'm working with a lot bigger arrays, and when you're blitting a 30x20 array, I need all the shortcuts I can get.
How do I only blit when the interger values in an array have been changed, and skip (don't blit) the values that have not changed?
You can use screen.blit only once, calling with a list of the rectangles that changed.
I think the best aproach is to create you own class deriving from DirtySprite:
class Cell: pygame.sprite.DirtySprite
which already has attributes for holding an image and a rectangle and you can add an attributes to hold the number and a method to change de number that will set it as dirty.
Then you can use LayeredDirty class to render the dirty sprites on the screen.