因此,我正在为游戏制作“ HUB”,并且我的背景在那里,但是我无法在背景上显示按钮。我在许多论坛上进行了搜索,但没有找到适合我脚本的答案。不知道我该怎么办。该脚本非常简单,因为它应该只是一个HUB。
编辑:所以我做了一些更改,现在我同时拥有按钮和图片,但是我不能将两个按钮都放在图片上。就像图片本身是一排。
这是脚本:
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()
答案 0 :(得分:1)
我已经调整了您的代码以支持我的工作..结果,我只想做了几处调整就可以了:-)
您可能需要记录columnconfigure和rowconfigure以及grid方法的粘性参数。
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)
答案 1 :(得分:0)
使用背景图像的最简单解决方案是使用place
将图像添加为背景。 place
不会更改父级的大小或父级中任何其他小部件的布局,并且可以像平常一样使用grid
或pack
在顶部添加其他小部件将。
请注意,在创建其他窗口小部件之前,请先创建图像窗口小部件,以使其在堆叠顺序中较低,这一点很重要。或者,您可以在小部件上调用lower()
方法以将其移至堆叠顺序的底部。
示例:
background1.place(relx=.5, rely=.5, anchor="c")