简体   繁体   中英

How do I overlay these buttons on top of an image for a background in Python Tkinter

Currently, I'm attempting to create a simple main menu for a game using Tkinter as a simple GUI since it's a simple RPG game and Python however the image overlays the buttons in all circumstances.

I've tried using some other solutions like placing them or creating a window but I can't find a straight answer of how to make that either.

import tkinter
from tkinter import *
from PIL import ImageTk, Image 
(PIL is from when I was using a JPG before.)   

root = Tk()
content = ttk.Frame(root)
root.geometry("600x600")

background = ImageTk.PhotoImage(Image.open("bred.png"))
canvas = tkinter.Canvas(root, width=580, height=600)

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

Btn1 = Button(content, text="Play", width=5, height=1)
Btn2 = Button(content, text="Kill me", width=7, height=1, command = 
root.quit)

backgroundlabel = tkinter.Label(root, image=background)
backgroundlabel.image = background

backgroundlabel.place(x=0, y=0, relwidth=1, relheight=1)

Btn1.grid(row=1, column=2, padx=(130))
Btn1.columnconfigure(1, weight=1)
Btn1.rowconfigure(1, weight=1)

Btn2.grid(row=1, column=3, pady=(130))
Btn2.columnconfigure(3, weight=1)
Btn2.rowconfigure(1, weight=1)

root.mainloop()

Currently your background's master is set to root while your buttons are set to a frame. The first thing you need to do is set both to the same master, ie changing background master to content :

backgroundlabel = tk.Label(content, image=background)

Next you need to deal with the stacking order. You can call widget.lift() to raise the buttons to top:

Btn1.grid(row=1, column=2, padx=(130))
...
Btn1.lift()

Btn2.grid(row=1, column=3, pady=(130))
...
Btn2.lift()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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