【发布时间】:2017-07-30 19:55:11
【问题描述】:
在我正在处理的项目中,您可以编辑以树形视图形式显示的数据内容。数据字段之一是始终以 0 开头的电话号码。
在表中选择正确的记录并使用tree.item(tree.selection()) 从中提取数据时。由于某种原因,电话号码会自动转换为整数,因此当它们在框中显示给用户时,它们会丢失开头的 0。
有没有办法解决这个问题?
from tkinter import *
import tkinter.ttk as ttk
def fnEdit(tree):
items = tree.item(tree.selection())
print(items['values'][0])
myGui = Tk()
container = Frame(myGui)
container.pack(expand=True, fill="both")
editButton = Button(myGui, text='Edit', command=lambda: fnEdit(tree))
editButton.pack(fill="x")
table_header = ['Contact No.']
tree = ttk.Treeview(container, columns=table_header, show="headings")
tree.column(table_header[0])
tree.insert('', 'end', values='01234567895')
tree.pack(expand=True, fill="both")
myGui.mainloop()
【问题讨论】:
-
我不确定为什么会发生这种情况(但似乎每次字符串也可以解释为数字时都会发生),但是您有一个笨拙的解决方法:您可以将返回的整数转换为字符串并在前面加上必要的零。
-
@nbro 这就是我认为我可以绕过它的方式。不太确定考试委员会会怎么想,但这可能是我唯一的选择。
-
这似乎与 stackoverflow.com/q/39067164/7432 重复。不幸的是,这似乎是 ttk 中的一个错误。
标签: python string tkinter treeview selection