0

I am trying to put a single vertical tkinter lable at the left of my window.

    FONT = ('helvetica', 18)
    write = 'hello world'

    label = tk.Label(
        window, anchor = 's', background = BACKGROUNDGREY, borderwidth = 0, font = FONT, foreground = 'white',
        justify = 'center', pady = 40, relief = 'flat', text = write, width = len(write) * 2
    )

    label.pack(side = 'left', fill = 'y')

Everything works just fine as shown here:enter image description here

But now, when I add this code to put a button...

    btn = tk.Button(
        label, activebackground = BACKGROUNDGREY, borderwidth = 0, background = BACKGROUNDGREY, height = 70,
        highlightcolor = 'white', width = 70
    )

    btn.pack()

This happen: enter image description here

as you can see, i don't know why but the label's text is not readable anymore and it also resize the width and I want to keep my label of the width shown in the first image.

UPDATE: I see that some of us did not understand: i want exatly the button inside the label.

Gianla
  • 63
  • 8
  • 1
    Um... You are trying to set the Button's parent to the Label?! Is this intended? – Minion Jim Aug 31 '20 at 14:58
  • yes i want the button to be inside the label. – Gianla Aug 31 '20 at 22:40
  • "I want exactly the button inside the label". What are you trying to achieve (because putting a button inside a label will not help you)? If you want text behind the button (which can be the only reason I can think of why you would be so insistent on having the button in the label), you could probably use a canvas with `create_text` and `create_window`. – Minion Jim Sep 01 '20 at 10:48
  • Little complex here. I just want, as many application do, put a little label at the left of the window filled with buttons and the main body on the right. Since i can't use grid because i want the column to be of a certain width, i thought that a label (that have a width) could solve my problem... I do not either know if this is the best option. If you have any ideas please tell me. – Gianla Sep 01 '20 at 14:24
  • In that case, you can probably still use a Frame with grid, but use `grid_propagate(0)` as suggested by Bryan in [another of his answers](https://stackoverflow.com/a/566840) – Minion Jim Sep 01 '20 at 15:10

1 Answers1

1

You have put the button inside the label. Also, you've requested that the button be 70 characters tall. pack will shrink or expand a parent to fit its children, so the label is shrinking to exactly fit the button, and the button has a higher stacking order so it obscures the text of the label.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Ok, you're right. But i also tried smaller size and the label keeps shrinking as the button size goes up/down. – Gianla Aug 31 '20 at 22:45
  • @Gianla: yes, like the answer said, the label will shrink since you are putting the button inside the label. Tkinter widgets by default shrink or expand to fit their children. – Bryan Oakley Sep 01 '20 at 01:10