0

我有一个 Tkinter 应用程序,我希望标签完全占据空白空间(因为我已将标签设置为我的应用程序的背景图片)。但是当我没有指定标签的高度和宽度时,它也会像下面的代码一样吃掉框架。如何使其位于框架下方,但占用空白空间???

代码 -->

#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()
4

2 回答 2

1

您可以在标签上使用place()而不是pack()填充可用空间:

# 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)
于 2021-02-23T15:27:50.430 回答
1

如果您将其用作背景图像,则最好使用place. 当您使用place时,它不会影响窗口或任何其他小部件的几何形状(即:您不必调用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")
于 2021-02-23T15:48:38.687 回答