是否可以将多个条目添加到列表框中的一行?

时间:2019-04-16 11:39:32

标签: python tkinter listbox

现在,由于堆栈溢出社区,您可以将数据添加到列表框中,我不确定是否可以将所有条目都放在列表框中的同一行上

我知道如果我使用一个输入框,所有内容都将在一行上

from tkinter import *
from tkinter import messagebox

def add_task():
    task = name_input.get()
    start_dte = start_date_input.get()
    due_dte = due_date_input.get()
    pri = pri_input.get()
    stat = status_input.get()
    # end

    listbox.insert(1, task)
    listbox.insert(1, start_dte)
    listbox.insert(1, due_dte)
    listbox.insert(1, pri)
    listbox.insert(1, stat)

def clear_all():
    listbox.delete(0,END)

root = Tk()
root.title("Todo list") # title of the application
    #root.geometry("900x400") # size of the application
titlelbl=Label(root, text = 'Welcome to your To-do-list', font='Times 30  bold').grid(row=0, column=5)#pack(),place(x = 25, y = 30)


name_input = StringVar()
start_date_input = StringVar()
due_date_input = StringVar()
pri_input = StringVar()
status_input = StringVar()

Label(root, text = "Name").grid(row = 6, column = 4)#pack()
name = Entry(root, textvariable = name_input)
name.grid(row = 7, column = 4)#pack()

Label(root, text = "Start date").grid(row = 6, column = 5)#pack()
start_date = Entry(root, textvariable = start_date_input)
start_date.grid(row = 7, column = 5)#pack()

Label(root, text = "Due date").grid(row = 6, column = 6)#pack()
due_date = Entry(root, textvariable = due_date_input).grid(row = 7, column = 6)#pack()

Label(root, text = "Priority").grid(row = 6, column = 7)#pack()
priority = Entry(root, textvariable = pri_input)
priority.grid(row = 7, column = 7)#pack()

Label(root, text = "Status").grid(row = 6, column = 8)#pack()
status = Entry(root, textvariable = status_input)
status.grid(row = 7, column = 8)#pack()

add_btn = Button(root, text = 'Add', width = 10, height = 3, command = add_task)
add_btn.grid(row = 9, column= 7)#pack()#place(x = 15, y = 50)

listbox = Listbox(root,font=('', 12), width = 60, height = 10)
listbox.grid(row = 3, column = 5)#pack()
listbox.insert(1, 'Name          Start date          Due Date       Priority        Status')

Button(root, text = 'Clear',width = 10, height = 3, command = clear_all).grid(row =9, column =10)

root.mainloop()

预期结果是用户将输入框中的数据作为一行输入到lisbox中。 相反,因为它们都以1作为插入点,所以它们彼此位于下面

1 个答案:

答案 0 :(得分:1)

实现此目标的一种方法是先将输入内容设为字符串,然后插入。

def add_task():
    task = name_input.get()
    start_dte = start_date_input.get()
    due_dte = due_date_input.get()
    pri = pri_input.get()
    stat = status_input.get()
    test_string = task+"          "+start_dte+"          "+due_dte+"          "+pri+"          "+stat
    listbox.insert(1,test_string)

就像您通过将其设为字符串在列表框中创建标题一样。