Reputation: 47
for my GUI I want to use a grid with 6 columns. The size of the table is set. How do I fill the width of the grid with the columns? Can you set the size of the columns? I only found padx
to change the padding, but not the actual size of the columns.
For now, this is supposed to work on a canvas. Is that even possible?
Upvotes: 1
Views: 4089
Reputation: 385970
If you've fixed the size of the table, you can configure the columns to each be 1/6th the width of the window as a whole. The trick is to give each column the same non-zero weight
, and use the uniform
option. The uniform
option takes a string, and all columns with the same value will be the same width.
If you run this code, notice how you can resize the window and the columns will automatically resize as well.
import tkinter as tk
import sys
root = tk.Tk()
root.geometry("600x400")
columns = []
for i in range(6):
frame = tk.Frame(root, borderwidth=1, relief="raised", background="bisque")
columns.append(frame)
root.grid_rowconfigure(0, weight=1)
for column, f in enumerate(columns):
f.grid(row=0, column=column, sticky="nsew")
root.grid_columnconfigure(column, weight=1, uniform="column")
root.mainloop()
Upvotes: 2
Reputation: 363
With only the information you passed i would recommend you following:
Grid layouts your objects. Its not a table.
If you work with grid here. The simplest thing should be to set the width of the object you put on the wanted grid. For example:
If you add a Label set the label to the width you want it to be. This label will then be displayed in the grid with its width and height.
If you want to display a table. Or even better load a table from a database. I would recommend a Treeview. Its good for design allows multiselect and its easy to fill and you dont have the layout problems :)
Upvotes: 0