Python 如何将数据从条目小部件导出到 Treeview
Python How to export data from entry widget to Treeview
我想做的是使用 Treeview
小部件制作一个列表框。列表框已成功创建,但我不明白如何将数据从条目小部件导出到列表框,我需要删除列表框内容的按钮。该程序运行成功。
from tkinter import ttk
import tkinter as tk
def update_sum(first_number_tk, second_number_tk, sum_tk) :
# Sets the sum of values of e1 and e2 as val of e3
try:
sum_tk.set((float(first_number_tk.get().replace(' ', '')) + float(second_number_tk.get().replace(' ', ''))))
except :
pass
root.after(10, update_sum, first_number_tk, second_number_tk, sum_tk) # reschedule the event
return
root = tk.Tk()
root.geometry('1000x600')
e1_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.
# Entries
e1 = tk.Entry(root, textvariable = e1_tk)
e1.grid(row=1,column=1)
e2 = tk.Entry(root, textvariable = e2_tk)
e2.grid(row=1,column=2)
e3 = tk.Entry(root, textvariable = sum_tk)
e3.grid(row=1,column=3)
e4=tk.Label(root,text="SL")
e4.grid(row=1,column=0)
e3_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e4_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.
# Entries
e5 = tk.Entry(root, textvariable = e3_tk)
e5.grid(row=2,column=1)
e6 = tk.Entry(root, textvariable = e4_tk)
e6.grid(row=2,column=2)
e7 = tk.Entry(root, textvariable = sum2_tk)
e7.grid(row=2,column=3)
e8=tk.Label(root,text="DR")
e8.grid(row=2,column=0)
cols = ('name', 'No1', 'No2', 'total sum')
listBox = ttk.Treeview(root, columns=cols, show='headings')
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=2)
listBox.place(x=10, y=300)
# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.
root.after(10, update_sum, e1_tk, e2_tk, sum_tk)
root.after(10, update_sum, e3_tk, e4_tk, sum2_tk)
root.mainloop()
提前致谢..
您可以只添加两个新按钮和两个新功能,例如:
b = tk.Button(root,text='Update Listbox',command=update)
b.grid(row=3)
b1 = tk.Button(root, text='Delete Items', command=delete)
b1.grid(row=4)
然后 update()
可能是这样的:
def update():
listBox.insert('','end',value=('SOME NAME', float(e1.get()),float(e2.get()),float(e3.get())))
listBox.insert('', 'end', value=('SOME NAME', float(e5.get()), float(e6.get()), float(e7.get())))
然后 delete()
是这样的:
def delete():
selected_item = listBox.selection()[0] # get selected item
listBox.delete(selected_item)
要使 delete()
正常工作,您必须单击要删除的项目,然后单击删除按钮
value
参数是您必须进行必要更改的唯一地方。
希望它消除了您的疑问,如果还有任何错误或疑问,请告诉我。
干杯
我想做的是使用 Treeview
小部件制作一个列表框。列表框已成功创建,但我不明白如何将数据从条目小部件导出到列表框,我需要删除列表框内容的按钮。该程序运行成功。
from tkinter import ttk
import tkinter as tk
def update_sum(first_number_tk, second_number_tk, sum_tk) :
# Sets the sum of values of e1 and e2 as val of e3
try:
sum_tk.set((float(first_number_tk.get().replace(' ', '')) + float(second_number_tk.get().replace(' ', ''))))
except :
pass
root.after(10, update_sum, first_number_tk, second_number_tk, sum_tk) # reschedule the event
return
root = tk.Tk()
root.geometry('1000x600')
e1_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.
# Entries
e1 = tk.Entry(root, textvariable = e1_tk)
e1.grid(row=1,column=1)
e2 = tk.Entry(root, textvariable = e2_tk)
e2.grid(row=1,column=2)
e3 = tk.Entry(root, textvariable = sum_tk)
e3.grid(row=1,column=3)
e4=tk.Label(root,text="SL")
e4.grid(row=1,column=0)
e3_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e4_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.
# Entries
e5 = tk.Entry(root, textvariable = e3_tk)
e5.grid(row=2,column=1)
e6 = tk.Entry(root, textvariable = e4_tk)
e6.grid(row=2,column=2)
e7 = tk.Entry(root, textvariable = sum2_tk)
e7.grid(row=2,column=3)
e8=tk.Label(root,text="DR")
e8.grid(row=2,column=0)
cols = ('name', 'No1', 'No2', 'total sum')
listBox = ttk.Treeview(root, columns=cols, show='headings')
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=2)
listBox.place(x=10, y=300)
# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.
root.after(10, update_sum, e1_tk, e2_tk, sum_tk)
root.after(10, update_sum, e3_tk, e4_tk, sum2_tk)
root.mainloop()
提前致谢..
您可以只添加两个新按钮和两个新功能,例如:
b = tk.Button(root,text='Update Listbox',command=update)
b.grid(row=3)
b1 = tk.Button(root, text='Delete Items', command=delete)
b1.grid(row=4)
然后 update()
可能是这样的:
def update():
listBox.insert('','end',value=('SOME NAME', float(e1.get()),float(e2.get()),float(e3.get())))
listBox.insert('', 'end', value=('SOME NAME', float(e5.get()), float(e6.get()), float(e7.get())))
然后 delete()
是这样的:
def delete():
selected_item = listBox.selection()[0] # get selected item
listBox.delete(selected_item)
要使 delete()
正常工作,您必须单击要删除的项目,然后单击删除按钮
value
参数是您必须进行必要更改的唯一地方。
希望它消除了您的疑问,如果还有任何错误或疑问,请告诉我。
干杯