栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Unicode警告:Tkinter中的特殊字符

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unicode警告:Tkinter中的特殊字符

我可以通过这种方式重现错误:

% pythonPython 2.7.2+ (default, Oct  4 2011, 20:03:08) [GCC 4.6.1] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 'kxf8**e' in [u'kxf8**e']__main__:1: UnipreWarning: Unipre equal comparison failed to convert both arguments to Unipre - interpreting them as being unequalFalse

因此,也许

s
strobject
liste
ordliste
包含
unipre
,或者(如eryksun在评论中指出的)反之亦然。解决方案是解码
strobject
s(最有可能使用
utf-8
编解码器)以使其成为
unipre

如果这样做没有帮助,请打印并发布输出

print(repr(s))print(repr(liste))print(repr(ordliste))

我相信可以通过将所有字符串转换为来避免该问题

unipre

  1. ordliste
    从中生成时
    norsk.txt
    ,请使用
    precs.open('norsk.txt','r','utf-8')

    encoding = sys.stdin.encoding

    with precs.open(‘norsk.txt’,’r’,’utf-8’) as fil:
    ordliste = [line.rstrip(u’n’) for line in fil]

  2. 尽快将所有用户输入转换为unipre:

    def get_unipre(widget):streng = widget.get()try:    streng = streng.depre('utf-8')except UnipreEnpreError:    passreturn streng

所以也许试试这个:

import Tkinter as tkimport tkMessageBoximport precsimport itertoolsimport sysalfabetet = (u"abcdefghijklmnopqrstuvwxyz"  u"N{LATIN SMALL LETTER AE}"  u"N{LATIN SMALL LETTER O WITH STROKE}"  u"N{LATIN SMALL LETTER A WITH RING ABOVE}")encoding = sys.stdin.encodingwith precs.open('norsk.txt','r',encoding) as fil:    ordliste = set(line.rstrip(u'n') for line in fil)def get_unipre(widget):    streng = widget.get()    if isinstance(streng,str):        streng = streng.depre('latin-1')    return strengdef siord():    alfa=lagtabell()    try:        streng = get_unipre(ordinn)        ordene=finnord(streng,alfa)        if len(ordene) == 0: # There are no words that match tkMessageBox.showinfo('Dessverre..','Det er ingen ord som passer...')        else: # Done: The words that fit the pattern tkMessageBox.showinfo('Ferdig',     'Ordene som passer er:n'+ordene.enpre('utf-8'))    except Exception as err:        # There has been a mistake .. Check your word        print(repr(err))        tkMessageBox.showerror('ERROR','Det har skjedd en feil.. Sjekk ordet ditt.')def finnord(streng,alfa):     liste = set()    for substitution in itertools.permutations(alfa,streng.count(u'*')):        s = streng        for ch in substitution: s = s.replace(u'*',ch,1)        if s in ordliste: liste.add(s)    liste = [streng]+list(liste)    return u','.join(liste)+u'.'def lagtabell():    tinbox = get_unipre(bokstinn)    if not tinbox.isalpha():        alfa = alfabetet    else:        alfa = tinbox.lower()    return alfaroot = tk.Tk()root.title('FeudHjelper av Martin Skow Røed')root.geometry('400x250+450+200')# root.iconbitmap('data/ikon.ico')skrift1 = tk.Label(root,     text = '''Velkommen til FeudHjelper. Skriv inn de bokstavene du har, og erstatt ukjente med *.F. eks: sl**geDet er kun lov til å bruke tre stjerner, altså tre ukjente bokstaver.''',     font = ('Verdana',8), wraplength=350)skrift1.pack(pady = 5)ordinn = tk.StringVar(None)tekstboks = tk.Entry(root, textvariable = ordinn)tekstboks.pack(pady = 5)# What letters do you have? Eg "ahneki". Leave blank here if you want all the words.skrift2 = tk.Label(root, text = '''Hvilke bokstaver har du? F. eks "ahneki". La det være blankt her hvis du vil ha alle ordene.''',     font = ('Verdana',8), wraplength=350)skrift2.pack(pady = 10)bokstinn = tk.StringVar(None)tekstboks2 = tk.Entry(root, textvariable = bokstinn)tekstboks2.pack()knapp = tk.Button(text = 'Finn ord!', command = siord)knapp.pack(pady = 10)root.mainloop()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517000.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号