繁体   English   中英

为什么我的小部件只出现在一种情况下?

[英]Why do my widgets only show up in one case?

我正在制作一个基于周期表 tkinter 的游戏。我把粒子框架做得很好,所以我决定复制代码并将其重新用于元素框架,只更改变量名称。 但出于某种原因,即使粒子框架工作得很好,元素框架也没有显示任何内容。 这是我的完整代码:

# Boilerplate

import random
import periodictable as pt
from tkinter import *
root = Tk()
root.title('Periodic Table Game')
root.geometry('350x250')
LightBlue = "#b3c7d6"
Menu = Frame(root)
elementFrame = Frame(root)
particleFrame = Frame(root)
settingsFrame = Frame(root)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
for AllFrames in (Menu, elementFrame, particleFrame, settingsFrame):
    AllFrames.grid(row=0, column=0, sticky='nsew')
    AllFrames.configure(bg=LightBlue)
def show_frame(frame):
    frame.tkraise()
show_frame(Menu)

# Menu Frame

Menu.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

MenuTitle = Label(Menu, text="Periodic Table Game", font=("Arial", 15), bg=LightBlue)
MenuTitle.grid(row=0, column=0, pady=25)
MenuTitle.grid_rowconfigure(1, weight=1)
MenuTitle.grid_columnconfigure(1, weight=1)

MenuButton1 = Button(Menu, width=25, text="Guess The Particles", command=lambda: show_frame(particleFrame))
MenuButton1.grid(row=1, column=0)

MenuButton2 = Button(Menu, width=25, text="Guess The Element Name", command=lambda: show_frame(elementFrame))
MenuButton2.grid(row=2, column=0, pady=5)

SettingsButton = Button(Menu, width=25, text="Settings", command=lambda: show_frame(settingsFrame))
SettingsButton.grid(row=3, column=0)

# Particle Frame

particleFrame.grid_columnconfigure(0, weight=1)

BackButtonF2 = Button(particleFrame, text='Back', command=lambda: show_frame(Menu))
BackButtonF2.grid(row=0, column=0, sticky=W)

ParticleLabel = Label(particleFrame, text='testing', bg=LightBlue)
ParticleLabel.grid(row=1, column=0, pady=15)

ParticleEntry = Entry(particleFrame)
ParticleEntry.grid(row=2, column=0, pady=10)

ParticleEnter = Button(particleFrame, text='Enter', width=10)
ParticleEnter.grid(row=3, column=0, pady=10)

# Element Frame

elementFrame.grid_columnconfigure(0, weight=1)

BackButtonF3 = Button(particleFrame, text='Back', command=lambda: show_frame(Menu))
BackButtonF3.grid(row=0, column=0, sticky=W)

ElementLabel = Label(particleFrame, text='testing', bg=LightBlue)
ElementLabel.grid(row=1, column=0, pady=15)

ElementEntry = Entry(particleFrame)
ElementEntry.grid(row=2, column=0, pady=10)

ElementEnter = Button(particleFrame, text='Enter', width=10)
ElementEnter.grid(row=3, column=0, pady=10)

root.mainloop()

为什么相同的代码只适用于一帧?

准确地说,因为您复制了代码,所以您没有发现问题所在。 当您定义元素框架小部件时,您会将它们全部放入 particleFrame 中。 例子:

BackButtonF3 = Button(particleFrame, text='Back', command=lambda: show_frame(Menu))

应该

BackButtonF3 = Button(elementFrame, text='Back', command=lambda: show_frame(Menu))

您的问题将得到解决。

将此particleFrame更改为elementFrame

片段代码:

BackButtonF3 = Button(elementFrame, text='Back', command=lambda: show_frame(Menu))
BackButtonF3.grid(row=0, column=0, sticky=W)

ElementLabel = Label(elementFrame, text='testing', bg=LightBlue)
ElementLabel.grid(row=1, column=0, pady=15)

ElementEntry = Entry(elementFrame)
ElementEntry.grid(row=2, column=0, pady=10)

ElementEnter = Button(elementFrame, text='Enter', width=10)
ElementEnter.grid(row=3, column=0, pady=10)

之前的截图:

在此处输入图像描述

elementFrameparticleFrame相同后的屏幕截图:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM