0
import curses

class TextEditor:
    def __init__(self):
        self.screen = curses.initscr()
        curses.noecho()
        curses.cbreak()
        self.screen.keypad(True)
        self.buffer = ""
        self.row = 0
        self.col = 0
        self.mode = "command"

    def run(self):
        while True:
            self.update()
            self.draw()

    def handle_input(self, key):
        if self.mode == "command":
            if key == ord("i"):
                self.mode = "insert"
            elif key == curses.KEY_LEFT:
                self.move_left()
            elif key == curses.KEY_RIGHT:
                self.move_right()
            elif key == curses.KEY_UP:
                self.move_up()
            elif key == curses.KEY_DOWN:
                self.move_down()
        elif self.mode == "insert":
            if key == ord("\n"):
                self.buffer += "\n"
                self.row += 1
                self.col = 0
            else:
                self.buffer += chr(key)
                self.col += 1

    def move_left(self):
        if self.col > 0:
            self.col -= 1

    def move_right(self):
        if self.col < len(self.buffer):
            self.col += 1

    def move_up(self):
        if self.row > 0:
            self.row -= 1

    def move_down(self):
        if self.row < self.num_rows() - 1:
            self.row += 1

    def num_rows(self):
        return self.buffer.count("\n") + 1

    def update(self):
        key = self.screen.getch()
        self.handle_input(key)

    def draw(self):
        self.screen.clear()
        if self.mode == "command":
            self.screen.addstr(self.row, self.col, self.buffer)
        elif self.mode == "insert":
            self.screen.addstr(self.row, self.col, self.buffer)
        self.screen.refresh()

    def exit(self):
        curses.nocbreak()
        self.screen.keypad(False)
        curses.echo()
        curses.endwin()

When I run this code, I get this error:

cts/IDE/IDE.py
Traceback (most recent call last):
  File "c:\Users\songk\Desktop\code\python\projects\IDE\IDE.py", line 1, in <module>
    import curses
  File "C:\Users\songk\AppData\Local\Programs\Python\Python311\Lib\curses\__init__.py", line 13, in <module>
    from _curses import *
ModuleNotFoundError: No module named '_curses'

Could you help me please?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Songkail
  • 27
  • 2
  • 1
    Try installing the Windows-specific curses package. `pip install windows-curses` – John Gordon Apr 16 '23 at 01:26
  • I just edited the question to put the actual error in the title. "Traceback (most recent call last)" just means "This is where the error occurred, in order". I guess you're new to Python, eh? Anyway, if you google the error message, you get results that look useful (though I've never used this library myself), like this: [ModuleNotFoundError: when importing curses in IDE](/q/72433839/4518341). BTW, welcome to Stack Overflow! Please take the [tour] and read [ask] for tips like how to write a good title. – wjandrea Apr 16 '23 at 01:48

1 Answers1

0

try installing "curses" again

  1. python.exe -m pip install --upgrade pip
  2. pip install windows-curses
wjandrea
  • 28,235
  • 9
  • 60
  • 81