编程语言
首页 > 编程语言> > python – 如何在PyCharm中使用curses?

python – 如何在PyCharm中使用curses?

作者:互联网

因此,我浏览了互联网最深处,似乎无法找到解决我的问题的方法.我在MacOS 10.12.4上使用PyCharm Community Edition 2017.1.1,并在Python 3.6.1中使用以下代码中的curses.

#!/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>')

我知道PyCharm使用自己的终端来显示输出,我似乎无法弄清楚如何配置它以便我的程序在PyCharm中运行.我可以在Mac的终端上成功运行我的程序,但是我希望能够在PyCharm中运行它.

我谦卑地转向你,互联网上的陌生人,为你提供帮助.如果我想在PyCharm中做什么,请告诉我,如果可以的话,如何做到这一点.

我在PyCharm的终端窗口中收到以下错误:

)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

解决方法:

我通过配置运行首选项解决了这个问题:

>点击菜单运行然后编辑配置…
>选择与项目对应的配置
>选中输出控制台中的Emulate终端复选框

标签:macos-sierra,python,macos,pycharm
来源: https://codeday.me/bug/20190828/1752610.html