如何让插入tkinter入口小部件中的数字在tkinter treeview中覆盖默认值

时间:2018-02-28 11:28:14

标签: python tkinter

我在下面有一些代码,可以将entry窗口小部件中输入的数字插入tkinter treeview tree2 tree1,从month中选择以覆盖默认数字。能够切片以插入figure,但当我将treeview插入所选column1内容之下且column2而不是2018时。如果未输入数字,则默认值应为from tkinter import ttk import tkinter as tk blow = [("january", "2013"),("february", "2014"),("march", "2015"),("april", "2016"),("may", "2017")] def append_select(): cur_id = tree.focus() data = 2018 data = e1_sub.get() # if none is provided the default should 2018 if cur_id: tree2.insert("", tk.END, values=data) #tree2.insert("", tk.END, values=tree2.column("#2")[data]) tree2.insert("", tk.END, values=tree.item(cur_id)['values'][:1]) root = tk.Tk() root.geometry("500x500") tree = ttk.Treeview(columns=("columns1", "columns"), show="headings", selectmode="browse") tree.heading("#1", text="Month") tree.heading("#2", text="Year") for n in blow: tree.insert("", tk.END, values=(n)) tree.pack() e1_sub = tk.StringVar() e1 = tk.Entry(root, textvariable=e1_sub) e1.pack() b1 = tk.Button(text="append", command=append_select) b1.pack() tree2 = ttk.Treeview(columns=("Month", "Year"), show="headings") tree2.heading("#1", text="First name") tree2.heading("#2", text="Surname") tree2.pack() root.mainloop()

我感谢您提出的实现

的建议
Publicly Accessed

1 个答案:

答案 0 :(得分:0)

我认为你想要做的是:

def append_select():
    cur_id = tree.focus()

    data = e1_sub.get()  # if none is provided the default should 2018
    if not data:
        data = 2018

    if cur_id:
        month = tree.item(cur_id)['values'][0]
        tree2.insert("", tk.END, values=(month, data))

基本上它首先检查条目是否为空,如果是,则为2018,然后它只是写入。可能结构更好。