问题描述
限时送ChatGPT账号..在我正在处理的项目中,您可以编辑以树状视图形式显示的数据内容.数据字段之一是始终以 0 开头的电话号码.
In a project that I am working on, you can edit the contents of the data shown in treeview form. One of the fields of data is a telephone number which always starts with a 0.
在表中选择正确的记录并使用 tree.item(tree.selection())
从中提取数据时.出于某种原因,电话号码会自动转换为整数,因此当它们在框中显示给用户时,它们会丢失开头的 0.
When selecting the correct record in the table and having the data pulled from it using tree.item(tree.selection())
. For some reason, the telephone numbers are automatically converted to integers and so they lose the 0 at the start of them when they are displayed to the user in the box.
有什么办法可以解决这个问题吗?
Is there any way to fix this?
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()
推荐答案
通过将数据从 values
复制到 text
(向树添加新项目时) 和稍后从 text
(而不是 values
)读取,将允许克服这个限制.
By duplicating data from values
into text
(when adding new item to the tree) and later reading from text
(instead of values
), would allow to overcome this limitation.
from tkinter import Tk
from tkinter import Frame
from tkinter import Button
import tkinter.ttk as ttk
# print(TkVersion)
# 8.6
def edit(tree):
if len(tree.selection()) == 1:
selected_item = tree.item(tree.selection())
#print(selected_item['values'][0])
print(selected_item['text'])
else:
print('please select item')
gui = Tk()
container = Frame(gui)
container.pack(expand=True, fill='both')
edit_button = Button(gui, text='Edit', command=lambda: edit(tree))
edit_button.pack(fill='x')
tree = ttk.Treeview(container, show='headings')
col_names = ['Contact No.']
tree['columns'] = col_names
for col_name in col_names:
tree.heading(col_name, text=col_name)
phone_nums = ['01234567895']
for phone_num in phone_nums:
tree.insert('', 'end', text=phone_num, values=phone_num)
tree.pack(expand=True, fill='both')
gui.mainloop()
这篇关于从树视图中选择自动将字符串数字转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论