简体   繁体   English

如何设置Tkinter中额外填充的位置

[英]How to set where extra padding goes in tkinter

I have some Python tkinter code with a label that changes length and spans various columns. 我有一些带有标签的Python tkinter代码,该标签可以更改长度并跨越各个列。 As it changes length, the extra padding seems to be shared among the columns it spans, whereas I want it to be added to just one column. 当它改变长度时,多余的填充似乎在它跨越的各列之间共享,而我希望将其仅添加到一列。 As an example in the following simple interface, I'd like all the extra padding to be added to column 2 so that as the spinbox increases, the 'Move me' label is moved to the right, but the 'Keep me still' label stays where it is. 作为以下简单界面的示例,我希望将所有额外的填充添加到第2列,以便随着旋转框的增加,“ Move me”标签移到右侧,而“ Keep me still”标签移到右侧留在原地。 Please could someone show me how to make this work? 请有人能告诉我如何进行这项工作吗? Preferably without classes (yes, I am aware that I need to learn about them soon :P) 最好不要上课(是的,我知道我需要尽快了解它们:P)

from tkinter import *
from tkinter import ttk

gui = Tk()

toplabel = StringVar()
value = StringVar()

def combofunc(*args):
    toplabel.set('* ' * 10 * int(value.get()))

ttk.Label(gui, textvariable=toplabel).grid(column=1, columnspan=3, row=1, sticky=W)
Spinbox(gui, from_=1, to=100, textvariable=value, command=combofunc).grid(column=1, row=2, sticky=W)
ttk.Label(gui, text='Keep me still').grid(column=2, row=2, sticky=W)
ttk.Label(gui, text='Move me').grid(column=3, row=2, sticky=W)

for child in gui.winfo_children():
    child.grid_configure(padx=5, pady=5)

combofunc()
gui.mainloop()

EDIT: I meant I want the padding to be added either on the right hand side of column 2, or between column 2 and column 3. 编辑:我的意思是我希望在第2列的右侧或第2列和第3列之间添加填充。

You need to configure the columns with expansion weights. 您需要为列配置扩展权重。 Add the following somewhere. 在某处添加以下内容。

for i, j in ((1,0), (2,1), (3,0)):
    gui.columnconfigure(i, weight=j)

I used this tkinter doc 我使用了这个tkinter文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM