0

I have a Tkinter application, and I want the Label to completely take up the empty space (because I have set the Label as a background picture for my App). But when I don't specify the height and the width of the Label, it also eats up the Frame as in the code below. How to make it so that its below the Frame, but takes up the empty space???

Code -->

#importing everything
from tkinter import *
from pypresence import Presence
import time


#making the root window
root = Tk()

dimension = '800x500'
#setting the window
bg = PhotoImage(file = r'C:\Users\Hunter\Desktop\school 1\module\pbm\bg.png')
window = Label(root, bd=0, image=bg)
window.pack(fill=BOTH, expand=True, side=BOTTOM)

#overriding the default properties
root.overrideredirect(True)

root.geometry(dimension)
#the main title bar
title_bar = Frame(root, bg='#496E82', bd=0, height=4)


#pack all the widgets
title_bar.pack(fill=X, side=TOP)



#code for moving the window
def get_pos(event):
    xwin = root.winfo_x()
    ywin = root.winfo_y()
    startx = event.x_root
    starty = event.y_root

    ywin = ywin - starty
    xwin = xwin - startx
    def move_window(event):
        root.geometry(dimension + '+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin))
    startx = event.x_root
    starty = event.y_root
    title_bar.bind('<B1-Motion>', move_window)


#binding the title bar so that it moves
title_bar.bind('<Button-1>', get_pos)


#main thing
root.mainloop()
Hunter
  • 188
  • 2
  • 11
  • Try put it inside a frame and set the width/height of the frame. You might also need to use `pack_propagate(False)` or `grid_propagate(False)`. – TheLizzard Feb 23 '21 at 13:47
  • Why are you using a frame? Just use `root` instead? – Delrius Euphoria Feb 23 '21 at 14:10
  • Cool Cloud, I tried using a Frame but didn't make a difference. I need any other way to set the background to only take up the empty space and not all the window... – Hunter Feb 23 '21 at 14:53
  • Remove the frame and use `root.bind(..)` instead of `title_bar.bind(..)` ? – Delrius Euphoria Feb 23 '21 at 17:31
  • Oh well, it's working now. I have removed the override and working with the basic window only haha. The Label is working fine. I will test it better tomorrow when I wake up it's last midnight rn... – Hunter Feb 23 '21 at 19:23

2 Answers2

1

You can use place() instead of pack() on the label to fill the available space:

# As the height of the title bar is 4
# so the label height should be <window height> - 4: relheight=1, height=-4
window.place(x=0, y=4, relwidth=1, relheight=1, height=-4)
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Hey there! Thanks for that, but tbh, I didn't understand what's the use of `relheight` and `relwidth` in that code. Can I get some context on that part? And also, shouldn't be the `y` attribute be set to `-4`? Because in a Cartesian Plane, the numbers are in negative when they go below the x axis if I ain't wrong... – Hunter Feb 23 '21 at 15:47
  • `relheight=1` means 100% of the parent height, `relheight=0.5` means 50% of the parent height, etc. If `relheight` and `height` are used together, then the final height will be `relheight` + `height`, so `relheight=1, height=-4` means 100% parent height minus 4. – acw1668 Feb 23 '21 at 15:49
  • Oh ok, i will try that code, and tell u the output! – Hunter Feb 23 '21 at 15:52
  • Oh, thanks for the context and the information on that. I tried that and it's working. Thank you soo much for the help! – Hunter Feb 23 '21 at 15:58
1

If you're using it as a background image, it's better to use place. When you use place, it won't affect the geometry of the window or any other widgets (ie: you don't have to call overrideredirect).

place allows you to specify a relative width and height as a percentage (where 1 means 100% of the width or height), so you can set it to always be the width and height of the window.

For example, this places the window in the upper-left corner and forces the label to be as tall and wide as the window:

window.place(x=0, y=0, relwidth=1.0, relheight=1.0)

If you want your label to be centered, you can set the relative x and y coordinates to be .5 (eg: 50% of the width or height)

window.place(relx=.5, rely=.5, anchor="center")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • You mean, if I use `overridedirect`, then I won't get the app icon in the task bar and other stuff? – Hunter Feb 23 '21 at 16:09
  • @Hunter: Yes, that's what happens when you use overrideredirect. You can use it if you want, but you don't _have_ to use it if you were only using it to prevent the window from shrinking when you called `window.pack(...)` – Bryan Oakley Feb 23 '21 at 16:11
  • I had to use that only because I want to make a custom title bar for my root window and the other windows. Is there any other way to make it, that's why am not getting my app's logo in the task bar – Hunter Feb 23 '21 at 16:13
  • @Hunter: that is unrelated to the question that is asked. You asked about placing the label. Whether you use `overrideredirect` or not is irrelevant. – Bryan Oakley Feb 23 '21 at 16:14
  • Yup, I think I will open another issue for that method information, sorry for bothering, and thanks for the help! – Hunter Feb 23 '21 at 16:15