简体   繁体   English

Python Tkinter 放置框架和 treeview 在笔记本内

[英]Python Tkinter placing frame and treeview within a notebook

I am working on a very simple TKinter layout.我正在研究一个非常简单的 TKinter 布局。 It is comprised of a notebook with two tabs.它由一个带有两个选项卡的笔记本组成。 In the first tab I have 1 Frames and inside that 3 more.在第一个选项卡中,我有 1 个 Frames,而在其中还有 3 个。 This works fine and can be seen below:这工作正常,可以在下面看到:

带 3 相框的笔记本

However, when I try to add a Treeview to the left most red box (confusingly called top_frame).但是,当我尝试将 Treeview 添加到最左侧的红色框(混淆地称为 top_frame)时。 It just sits underneath.它只是坐在下面。

Like the below:像下面这样:

带树视图

I think I have the parent child relationships correct, so can not see why it is not sitting as intended.我想我有正确的父子关系,所以看不出它为什么不按预期坐着。

Any help welcome.欢迎任何帮助。 My code is below and I need the result to place the treeview in to the red box.我的代码在下面,我需要将 treeview 放入红色框中的结果。 I have spent a long time trying to fix this having started out using grid() within the notebook but still no luck even when switching to pack()我花了很长时间试图解决这个问题,开始在笔记本中使用 grid() 但即使切换到 pack() 仍然没有运气

from tkinter import *
from tkinter import ttk

root = Tk()

tabControl = ttk.Notebook(root) 
  
tab1 = ttk.Frame(tabControl) 

tab2 = ttk.Frame(tabControl) 
  
tabControl.add(tab1, text ='Tab 1') 
tabControl.add(tab2, text ='Tab 2') 
tabControl.pack()




top_frame1 = Frame(tab1, bg='red', width = 450, height=400, relief=SUNKEN).pack(side=LEFT, fill = BOTH)
Mid_frame = Frame(tab1, bg='Orange', width = 450, height=100, relief=SUNKEN).pack(side=RIGHT, fill = BOTH)
bottom_frame = Frame(tab1, bg='green', width = 450, height=100, relief=SUNKEN).pack(side=RIGHT, fill = BOTH)

treeview_menu = ttk.Treeview(top_frame1)
treeview_menu.pack()
treeview_menu.insert('','0','m1', text = 'department 1')
treeview_menu.insert('','1','m2', text = 'department 2')
treeview_menu.insert('','2','m3', text = 'department 3')
treeview_menu.insert('','3','m4', text = 'department 4')
treeview_menu.insert('','4','m5', text = 'department 5')

treeview_menu.insert('m1','end','cs1', text = 'Staff Mbr1')
treeview_menu.insert('m1','end','cs2', text = 'Staff Mbr2')

treeview_menu.insert('m2','end','bc1', text = 'Staff Mbr1')
treeview_menu.insert('m2','end','bc2', text = 'Staff Mbr2')

treeview_menu.insert('m3','end','av1', text = 'Staff Mbr1')
treeview_menu.insert('m3','end','av2', text = 'Staff Mbr2')

treeview_menu.insert('m4','end','mc1', text = 'Staff Mbr1')
treeview_menu.insert('m4','end','mc2', text = 'Staff Mbr2')

treeview_menu.insert('m5','end','ss1', text = 'Staff Mbr1')
treeview_menu.insert('m5','end','ss2', text = 'Staff Mbr2')

Since I just ran into this and there still is not an accepted answer.因为我刚刚遇到这个问题,但仍然没有一个可以接受的答案。

top_frame1 = Frame(tab1, bg='red', width = 450, height=400, relief=SUNKEN).pack(side=LEFT, fill = BOTH)
Mid_frame = Frame(tab1, bg='Orange', width = 450, height=100, relief=SUNKEN).pack(side=RIGHT, fill = BOTH)
bottom_frame = Frame(tab1, bg='green', width = 450, height=100, relief=SUNKEN).pack(side=RIGHT, fill = BOTH)

.pack does not return the object. .pack 不返回 object。 Since these are all on the one line the variable is never actually set.由于这些都在一行上,因此从未实际设置变量。 Thus bottom_frame, mid_frame, and top_frame all return None.因此,bottom_frame、mid_frame 和 top_frame 都返回 None。

The variable must be set from the Frame() function and then call the pack() function.必须从 Frame() function 设置变量,然后调用 pack() function。

top_frame1 = Frame(tab1, bg='red', width = 450, height=400, relief=SUNKEN)
top_frame1.pack(side=LEFT, fill = BOTH)
Mid_frame = Frame(tab1, bg='Orange', width = 450, height=100, relief=SUNKEN)
Mid_frame.pack(side=RIGHT, fill = BOTH)
bottom_frame = Frame(tab1, bg='green', width = 450, height=100, relief=SUNKEN)
bottom_frame.pack(side=RIGHT, fill = BOTH)

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

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