【问题标题】:How to put button widget on label widget and decide its position in Tkinter如何将按钮小部件放在标签小部件上并确定其在 Tkinter 中的位置
【发布时间】:2012-04-16 19:42:14
【问题描述】:

如何将按钮小部件放在背景图像之上?

from Tkinter import *
import tkMessageBox

root = Tk()

# load image
logo = PhotoImage(file="C:\\Users\\admin\\Desktop\\project\\front page.gif")
w = logo.width()
h = logo.height()

root.geometry('%dx%d+0+0' % (w,h))

def helloCallBack():
    tkMessageBox.showinfo("Hello Python", "Hello World")

#create widgets
B = Button(root,text = "ASSOCIATIONS", command = helloCallBack,width = 20,padx = 20)
B.pack()

B1 = Button(root,text = "HELP", command = helloCallBack,width = 20,padx = 20)
B1.place(relx=1, x=-50, y=2, anchor=NE)
B1.pack()

w1 = Label(root, compound = CENTER, image = logo).pack(side="right")

root.mainloop()

【问题讨论】:

    标签: python tkinter tk


    【解决方案1】:

    最简单的做法是使用place 将背景图像放入小部件中,然后按照您通常使用packgrid 的方式将其他小部件放入该小部件中

    background=Label(root, image=logo).place(x=0,y=0,relwidth=1, relheight=1)
    b = Button(root, ...).pack(...)
    

    确保先创建背景,使其具有较低的堆叠顺序。

    如果你真的想在标签中放置一个按钮,只需将标签设为按钮的父级,然后像往常一样使用 packgrid —— 在 Button 或标签或任何其他小部件(不过,最终结果可能不是您所期望的)。

    【讨论】:

    • 另一个简单的事情是将按钮和图像放在画布中。画布内的小部件始终位于画布中其他所有内容的顶部。 (如果可以选择正确分层,那就太好了,但目前还没有。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2022-06-18
    相关资源
    最近更新 更多