如何在Python Tkinter中将这些按钮覆盖在图像的背景上

时间:2019-07-09 09:34:09

标签: python-3.x 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()

1 个答案:

答案 0 :(得分:0)

当前,背景的主菜单设置为import * as Future from 'fluture'; const buildPerson = (name, age) => name !== undefined && age !== undefined ? Future.of({ name, age }) : Future.reject(Error("Missing parameter")); const validatePerson = ({ name, age }) => age < 18 ? Future.reject(Error("Too Young")) : Future.of({ name, age }); const getNameFromRepo = (repo: Repo) => { return Future.Future((reject, resolve) => resolve(repo.getName())); } const getAgeFromRepo = (repo: Repo) => { return Future.Future((reject, resolve) => resolve(repo.getAge())); } const savePerson = (repo: Repo) => (person) => { return Future.Future((reject, resolve) => resolve(repo.setPerson(person))); } const hello = (repo: Repo) => Future.parallel(1, [ getNameFromRepo(repo), getAgeFromRepo(repo), ]) .chain(([name, age]) => buildPerson(name, age)) .chain(validatePerson) .chain(savePerson(repo)) .fork( (err) => console.warn('err', err), () => console.log('it worked!'), ); hello(new Repo()); ,而按钮设置为框架。您需要做的第一件事是将两者都设置为相同的母版,即将背景母版更改为root

content

接下来,您需要处理堆叠顺序。您可以调用backgroundlabel = tk.Label(content, image=background) 将按钮移至顶部:

widget.lift()