[英]How can I use curses with PyCharm?
所以我瀏覽了互聯網的最深處,似乎無法找到解決我的問題的方法。 我在 MacOS 10.12.4 上使用 PyCharm Community Edition 2017.1.1,並在以下代碼中使用 Curses 和 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>')
我知道 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
我通過配置運行首選項解決了這個問題:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.