tkinter 中按钮后面的图像(PhotoImage) [英] Image behind buttons in tkinter (PhotoImage)

查看:36
本文介绍了tkinter 中按钮后面的图像(PhotoImage)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试添加图像,以便我的按钮位于图像顶部,但只能使图像完全覆盖所有内容或强制图像位于按钮覆盖的水平部分下方.

I've been trying to add an image so that my buttons sit on top of the image, but have only been able to make the image cover everything completely or force the image to be underneath the horizontal part the buttons cover.

这里是它的相关代码:

class MainMenu(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.initUI()

    def initUI(self):
        self.master.title("Adventure")
        bg = PhotoImage(file="Background-gif.gif")

        newGameButton = Button(self, text="New Game", height=2, width=20, command=self.newGame)
        newGameButton.pack(side=TOP, pady=50)
        loadGameButton = Button(self, text="Load Game", height=2, width=20, command=self.loadGame)
        loadGameButton.pack(side=TOP)
        quitButton = Button(self, text="Quit", height=2, width=20, command=self.close)
        quitButton.pack(side=TOP, pady=50)

        label = Label(self, image=bg)
        label.image = bg
        label.pack(fill=BOTH, expand=1)

        self.pack()

非常感谢.

推荐答案

你可以在画布上放置一个图像,然后放置一个画布上的按钮:

You could place an image on a canvas, and then place a button on the canvas:

import Tkinter as tk
import ImageTk

FILENAME = 'image.png'
root = tk.Tk()
canvas = tk.Canvas(root, width=250, height=250)
canvas.pack()
tk_img = ImageTk.PhotoImage(file = FILENAME)
canvas.create_image(125, 125, image=tk_img)
quit_button = tk.Button(root, text = "Quit", command = root.quit, anchor = 'w',
                    width = 10, activebackground = "#33B5E5")
quit_button_window = canvas.create_window(10, 10, anchor='nw', window=quit_button)    
root.mainloop()

这篇关于tkinter 中按钮后面的图像(PhotoImage)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆