我不知道如何在我的背景图片顶部放置按钮,我试过很多论坛,但没有一个答案有效 [英] I can't figure out how to put buttons on top off my background picture, I've tried many forums, but none off the answers worked
问题描述
所以我正在为我的游戏制作一个HUB",我在那里得到了我的背景,但我无法在背景上显示按钮.我在许多论坛上搜索过,没有一个答案适用于我的脚本.不确定我需要做什么.该脚本非常简单,因为它只是一个 HUB.
So I am making a "HUB" for my game, and I got my background there, but I can't display the buttons over the background. I've searched up on many forums and none off the answers worked for my script. Not sure what I need to do. The script is really simple as it's just supposed to be a HUB.
所以我做了一些改变,现在我有按钮和图片,但我不能把这两个按钮放在图片上.就像图片本身就是一排.
So I made a few changes and now I have both buttons and picture, but I can't place both the buttons on the picture. It's like the picture in itself is a row.
脚本如下:
from tkinter import *
root = Tk()
root.geometry("1280x640")
root.title("Choose level")
topFrame = Frame(root, width=1280, height=640)
topFrame.grid(row=0)
background = PhotoImage(file="HUB_BG.png")
background1 = Label(root, image=background)
background1.grid(row=0)
level1 = Button(topFrame, text="Level 1")
level1.grid(row=0)
level2 = Button(topFrame, text="Work in progress")
level2.grid(row=1)
#level3
#level4
#level5
#level6
#level7
#level8
#level9
#level10
root.mainloop()
推荐答案
我已经调整了您的代码以在我这边工作.. 只需进行一些调整即可实现我认为您想要的结果 :-)
I have adapted your code to work on my side.. just a few tweaks will make what I think you wanted as a result :-)
您可能需要记录 columnconfigure 和 rowconfigure 以及 grid 方法的粘性参数.
You may want to document on columnconfigure and rowconfigure, and the sticky parameter of the grid method.
from tkinter import *
root = Tk()
root.geometry("1280x640")
root.title("Choose level")
# Let's assume we are not using your frame.
#topFrame = Frame(root, width=1280, height=640)
#topFrame.grid(row=0)
background = PhotoImage(file="HUB_BG.png")
background1 = Label(root, image=background)
# adding a column to use columnconfigure and rowconfigure...
# using sticky so the image stays expanded in your widget
background1.grid(column=0, row=0, sticky='nsew')
# Below will stick your background label so it doesn't resize with your widgets
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
# replaced topframe with background1.
level1 = Button(background1, text="Level 1")
level1.grid(row=0)
level2 = Button(background1, text="Work in progress")
level2.grid(row=1)
这篇关于我不知道如何在我的背景图片顶部放置按钮,我试过很多论坛,但没有一个答案有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!