10

I have been working on a note taking program for myself and it is going well however I have had a lot of problems with getting all my widgets placed where I want them using the .pack() or .grid() options.

After looking around I found that I could use the .place() option instead. Before I decided to use .place() I found countless forum post saying "don't use .place()!".

I was at a stand still with my other options so I decided to give .place() a try. It turns out .place() is exactly what I needed to fix my layout issues and I just don't understand why everyone is hating on .place() so much.

Is there something inherently wrong with .place()? Or do people just prefer to use .pack() and .grid() for some practical reason other than ease of use?

martineau
  • 119,623
  • 25
  • 170
  • 301
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Who said "*dont use `.place()`!*"? Those countless forum lies – Taku Mar 28 '17 at 15:04
  • @abccd I said "dont use place()!" as a generalization of the comments I was reading over the net. people who would ask questions about grid layouts using pixels or using .place() and a problem they were having were told to use .grid() instead. Note: I do a lot of my research on things outside of stackoverflow. many of the questions I have tend to be on other sites when asking in google search. – Mike - SMT Mar 28 '17 at 15:08
  • I am a little late commenting on this, but it looks you weren't aware of all the possibilities of grid, pack and their combination. I feel no limitations with them and I think Bryan Oakley answer is quite definitive. Not because it's Bryan opinion but because he's right in every point. Of course there are good edge case uses for place but I personally don't like seeing answers that may promote it's use for the unaware. For example do you parametrize the pixel units in your place GUI? Because if pixel density becomes higher, your app will shrink. – progmatico Apr 19 '19 at 16:16
  • And what about the fonts. If this was not bad enough I am not sure they shrink along in the same way with the widgets dimensions. – progmatico Apr 19 '19 at 16:17
  • And you'll spend more time adjusting everything and worrying about resizing. – progmatico Apr 19 '19 at 16:19

2 Answers2

8

I'm not sure what evidence you have that says everyone says not to use place. I suspect if you're judging by stackoverflow posts, you're mostly reading my opinion a hundred times rather than a hundred different opinions.

I recommend against place mainly because it requires more work to make a UI that is responsive to changes in fonts, resolutions, and window sizes. While it's possible to write a GUI that uses place and is responsive to those things, it requires a lot of work to get right.

One advantage that both pack and grid have over place is that they allow tkinter to properly configure the size of the root and Toplevel windows. With place you must hard-code a size. Tkinter is remarkably good at making windows to be the exact right size without you having to decide on explicit sizes.

In addition, long term maintenance of applications that use place is difficult. If you want to add a new widget, you will almost certainly have to adjust every other widget. With grid and pack it's much easier to add and remove widgets without having to change the layout of all of the other widgets. If I've learned anything over years of using tk and tkinter is that my widget layout changes a lot during development.

place is mostly useful for edge cases. For example, if you want to center a single widget inside another widget, place is fantastic. Also, if you want to place a widget such that it is independent of other widgets, place is great for that too.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • most of my research is outside of stack overflow. I start in google search and work my way down the list. I tend to use stackoverflow when I get stuck or I need a well written explanation of something. – Mike - SMT Mar 28 '17 at 15:26
  • Also, `place` is probably the easiest way to display a widget over another, when the position of the child is relative to its parent. – Right leg Sep 06 '17 at 15:56
  • @Rightleg: I wouldn't say that's always true. For example, if I'm placing a single widget such as a canvas or text widget over a frame and I want it to fill the frame, `pack` can be as good if not better than `place`. – Bryan Oakley Sep 06 '17 at 16:07
  • @BryanOakley Yes, I was more thinking about situations where the relative coordinates are actually useful - but my knowledge of `pack` is probably partial. – Right leg Sep 06 '17 at 18:49
4

There's nothing really wrong with .place, although using grid and pack give you more maintainable code. If you want to add a feature then place would require you to alter loads of absolute placements to fit a button in, for example.

If you need to use it then use it, there's no real problem with it, it just isn't the most maintainable solution to many problems. As you say, it's a matter of preference and ease of use.

Edit: there's an excellent answer you can read about it here.

Community
  • 1
  • 1
Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
  • That makes sense. But to be honest I have had a really hard time with .grid trying to get everything where I want it and .place is just as bad if not worse than .grid. How do you deal with the resizing issues. columnspan or rowspan helps a little but in the long run getting stuff place where you want it and the size you want it becomes a huge pain. – Mike - SMT Mar 28 '17 at 14:42
  • @BaconTech What do you mean with resizing issues? You can bind your canvas to a resize function if it needs to resize dynamically, or if you mean how you can border or play with white space then there are [lots of options](http://stackoverflow.com/a/14947657/6144626). If you're having a particular problem with resizing then that's best left to a different question :) – Ari Cooper-Davis Mar 28 '17 at 14:50
  • @Air Cooper-Davis I will be looking at the link you provided. I am new to programing and there is much for me to figure out. I might have been missing something from the grid option that could help with my problem. – Mike - SMT Mar 28 '17 at 15:01
  • 2
    @BaconTech: resizing issues are relatively simple to solve if you follow some basic best practices, such as specifying `fill` and `expand` options for `pack`, and using the `sticky` attribute and `rowconfigure` and `columnconfigure` with grid. – Bryan Oakley Mar 28 '17 at 15:26
  • @Bryan Oakley I have been reading the stuff about fill. I only was aware of row/columnconfigure, padding, row/columnspan. This was causing me several problems. I tried to Use minsize=value + a text inside each row/column just to get the spacing going. Still was not doing what I needed. I will be looking into fill and expand soon. – Mike - SMT Mar 28 '17 at 15:29