【问题标题】:Tkinter entry get unicode content errorTkinter 条目获取 unicode 内容错误
【发布时间】:2018-04-20 09:26:25
【问题描述】:

我在尝试从 Tkinter 条目中获取一些值时遇到了一个奇怪的问题。基本上,如果在条目中我有一个无 ascii 值并使用 get() 内容,有时我有一个正确的 unicode 字符串,有时我有一个没有规范 'u' 的 ascii 字符串 a-la unicode。这是我的代码:

def create():
    try:
        cols = []
        values = []                
        for name in names:
            if (pk <> name):
                if (name in fk_columns):
                    cols.append(name)
                    values.append(box[name].get())
                else:                        
                    cols.append(name)
                    values.append(entry[name].get())                        
        print values                        
        dbutils.create(mDBname, mTable,cols,values)
        frame.master.destroy()
        tkMessageBox.showinfo("New record", "Record created") 
        scrolled_view(root,mDBname,mTable,'g',0, editable)           
    except Exception, err:
        tkMessageBox.showerror("Error", err)           

def update():
    try:
        cols = []
        new_values = []
        old_values = []
        i = 0
        for name in names:
            if (name in fk_columns):
                cols.append(name)
                new_values.append(box[name].get())
            else:                        
                cols.append(name)                    
                new_values.append(entry[name].get())                                       
            if rows[record][i] == None:
                old_values.append('')
            else:
                old_values.append(rows[record][i]) 
            i = i + 1
        print new_values 
        dbutils.update(mDBname, mTable, cols, new_values, old_values)
        frame.master.destroy()
        tkMessageBox.showinfo("Update", "Record updated")
        scrolled_view(root,mDBname,mTable,'g',0, editable)            
    except Exception, err:
        tkMessageBox.showerror("Error", err) 

所以问题是,如果我使用“create”函数并且我有一个值为“John Canà”的条目,当我在 shell 中打印值时(请注意调用 dbutils 之前的“打印值” ) 我得到了:

[u'John Can\xe0', 'Amministrazione'] 

并且该功能正常工作。

当我使用更新功能时,即使只更新第二个值而不是 python shell 上带有“John Canà”的值(在调用 dbutils 之前调用 print new_values)我得到了:

['17', 'John Can\xe0', 'Marketing']

字符串 'John Can\xe0' 没有 'u'。这最终会产生经典错误“序数不在 128 范围内”。所以我的问题是为什么同一个 Entry.get() 调用会产生这种不同的行为以及如何解决它。提前致谢!

【问题讨论】:

    标签: python tkinter unicode-string tkinter-entry


    【解决方案1】:

    我不确切知道它是否是您正在寻找的东西,我正在向您展示迄今为止测试的行为:

    >>> name = u'John Can\xe0'
    >>> name = bytes(name,"unicode_escape")
    >>> name.decode('unicode_escape')
    'John Canà' # This is the output
    

    所以为了避免在您的字符串中出现\xe0,请在显示任何字符串之前进行转换

    【讨论】:

    • 感谢 Lemayzeur,但这无济于事。基本上我从 db 得到的是 name = 'John Can\xe0' 这就像一个没有 u 前缀的 unicode,我不知道如何处理它。
    • 即使没有前缀 u 就像你说的那样,它也可以工作。您使用\xe0 将数据存储在数据库中,并在bytes(name,"unicode_escape").decode('unicode_escape') 之后显示它们。它将显示à
    【解决方案2】:

    问题是由于 sqlalchemy 原始查询:当我创建记录(插入)时,条目正确地将文本处理为 unicode(无纯字符串):

    u'John Can\xe0'
    

    基本上更新时,我会进行原始选择(通过 sqlalchemy)来填充条目和值

    'John Can\xe0' 
    

    来自原始选择。添加

    '?charset=utf8' 
    

    到连接字符串解决了这个问题。 唯一不明白的是为什么 Tkinter Entry 正确处理 'John Can\xe0' 而没有引发错误。顺便说一句,现在代码可以工作了!

    【讨论】:

      猜你喜欢
      • 2022-12-08
      • 2012-03-03
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2021-06-29
      • 1970-01-01
      相关资源
      最近更新 更多