当我创建表格时,我需要不同大小的列。但是当我设置不同的权重时,列不会调整大小。请参阅以下代码:
root = Tk()
frame = Frame(root, # creating frame in root
width=1440,
height=900,
bg="lightblue")
frame.grid(in_=root) # add frame in root row=0 column=0 (default)
canv1 = Canvas(frame, # creating canvas
bg="red",
height=10)
canv2 = Canvas(frame,
bg="green",
height=20)
canv3 = Canvas(frame,
bg="yellow",
height=30)
canv4 = Canvas(frame,
bg="blue",
height=40)
canv1.grid(in_=frame, row=0, column=0) #add canvas in the grid
canv2.grid(in_=frame, row=0, column=1)
canv3.grid(in_=frame, row=0, column=2)
canv4.grid(in_=frame, row=0, column=3)
frame.columnconfigure(0, weight=1) # set weights
frame.columnconfigure(1, weight=2)
frame.columnconfigure(2, weight=3)
frame.columnconfigure(3, weight=4)
root.mainloop()
非常感谢!
答案 0 :(得分:1)
在画布小部件上调用sticky
函数时,需要添加grid
属性:
canv1.grid(in_=frame, row=0, column=0, sticky="nsew")
这将导致画布的边缘粘在它给出的空间的两侧。随着空间的增长和缩小,画布也将如此。
顺便说一句,您不需要in_
参数,因为画布是框架的子项。
答案 1 :(得分:-1)
columnconfigure
用于在调整窗口大小时共享空间因此,如果要修改默认值'大小,最好的方法是修改画布'尺寸:
canv1 = Canvas(frame, # creating canvas
bg="red",
height=10,
width=700)