簡體   English   中英

如何在Python Tkinter中將這些按鈕覆蓋在圖像的背景上

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

目前,我正在嘗試使用Tkinter作為簡單的GUI為游戲創建簡單的主菜單,因為它是簡單的RPG游戲和Python,但是圖像在所有情況下都覆蓋按鈕。

我嘗試過使用其他解決方案,例如放置它們或創建窗口,但我都找不到如何做到的直接答案。

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

當前,背景的母版設置為root而按鈕設置為框架。 您需要做的第一件事是將兩者都設置為同一母版,即將背景母版更改為content

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

接下來,您需要處理堆疊順序。 您可以調用widget.lift()將按鈕升至頂部:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM