#๐ how do I parse inputted text from curses with gather?
53 messages ยท Page 1 of 1 (latest)
@next lichen
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
@lapis igloo
!code
Please paste the code here directly.
Hey @next lichen!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
win = curses.newwin(19, 12, 20, 53)
box = Textbox(win)
rectangle(stdscr, 19, 52, 21, 75)
stdscr.refresh()
box.edit()
text = box.gather()
stdscr.addstr(10, 40, text)
stdscr.getch()
message = box.gather()
I don't see any imports etc. Is that complete?
it's sample code
You need to post working code, whatever its shortcomings.
import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None``` assuming this is the imports
So put it all together in a .py file and run it. Show the complete .py file, and a transcript of the run.
after pressing control g
If I run the code you just posted:
import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None
win = curses.newwin(19, 12, 20, 53)
box = Textbox(win)
rectangle(stdscr, 19, 52, 21, 75)
stdscr.refresh()
box.edit()
text = box.gather()
stdscr.addstr(10, 40, text)
stdscr.getch()
message = box.gather()
I get this:
[~/tmp/python-discord]fleet2*> py3 tofu.py
+ exec /Users/cameron/var/venv/3/bin/python tofu.py
File "/Users/cameron/tmp/python-discord/tofu.py", line 12
win = curses.newwin(19, 12, 20, 53)
IndentationError: unexpected indent
That looks nothing like your screenshot.
if you have any knowledge in python provide help
it doesn't seem like you do, no disrespect meant
@next lichen cannot be helped if they're not posting the same code which they're running.
https://mystb.in/547309fdbc2e685a02 @bronze vault
import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None
win = curses.newwin(19, 12, 20, 53)
box = Textbox(win)
rectangle(stdscr, 19, 52, 21, 75)
stdscr.refresh()
box.edit()
text = box.gather()
stdscr.addstr(10, 40, text)
stdscr.getch()
message = box.gather()
here you go
he provided the full code
import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None
def add_str(stdscr,string,y):
stdscr.addstr(y,(lim[1]-lim[1]//4),string)
stdscr.refresh()
def add_win(stdscr):
lim = stdscr.getmaxyx()
win = curses.newwin(lim[0] - 10, lim[1] - 10, 5, 5)
win.refresh()
stdscr.refresh()
def main(stdscr):
global lim
lim = stdscr.getmaxyx()
add_win(stdscr)
content=[*Path('').glob('*')] #lits the contents of ./test/*
curses.init_pair(1,curses.COLOR_BLUE,curses.COLOR_YELLOW)
BLUE_AND_YELLOW=curses.color_pair(1)
for i,item in enumerate(content):
add_str(stdscr,str(item),i+1)
win = curses.newwin(19, 12, 20, 53)
box = Textbox(win)
rectangle(stdscr, 19, 52, 21, 75)
stdscr.refresh()
box.edit()
text = box.gather()
stdscr.addstr(10, 40, text)
stdscr.getch()
message = box.gather()
print(message)
wrapper(main)
Maybe you should look at what they just posted to the pastebin. It does produce the screenshot they showed.
@lapis igloo here you go, you posted an indentation error, which isn't what they posted
If I run this gode and type foo then ^G, it does the read of foo and writes it via your addstr() call. Then the next getch() happens just fine. But then it quits immediately.
Possibly you need to make a new textbox. I've not used textbox.
for reading/parsing the text box?
Well it looks to me like this line:
message = box.gather()
returns immediately. Possibly it just fetches the current context of the text box? print out message, see what's in it.
Maybe you need to call box.edit() again if you expect to enter more text in that text box.
just one input in this text box
Ok, so you've got the contets in text. What did you want to do with it then?
parsing it, if I say I want to move to /usr/share from /var/www from input with chdir(path): os.chdir('input') content=[*Path('').glob('*')] #lits the contents of ./test/*
Parsing means examining the text and extracting information from it.
What did you intend to enter into the textbox?
It is meant to be the path in your suggestion?
Ok. So why not:
path = box.gather()
os.chdir(path)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/share/ \n'
It looks like your string includes a trailing space and a newline character. Did you type those?
You could go path = box.gather().strip() to trim those.
that works, but does not refresh content
The content is not magic. You need to reload it and redraw it.
Just as you originally drew it. (I assume you mean the listdir listing on the right?)
stdscr.refresh()
yes
That just redraws the terminal display. in case is has been mangled.
It doesn't for example reread the directory list and show a new one.
You should probably run a loop in the main() function. Initially set up the screen. Then a while true loop which:
- update the directory listing on the right
- prompts for a new directory
- chdir the new directory
then next time round the loop the directory listing just gets redrawn
ok
!close
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.