簡體   English   中英

詛咒window.getstr()

[英]Curses window.getstr()

我正在嘗試學習Windows XP上Python的詛咒。 我可以使window.getkey命令正常工作,但是命令window.getstr不僅失敗,而且程序退出。 以下是示例代碼行:

x = window.getkey()  # this works
y = window.getstr()  # this fails

顯然, stdscr = curses.initscr()第一行工作,我已經正確導入了curses並執行了stdscr = curses.initscr()命令。 我的窗口已定義並且可以正常工作。 我嘗試將窗口坐標放在getstr括號內,並且使用了window.move。 都不行。

有什么想法為什么getstr無法正常工作?

以下是第一個建議的更多信息:

首先,建議在命令提示符窗口而不是從桌面運行程序,因為該窗口確實消失了。

這是整個程序:

# program = testcurses
import curses
stdscr = curses.initscr()

window = stdscr.subwin(23,79,0,0)
window.box()
window.refresh()
window.addstr(2,10, "This is my line of text")
window.addstr(4,20,"What happened? ")
window.refresh()

mykey = window.getkey(5,20)
mystr = window.getstr (6,20)
#window.addstr (7,1, "Key data should be here: ")
#window.addstr (7,33, mykey)
window.addstr (8,1, "Str data should be here: ")
window.addstr (8,33,mystr)
window.refresh()

我記下了與關鍵數據顯示有關的行,因為那行得通。

這是“回溯”消息的相關部分:

window.addstr(8,33,mystr)TypeError:必須為str,而不是字節。

湯姆

追溯告訴您,變量mystr是字節對象而不是字符串。 這意味着您必須先對其進行解碼,然后才能將其用作字符串,這是addstr()所需的。

這是您需要進行的更改:

mystr = window.getstr(6,20).decode(encoding="utf-8")

這僅是Python 3問題! 我也使用Python 2.7對此進行了測試,無需更改即可使用。 由於Python 2和3之間的字節/字符串處理方式不同,導致出現問題。我假設您自己使用py3時遵循了py2教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM