0

I feel like I am missing something stupidly obvious here - surely the widgets should just be positioned along the top of the root window, but they completely ignore the .grid() function.

When I printed the grid_size(), it returned (0,0). Why?!

Any help greatly appreciated :)

title = tk.Label(root, text="Enter a city below")
title.grid(row=0, column=0)
title.pack()

e = tk.Entry(root)
e.grid(row = 0, column = 1)
e.pack()

current_clicker = ttk.Button(root, text = "Current forecast", command=current)
current_clicker.grid(row=0, column=2)
current_clicker.pack()

hourly_clicker = ttk.Button(root, text = "By hour", command=hourly)
hourly_clicker.grid(row=0, column=3)
hourly_clicker.pack()

minute_clicker = ttk.Button(root, text = "By minute", command=minute)
minute_clicker.grid(row=0, column=4)
minute_clicker.pack()

daily_clicker = ttk.Button(root, text = "Daily", command=daily)
daily_clicker.grid(row=0, column=5)
print(daily_clicker.grid_size())
daily_clicker.pack()

3 Answers3

1

I believe all you have to do is remove either pack() or grid(..) method. It is not recommended to mix pack() and grid(), as it might erase the effect of the other and lead to such errors.

I recommend to get rid of pack() as grid(...) is a more orderly way of managing widgets, compared to pack().

Tiny example:

hourly_clicker = ttk.Button(root, text = "By hour", command=hourly)
hourly_clicker.grid(row=0, column=3)

minute_clicker = ttk.Button(root, text = "By minute", command=minute)
minute_clicker.grid(row=0, column=4)

Hope it solved your doubts, if any more errors, do let me know

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
1

Only one geometry manager can manage a widget at a time. So exactly like the title of the question is saying, when you call .pack() after calling .grid(...), the effects of grid(...) are ignored. For any given widget, you must only use one, and you need to be consistent with all widgets that have the same parent.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

pack, grid and place are the 3 methods available for placing a widget. tkinter refers to these as "geometry managers". Each of these provides a unique way to position and scale widgets.

  • pack will dock your widget to a defined or available edge
  • grid behaves like a table
  • place allows you to define an arbitrary position (and size)

An individual widget cannot have more than one geometry manager attached to it, and parents cannot contain widgets that alternate between pack and grid. This means (for example), if you have a Frame full of widgets that use pack, none of those widgets can use grid, and vise-versa. place is not subject to this limitation and can be used anywhere.

OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26