简体   繁体   English

Tkinter:为什么子帧不出现在一帧中,而是出现在另一帧中?

[英]Tkinter: Why do child-frames not appear in one frame, but do in another?

I am learning tkinter.我正在学习 tkinter。

I am trying to create the skeleton for an app right now, and I am working on learning tk.Frame( ).我现在正在尝试为应用程序创建骨架,并且正在努力学习 tk.Frame( )。 For background, I have a tk.Tk( ) root with 3 child tk.Frame( )s, and each of those will have a number of tk.Frame( ) children.对于背景,我有一个 tk.Tk( ) 根和 3 个子 tk.Frame( ),每个子都有一些 tk.Frame( ) 子。

Now, things went as expected for creating the root and the 3 child frames in the master=root.现在,在 master=root 中创建根框架和 3 个子框架的事情按预期进行。 Furthermore, the top child frame of root can have frames in it just fine.此外,根的顶部子框架可以在其中很好地包含框架。 However, the frames that I put into the middle child frame of root do not display.但是,我放入根的中间子框架的框架不显示。 Does anyone know why this may occur?有谁知道为什么会发生这种情况? Another issue is that a label that I have in one frame disappears once I put a child frame into a separate master frame.另一个问题是,一旦我将子框架放入单独的主框架中,我在一个框架中的 label 就会消失。

My code below should help explain.我下面的代码应该有助于解释。

from commands import *从命令导入 *

# create root
root = tk.Tk()
appTitle = "Fotoshoop"
root.title(appTitle)

factor = 1.618
rootWidth = 800
rootHeight = int(rootWidth / factor)
geo = "{}x{}".format(str(rootWidth), str(rootHeight))
root.geometry(geo)
# root.grid()

# LAYER 1
l1padx = 10
l1pady = l1padx
l1w = 200
l1h = int(l1w / factor)
l1color = "gray"

Header = tk.Frame(root, bg=l1color, width=l1w, height=l1h)
Header.grid(row=0, column=0,
            ipadx=l1padx, ipady=l1pady, padx=l1padx, pady=l1pady)

Transformations = tk.Frame(root, bg=l1color, width=l1w, height=l1h)
Transformations.grid(row=1, column=0,
                     ipadx=l1padx, ipady=l1pady, padx=l1padx, pady=l1pady)

Content = tk.Frame(root, bg=l1color, width=l1w, height=l1h)
Content.grid(row=2, column=0,
             ipadx=l1padx, ipady=l1pady, padx=l1padx, pady=l1pady)


# LAYER 2
l2padx = 5
l2pady = l2padx
l2color = "blue"

msg_header = "Welcome!"
Label_Header = tk.Label(Header, bg=l2color, text=msg_header)
Label_Header.grid(row=0, column=0, ipadx=l2padx,
                  ipady=l2pady, padx=l2padx, pady=l2pady)

Label_Header = tk.Label(Header, bg=l2color, text="PLACEHOLDER FOR LINKS")
Label_Header.grid(row=0, column=1, ipadx=l2padx,
                  ipady=l2pady, padx=l2padx, pady=l2pady)

#PROBLEM the following code does not display
#        in addition, once added, Label_Header does not display
Transformations_left = tk.Frame(
    Transformations, bg=l2color)
Label_Header.grid(row=0, column=0, ipadx=l2padx,
                  ipady=l2pady, padx=l2padx, pady=l2pady
                  )

root.mainloop()

Thank you!谢谢你!

You haven't called pack , place , or grid on Transformations_left .您还没有在Transformations_left上调用packplacegrid

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

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