How can I use curses with PyCharm?

2019-05-17 20:26发布

So I've looked across the deepest reaches of the Internet and cannot seem to find a solution to my issue. I'm using PyCharm Community Edition 2017.1.1 on MacOS 10.12.4 and using curses in the following code with Python 3.6.1.

#!/usr/local/bin/python3
import sys
import urllib.request
import json
import time
from datetime import datetime
from datetime import timezone
import curses
import ssl

class Stock:
    def stockFromYahooWebService(ticker):
        url = "https://finance.yahoo.com/webservice/v1/symbols/{}/quote?format=json&view=detail".format(ticker.upper())
        user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1"

        urlrequest = urllib.request.Request(url, data=None, headers={"User-Agent": user_agent})

        ssl._create_default_https_context = ssl._create_unverified_context

        urlcontent = json.loads(urllib.request.urlopen(urlrequest).read().decode())
        fields = urlcontent['list']['resources'][0]['resource']['fields']    
        return fields
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1) 
try: 
    if len(sys.argv) == 2:
        ticker = sys.argv[1]
        while ('true') :
            stdscr.erase()
            fields = Stock.stockFromYahooWebService(ticker) 
            utctime = datetime.strptime(fields['utctime'], "%Y-%m-%dT%H:%M:%S+%f")
            localtime = utctime.replace(tzinfo=timezone.utc).astimezone(tz=None)
            localtimeString = localtime.strftime("%I:%M%p %Z %m/%d/%y")
            stdscr.addstr(0, 0, "{} ({})".format(fields['issuer_name'], fields['symbol']))
            stdscr.addstr(1, 0, "Price: ${:,.2f}".format(float(fields['price'])))
            stdscr.addstr(2, 0, "Change: {:+,.2f} ({:+,.2f}%)".format(float(fields['change']),float( fields['chg_percent'])))
            stdscr.addstr(3, 0, "Volume: {:,}".format(int(fields['volume'])))
            stdscr.addstr(4, 0, "Last Trade: {}".format(localtimeString))
            stdscr.refresh()
            time.sleep(5)
finally:
    curses.nocbreak()
    stdscr.keypad(0)
    curses.echo()
    curses.endwin()
    if len(sys.argv) != 2 : 
        print('Usage: ./rtquote.py <ticker>')

I know that PyCharm uses its own terminal to display the output, and I can't seem to figure out how to configure it so that my program runs within PyCharm. I can successfully run my program in Mac's terminal, however I want to be able to do it in inside PyCharm.

I am humbly turning to you, the kind strangers of the Internet, for your help. Please let me know if what I'm trying to do is even possible in PyCharm, and if so, how can this be done.

I get the following error in PyCharm's terminal window:

)07[?47h[1;24r[m[4lTraceback (most recent call last):
  File "/rtquote.py", line 24, in <module>
    curses.cbreak()
_curses.error: cbreak() returned ERR

Process finished with exit code 1

1条回答
劳资没心,怎么记你
2楼-- · 2019-05-17 20:54

I solved this problem by configuring the running preferences :

  • hit menu Run then Edit configurations...
  • select the configuration corresponding to your project
  • check the checkbox Emulate terminal in output console
查看更多
登录 后发表回答