6
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import curses 

screen = curses.initscr() 
curses.noecho() 
curses.curs_set(0) 
screen.keypad(1) 
curses.mousemask(1)

screen.addstr("This is a Sample Curses Script\n\n") 

while True: 
   event = screen.getch() 
   if event == ord("q"): break 
   if event == curses.KEY_MOUSE: screen.addstr(curses.getmouse()) 

curses.endwin()

if event == curses.KEY_MOUSE: screen.addstr(curses.getmouse()) I think I should get the text where mouse is clicked or not? All I get is TypeError: str. Why is that? What am I missing? I couldn't find any good tutorials on this topic. Thanks.

good_evening
  • 21,085
  • 65
  • 193
  • 298
  • 1
    I think `curses.getmouse()` returns a [tuple](http://docs.python.org/library/curses.html#curses.getmouse). – RanRag Feb 13 '12 at 02:00
  • @RanRag: What that tuple means? It gives tuple of what? – good_evening Feb 13 '12 at 02:07
  • See the [docs](http://docs.python.org/library/curses.html#curses.getmouse) for the contents of that tuple. Coordinates x,y are in it, but no text. – Irfy Feb 13 '12 at 02:34

1 Answers1

11
import curses 

screen = curses.initscr() 
#curses.noecho() 
curses.curs_set(0) 
screen.keypad(1) 
curses.mousemask(1)

screen.addstr("This is a Sample Curses Script\n\n") 

while True:
    event = screen.getch() 
    if event == ord("q"): break 
    if event == curses.KEY_MOUSE:
        _, mx, my, _, _ = curses.getmouse()
        y, x = screen.getyx()
        screen.addstr(y, x, screen.instr(my, mx, 5))

curses.endwin()

You should read the docs more carefully, it's all in there :-)

Irfy
  • 9,323
  • 1
  • 45
  • 67
  • 1
    Great, +1, it works, but all I get now it's coordinates. Is it possible that I could get words instead of coordinates? – good_evening Feb 13 '12 at 01:59
  • what are the 3 underscores?? – Tcll Aug 01 '15 at 06:43
  • 3
    `_` is just a single-character variable name, a valid variable name in most languages. In some languages that support multiple return values from a function and/or tuple unpacking, the variable named `_` is used *by convention* for elements you don't care about. So in my code, I don't care about the values of the first, fourth and fifth return tuple elements. I could just as well have written `a, mx, my, b, c = ...` and never used the variables `a`, `b` and `c`, but using `_` conveys the intent of not caring, explicitly. – Irfy Aug 17 '15 at 16:01
  • It returns (id, x, y, z, bstate). See https://github.com/python/cpython/blob/0be7c216e16f0d459f1c8f6209734c9b2b82fbd4/Modules/_cursesmodule.c#L2956 – maarten Aug 19 '20 at 20:18
  • it is not work for window – IsaacMak Sep 02 '22 at 12:14