简体   繁体   English

如何在背景图像Tkinter上添加标签

[英]how to add label over background image tkinter

button, label etc over an background image using tkinter. 使用tkinter在背景图像上单击按钮,标签等。 few days before I just stared learning python so I am sorry if this question look silly.. my code is below.. but at this moment label is showing below image not over image..please correct me. 在我刚开始学习python的几天前,如果这个问题看起来很傻,我很抱歉..我的代码在下面..但是此刻,标签显示在图像下方而不是图像上方..请纠正我。

import tkinter

haren = tkinter.Tk()

width, height = haren.winfo_screenwidth(), haren.winfo_screenheight()
bg = tkinter.PhotoImage(file="img/bg.png")

panel1 = tkinter.Label(haren, image=bg)
panel1.pack(side='top', fill='both', expand='yes')

haren.wm_title("Hi Sana")
haren.grid()
yeah=tkinter.Label(haren, text="Developed by Full Mad Haren Sarma")
yeah.pack()
haren.wm_geometry("%dx%d+0+0" % (width, height))
haren.mainloop()

In your current code, if you want both the image and the text to be visible, the window must be big enough for both. 在当前代码中,如果希望图像和文本都可见,则窗口必须足够大。 If the background image is exactly as large as your screen size, the text will be hidden. 如果背景图片恰好与您的屏幕尺寸一样大,则文本将被隐藏。 You can reveal the text by increasing the window size (I recommend testing with a smaller image), noting that it collapses under the image when you shrink the window. 您可以通过增加窗口大小来显示文本(我建议使用较小的图像进行测试),请注意当您缩小窗口时,该文本会在图像下方折叠。

Try changing your geometry manager to grid instead of pack . 尝试将几何图形管理器更改为grid而不是pack

panel1.pack(side='top', fill='both', expand='yes')

changes to: 更改为:

panel1.grid(row=0, column=0)

and

yeah.pack()

changes to: 更改为:

yeah.grid(row=0, column=0, sticky='s')

Note how both widgets are added to the same row and column, so the most recently- grid() ed widget will appear on top of previous ones. 请注意,这两个小部件是如何添加到同一行和同一列的,因此,最新grid()小部件将显示在先前小部件的顶部。 The sticky option indicates where in its grid square the widget will rest (the south end in this case). sticky选项指示小部件将在其grid正方形中停在的位置(在这种情况下为南端)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM