Как правильно отформатировать отображаемые данные в текстовом поле с помощью tkinter? - PullRequest
0 голосов
/ 16 марта 2020

Я пытаюсь сделать так, чтобы кнопка «Добавить следующий набор» отображала введенные данные в текстовом поле. Как например:

display_example

Следующим шагом будет применение данных из каждой предыдущей записи и вставка данных в текстовое поле в этом идеальном формате. Как показано в примере.

display_example2

Какие-либо советы по форматированию данных в текстовом поле?

Вот некоторый код для контекста:

def insert_into_textbox(): #generates input fields for the next set of items

    #get the inputs
    handling_unit = e3.get()
    pieces = e4.get()
    description = e5.get()
    length = e6.get()
    width = e7.get()
    height = e8.get()
    weight = e9.get()
    classification = e10.get()

    textbox_display = (handling_unit+"   "+pieces+"   "+description+"   "+length+"   "+width+"   "+height+"   "+weight+"   "+classification)
    textbox.insert("end",textbox_display)
    e3.delete(0, "end")
    e4.delete(0, "end")
    e5.delete(0, "end")
    e6.delete(0, "end")
    e7.delete(0, "end")
    e8.delete(0, "end")
    e9.delete(0, "end")
    e10.delete(0, "end")

e1 = Entry(master, borderwidth=5, width=12) #origin zip
e2 = Entry(master, borderwidth=5, width=12) #destination zip
e3 = Entry(master, borderwidth=5, width=12) #handling unit(s)
e4 = Entry(master, borderwidth=5, width=12) #piece(s)
e5 = Entry(master, borderwidth=5, width=13) #description(s)
e6 = Entry(master, borderwidth=5, width=12) #length(s)
e7 = Entry(master, borderwidth=5, width=12) #width(s)
e8 = Entry(master, borderwidth=5, width=12) #height(s)
e9 = Entry(master, borderwidth=5, width=12) #weight(s)
e10 = Entry(master, borderwidth=5, width=12) #class(s)

textbox=Text(master, width=9, borderwidth=5, height=7)
textbox.grid(row=5, column=1, columnspan=9, sticky="nsew")


b0 = Button(master, text = "Add Next Set", bg="white", fg="black", font=arial8, command=insert_into_textbox)

mainloop()

Вот вывод

ui4

Как правильно отформатировать дисплей в соответствии с идеальным примером на рисунке 2?

1 Ответ

0 голосов
/ 16 марта 2020

Ссылка, которую предоставил Генри Йик, была очень полезной. Я внес следующие изменения в код:

textbox_display = (handling_unit.ljust(10)+pieces.ljust(11)+description.ljust(11)+length.ljust(10)+width.ljust(11)+height.ljust(11)+weight.ljust(10)+classification+"\n")

Или, альтернативно,

textbox_display = "{:>12} {:>13} {:>14} {:>13} {:>13} {:>13} {:>13} {:>13}".format(handling_unit, pieces, description, length, width, height, weight, classification)

Получает следующий результат: enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...