简体   繁体   English

如何在Tkinter中堆叠多个帧

[英]How can I stack multiple frames in Tkinter

For my project, I need to create a GUI using Tkinter. 对于我的项目,我需要使用Tkinter创建一个GUI。 I wanted to divide it into 4 different frames, each with different background colors. 我想将其分为4个不同的框架,每个框架具有不同的背景色。 However, when I do this, only the first frame's background color is shown. 但是,当我这样做时,仅显示第一帧的背景色。 I attached my code below as well as a screenshot of the output I'm getting. 我在下面附加了我的代码以及获得的输出的屏幕截图。

我的Tkinter GUI

from Tkinter import *
import tkMessageBox

root = Tk()
root.geometry("950x800")
root.configure(background="black")

def test():
   print("this is a test")

# *****Frames*****

fileFrame = Frame(root)
fileFrame.configure(background="yellow")
fileFrame.pack()
attributeFrame = Frame(root)
attributeFrame.configure(background="red")
attributeFrame.pack()
constraintFrame = Frame(root)
constraintFrame.configure(background="purple")
constraintFrame.pack()
preferenceFrame = Frame(root)
preferenceFrame.configure(background="blue")
preferenceFrame.pack()

# *****File Frame*****

label_1 = Label(fileFrame, text="Enter Attributes file name:", anchor="e",                 
bg="red", font="Times 25", width=25, height=1)
label_2 = Label(fileFrame, text="Enter hard constraints file name:",     
anchor="e", bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_3 = Label(fileFrame, text="Enter preferences file name:", 
anchor="e", bg="purple", fg="green", font="times 25", width=25, height=1)
entry_1 = Entry(fileFrame, font="Times 25")
entry_2 = Entry(fileFrame, font="Times 25")
entry_3 = Entry(fileFrame, font="Times 25")
button_1 = Button(fileFrame, text="Submit", bg="red", font="Times 20")
button_2 = Button(fileFrame, text="Submit", bg="blue", fg="yellow", 
font="Times 20")
button_3 = Button(fileFrame, text="Submit", bg="purple", fg="green", 
font="Times 20")
label_1.grid(row=0, padx=5, pady=5)
entry_1.grid(row=0, column=1)
button_1.grid(row=0, column=2, padx=5, pady=5)
label_2.grid(row=1, padx=5, pady=5)
entry_2.grid(row=1, column=1)
button_2.grid(row=1, column=2, padx=5, pady=5)
label_3.grid(row=2, padx=5, pady=5)
entry_3.grid(row=2, column=1)
button_3.grid(row=2, column=2, padx=5, pady=5)

# *****Attribute Frame*****

label_Attribute_header = Label(attributeFrame, text="Attributes:", 
bg="red", font="Times 25", width=25, height=1)
label_Attribute_header.pack()

# *****Constraint Frame*****

label_Constraint_header = Label(constraintFrame, text="Hard Constraints:", 
bg="purple", fg="green", font="Times 25", width=25, height=1)
label_Constraint_header.pack()

# *****Preference Frame*****

label_Preference_header = Label(preferenceFrame, text="Preferences:", 
bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_Preference_header.pack()

root.mainloop()

I expect there to be 4 different frames, all stacked on top of each other with different background colors, but instead only the first frame has a background color. 我希望有4个不同的框架,它们彼此堆叠在一起,并具有不同的背景颜色,但只有第一个框架具有背景颜色。 Can somebody explain to me why this is and how I should go about fixing this? 有人可以向我解释为什么会这样,我应该如何解决呢? Thanks 谢谢

Your frames are there. 您的相框在那里。 The bottom three frames have fewer things in them and you haven't given them any padding. 底部的三个框架中包含的内容较少,您没有给它们填充任何内容。 The frame shrinks to fit, so when you just have one item, you won't see the frames. 框架会缩小以适合,因此当您只有一件物品时,您将看不到框架。

You can easily see the frames if you do one of two things: 如果执行以下两项操作之一,则可以轻松看到框架:

First, you can request that the frames fill their parent window in the x direction. 首先,您可以请求框架在x方向上填充其父窗口。 When you do this, you'll see them: 执行此操作时,您将看到它们:

fileFrame.pack(fill="x")
attributeFrame.pack(fill="x")
constraintFrame.pack(fill="x")
preferenceFrame.pack(fill="x")

Second, instead of or in addition to that, you can give padding around the labels in the bottom frames. 其次,除了此以外,还可以在底部框架中的标签周围添加填充。 That will let the frame colors appear. 这将使框架颜色出现。

label_Attribute_header.pack(padx=20, pady=20)
...
label_Constraint_header.pack(padx=20, pady=20)
...
label_Preference_header.pack(padx=20, pady=20)

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

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