我正在制作一个游戏来测试我的技能,我创建了一个框架,该框架应该显示更多包含带有文本和图像的标签的框架。由于某种原因,主框架自行出现和消失,有时在屏幕上停留几秒钟,有时闪烁。框架内似乎没有出现任何小部件。
我添加了一些调试打印,似乎所有图像都已正确加载。程序不会停止,它会继续工作,但是,正如我所说,那一帧似乎闪烁并消失了。
这里是被调用以显示有问题的框架的函数的代码:
def show_party():
global root, screenw, button_set, collection_page, selected_party_member
try:
screenw["PARTY_GRID"].destroy()
del screenw["PARTY_GRID"]
except: pass
screenw["PARTY_GRID"] = Frame(
root,
bg = "#050505",
width = 450,
height = 128 * 6,
)
screenw["PARTY_GRID"].place(x = 100, y = 150)
n = 0
for party_slot in player_account.party:
n += 1
screenw[f"PARTY_FRAME_NO{n}"] = Frame(
screenw["PARTY_GRID"],
bg = "#FFFFFF",
)
screenw[f"PARTY_FRAME_NO{n}"].grid(row = n, column = 0)
print(f"PARTY SLOT NO. {n}: {party_slot}")
if party_slot[0] != None:
pokeicon = POKEMON_ICONS[party_slot[0].id]
else:
pokeicon = IMAGE_UNKNOWN
print(f"POKEICON NO. {n}: {pokeicon}")
screenw[f"PARTY_NO{n}_ICON"] = Button(
screenw[f"PARTY_FRAME_NO{n}"],
bg = "#050505",
image = pokeicon,
border = 0,
borderwidth = 0,
) #
print(f"ICON LOADED!")
screenw[f"PARTY_NO{n}_ICON"].place(x = 0, y = 0)
print(f"ICON PLACED!")
if party_slot[0] != None:
screenw[f"PARTY_NO{n}_NICKNAME"] = Label(
screenw[f"PARTY_FRAME_NO{n}"],
bg = "#050505",
fg = "#FFFFFF",
font = ("Minecraft", 15),
text = f"{party_slot[0].nickname}",
)
screenw[f"PARTY_NO{n}_NICKNAME"].place(x = 132, y = 0)
screenw[f"PARTY_NO{n}_LEVEL"] = Label(
screenw[f"PARTY_FRAME_NO{n}"],
bg = "#050505",
fg = "#FFFFFF",
font = ("Minecraft", 10),
text = f"Level {party_slot[0].level}",
)
screenw[f"PARTY_NO{n}_LEVEL"].place(x = 132, y = 20)
我正在使用Python 3.10.11。
请注意,使用
.place()
将小部件放入框架中不会调整父框架的大小,因此默认情况下父框架的大小为 1x1(以像素为单位)。
您需要指定足够大的父框架的大小以查看所有子窗口小部件:
for party_slot in player_account.party:
n += 1
screenw[f"PARTY_FRAME_NO{n}"] = Frame(
screenw["PARTY_GRID"],
bg = "#FFFFFF",
width = 200, # adjust this value to the desired width
height = 200, # adjust this value to the desired height
)
...
或者使用
.grid()
或 .pack()
来放置这些子部件。