如何使用 tkinter 和 python 更改文本框的大小,使其在 UI 范围内水平扩展

How to change size of textbox so that it expands horizontally across the UI with tkinter and python

我正在设计一个 UI 并想实现一个文本框来显示输入的数据。例如,当在相应的输入字段中输入处理单元时,数据将显示并存储在文本框中。

我想要更改文本框的宽度,以便从第 1 列扩展到第 8 列。我尝试更改网格方法中的一些参数,但它会导致一些格式错误。

这里是上下文的一些代码:

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)

e1.grid(row = 0, column = 2, pady = 1, sticky="W")  #origin zip
e2.grid(row = 0, column = 4, pady = 1, sticky="W") #destination zip
e3.grid(row = 3, column = 1, pady = 1, sticky="W") #handling unit(s)
e4.grid(row = 3, column = 2, pady = 1, sticky="W") #piece(s)
e5.grid(row = 3, column = 3, pady = 1, sticky="W")  #description(s)
e6.grid(row = 3, column = 4, pady = 1, sticky="W")  #length(s)
e7.grid(row = 3, column = 5, pady = 1, sticky="W") #width(s)
e8.grid(row = 3, column = 6, pady = 1, sticky="W") #height(s)
e9.grid(row = 3, column = 7, pady = 1, sticky="W") #weight(s)
e10.grid(row = 3, column = 8, pady = 1, sticky="W") #class(s)

textbox=Text(master, width=9, borderwidth=5, height=7)
textbox.grid(row=5, column=0, columnspan=3)

scrollbar=Scrollbar(master, orient="vertical", command=textbox.yview)
scrollbar.grid(row=5, column=9, sticky=NS)
textbox.configure(yscrollcommand=scrollbar.set)

I want the change the width of the textbox so that is expands from column 1 to column 8.

如果是这种情况,您需要告诉它跨越八列,而不是三列。您还需要使用 sticky 属性,以便文本小部件扩展以填充已分配的 space。

textbox.grid(row=5, column=0, columnspan=8, sticky="nsew")