Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4091664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:23:43+00:00 2026-05-20T19:23:43+00:00

I was writing a little program that takes a list and generates a menu

  • 0

I was writing a little program that takes a list and generates a menu out of it in curses (straight up, standard library or whatever, batteries included python’s curses) when I noticed the strangest problem (if you’d like, a heavily commented copy of the entire program is below). Simply put, when accepting the results of an os.listdir generated list, curses crashes with an addstr ERR, BUT, if I feed it a hardcoded list, it works fine. This, of course, makes absolutely no sense, right? A list is a list is a list and a list by any other name should still be a list, right?

To make things even more complicated, I sent the code to a friend of mine who works mainly in python2.6 (mine was originally written to work in python3.1). He uncommented the broken_input() call (which feeds the program the os.listdir generated information) and said that it worked fine for him. I have both python 2.6 and 3.1 installed, so I changed my shebang to make the program run in 2.6, and (with the broken_input() uncommented) for me, it still throws the addstr ERR (yet runs fine with the hardcoded input… which is, of course, btw, entirely useless apart from proof of concept).

Thus, my question is this: is there something broken in my python installation (I’m running Ubuntu lucid, with python2.6.5 and 3.1 installed), and, if so, how do I fix it so I can get curses to execute this code properly. And, if it’s not my python installation, how can I get the same functionality out of curses (i.e.: paint a menu from a list containing an arbitrary number of items, numbering them so that the user can make a selection based on the item number).

#!/usr/bin/env python3.1
"""curses_mp3eater.py: a curses-based implementation of my mp3eater program;
diplays the contents of cwd, allows user to make a selection. But I'm having
problems getting it to iterate over a list.
v0.1 03.14.11
by skookie sprite
address@gmail.com
"""

import curses, curses.wrapper, os, sys


def working_input():
    """the following is demo code to demonstrate my problem... main will accept the following,
    but won't accept the product of a directorylist for reasons that I can't figure out."""
    dircontents=['this','is','a','list','','and','it','will','iterate','fine','in','the','(main) function.']
    return dircontents

def broken_input():
    """this is the code that I NEED to have work... but for reasons beyond me will not iterate in
    the main function. It's a simple list of the contents of the CWD."""
    cwd=os.getcwd()
    dircontents=[]
    for item in os.listdir(cwd):
        dircontents += [item]
    return dircontents

def main(stdscr):
    """This is the program. Designed to take a list of stuff and display it. If I can solve
    that hurdle, I'll add selection mechanisms, and break it across screens - amongst other
    things. But, currently, it can only accept the demo code. Uncomment one or the other to
    see what I mean."""
    #broken_input returns an addstr() ERR, but I don't see the difference between working_input
    #and broken_input as they are both just lists. 
    #working_input() is demo code that illustrates my problem
    stuffin=working_input()
    #stuffin=broken_input()

    #the rest of this stuff works. The problem is with the input. Why?
    linenumber=int()
    linenumber=6
    itemnumber=int()
    itemnumber=1

    stdscr.clear()
    stdscr.border(0)

    for item in stuffin:
        stdscr.addstr(linenumber, 10, '%s   -   %s' % (itemnumber, item), curses.A_NORMAL)
        linenumber += 1
        itemnumber += 1

    curses.doupdate()
    stdscr.getch()



if __name__ == '__main__':
    curses.wrapper(main)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:23:44+00:00Added an answer on May 20, 2026 at 7:23 pm

    You’re stuffing too much onto the screen and thus passing an out-of-bounds line number to addstr. If you make an empty directory to run the program in (or enlarge your terminal window), it works.

    To fix this, check the number of lines in the window before the output loop in main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total newbie, but I was writing a little program that worked on
Im writing a little python program that goes through an XML file and does
I'm writing a tiny program that takes user input using Getops, and based on
I'm looking for some information and a little history. I am writing a program
I'm writing a little desktop app that should be able to encrypt a data
I've been writing a little application that will let people upload & download files
I am currently writing a little bootstrap code for a service that can be
I'm learning C++ and writing little programs as I go along. The following is
I am writing a little application to download files over http (as, for example,
I'm writing a little debug app for a bit of kit we're developing and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.