简体   繁体   English

mintty终端,python,curses和utf8:显示不正确

[英]mintty terminal, python, curses, and utf8 : incorrect display

I've asked here but I'm pretty sure I won't get an answer. 我在这里过,但我很确定我不会得到答案。

  • copy - paste this python code : copy - 粘贴此python代码:
  • execute it 执行它
  • in a classical utf8 shell it work properly and displays "Coordonnées" but in my mintty terminal at home it displays "CoordonnM-CM-)es". 在经典的utf8外壳中,它可以正常工作并显示“Coordonnées”,但在我家的薄荷终端显示“CoordonnM-CM-”es。 Any idea how I could make it work, or where I should look (mintty has no character encoding options)? 知道我怎么能让它工作,或者我应该在哪里(mintty没有字符编码选项)?

code: 码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, time, random, copy, pprint
import termios, tty, fcntl
import thread, curses
import locale

def curses_main(stdscr, code):
    curses.curs_set(0)
    stdscr.addstr(2, 0, "Coordonnées")
    stdscr.refresh()
    # (1) = délai d'attente de 1/10 ème de seconde :
    curses.halfdelay(1)
    while True:
        try:
            c=stdscr.getch()
        except:
            c=-1
        if 0<c<256:
            if chr(c) in 'Qq':
                break
def main():
    locale.setlocale(locale.LC_ALL, '')
    code = locale.getpreferredencoding()
    curses.wrapper(curses_main, code)

if __name__ == '__main__':
    main()

Update 更新

Error: I thought found it: it's written everywhere to begin with that code: 错误:我以为找到了它:它随处可写以代码开头:

locale.setlocale(locale.LC_ALL, '')

so i tried with that and it worked: 所以我尝试了它,它工作:

locale.setlocale(locale.LC_ALL, 'fr_FR')

=> force it to French language =>强迫它使用法语

... it worked but only for the accents. ......它有效,但只适用于口音。 If i try to print line drawing chars: 如果我尝试打印线条图:

stdscr.addstr(20,20, "─ │ ┌ ┘ ┐ └ ├ ┤ ┬ ┴ ┼ ═ ║ ╔ ╝ ╗ ╚ ╠ ╣ ╦ ╩ ╬")

gives me: 给我:

M-b~T~@ M-b~T~B M-b~T~L M-b~T~X M-...

what is head banging is that vim displays properly those chars. 头撞的是vim 正确显示那些字符。 So it seems like it comes... from python? 所以它似乎来自...来自python? From my Windows/cygwin/mintty that works: 从我的Windows / cygwin / mintty工作:

env | grep -i lang
LANG=C.UTF-8

if I force encoding at home in my Windows/cygwin/mintty that doesn't work: 如果我在我的Windows / Cygwin的/ mintty强制编码在家工作:

locale.setlocale(locale.LC_ALL, 'C.UTF-8')

this doesn't work. 这不起作用。

Any idea where I could look? 知道我在哪里看? python seems to override properly the locale settings... the console is properly configured, and the fonts can display line drawing chars, because I see them with vim. python似乎正确覆盖了语言环境设置...控制台已正确配置,字体可以显示行绘图字符,因为我用vim看到它们。 So thinking more about it, it may be either a problem of python or a problem of curses... 所以更多地考虑它,它可能是python的问题或诅咒的问题......

Your locales preferred encoding and your tty's encoding doesn't have to be the same. 您的语言环境首选编码,您的tty编码不必相同。

Check what sys.stdin.encoding and sys.stdout.encoding is and try to use that. 检查sys.stdin.encoding和sys.stdout.encoding是什么,并尝试使用它。

Wild guesses is that the default encoding for mintty is UTF8, but your locales preferred encoding is (since it's windows) cp1252. 疯狂的猜测是,mintty的默认编码是UTF8,但你的locales首选编码是(因为它是windows)cp1252。

It's solved. 它已经解决了。 here's what I did: I re-installed cygwin then repacked my python program at work, a re-downloaded it then re-installed minnty 0.9.5.1 (mintty-0.9.5-cygwin17.zip) and it works now. 这就是我的所作所为:我重新安装了cygwin,然后在工作时重新打包我的python程序,重新下载然后重新安装了0.9.5.1(mintty-0.9.5-cygwin17.zip)并且它现在正常工作。 Strange. 奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM