【问题标题】:Is it possible to add mulitple entries to one line in a listbox?是否可以将多个条目添加到列表框中的一行?
【发布时间】:2019-04-16 11:39:31
【问题描述】:

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

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

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 处的插入,所以它们彼此位于下方

【问题讨论】:

    标签: python tkinter listbox


    【解决方案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)
    

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

    【讨论】:

    • 感谢您的帮助,我只需要确定它们之间的距离
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多