小部件的tkinter放置 [英] tkinter placement of widgets
问题描述
考虑以下简单代码:
from tkinter import *
mainFrame = Frame(parent, bd=5, bg="yellow").pack(expand=True,fill=BOTH)
frameA = Frame(mainFrame, bd=5, bg="green").place(anchor=NW, relx=0, rely=0 , relwidth=1.0, height=40)
my_label = Label(mainFrame, text="Hello world ", bg="red").place(in_=frameA, relx=0, anchor=NW, rely=0, relwidth=0.5)
frameB = Frame(frameA, bd=5, bg="gold").place(in_=frameA , anchor=N, relx=0.5, rely=0.8, width=30, height=50)
结果如下所示:
黄色框很有意义,这正是我所期望的. frameA(绿色)和my_label也是如此.
但是,frameB拒绝执行我期望的操作,最初我将其== 1.0作为其place命令,并且我期望将其N(顶部中央边缘)放置在底部的水平中点(1.0 *高度)上我已将其设为父级并用作.place()命令_in参数的绿色框.
似乎不管我在frameB的实例化中使用的根参数还是所说的in_参数,frameB的依赖关系始终与大型机或根保持在一起.
我知道我可以使用.pack(),但是我试图弄清楚.place()的行为,所以请不要让我不要同时使用.pack()和.grid().>
我做错了什么?还是(更糟糕的)误会?
pack()
,grid()
和place()
返回None
,因此,分配frameA = Frame(bla bla).place(bla)
就是将None
分配给frameA
您将需要分两步执行此操作以获取所需的内容:
- 首先创建窗口小部件.
- 第二个地方.
遵循以下原则:
mainFrame = Frame(parent, bd=5, bg="yellow")
mainFrame.pack(expand=True,fill=BOTH)
frameA = Frame(mainFrame, bd=5, bg="green")
frameA.place(anchor=NW, relx=0, rely=0 , relwidth=1.0, height=40)
my_label = Label(mainFrame, text="Hello world ", bg="red")
my_label.place(in_=frameA, relx=0, anchor=NW, rely=0, relwidth=0.5)
frameB = Frame(frameA, bd=5, bg="gold")
frameB.place(in_=frameA , anchor=N, relx=0.5, rely=0.8, width=30, height=50)
Consider the following simple code:
from tkinter import *
mainFrame = Frame(parent, bd=5, bg="yellow").pack(expand=True,fill=BOTH)
frameA = Frame(mainFrame, bd=5, bg="green").place(anchor=NW, relx=0, rely=0 , relwidth=1.0, height=40)
my_label = Label(mainFrame, text="Hello world ", bg="red").place(in_=frameA, relx=0, anchor=NW, rely=0, relwidth=0.5)
frameB = Frame(frameA, bd=5, bg="gold").place(in_=frameA , anchor=N, relx=0.5, rely=0.8, width=30, height=50)
The result looks like this:
The yellow frame makes sense and is what I expected. The same goes for frameA (green) and my_label.
However, frameB refuses to do what I expect, originally I have rely=1.0 for its place command and I was expecing it's N (top center edge) to be placed at the horizontal midpoint of the bottom (1.0*height) of the green frame which I have set up to be both its parent and used as it .place() command _in parameter.
It would just seem that rely of frameB keeps being placed w.r.t mainframe or root regardless of the root parameter I use for the instantiation of frameB or the said in_ parameter.
I know that I can use .pack(), but I am trying to figure out the behavior of .place() so please don't send me to use neither .pack() nor .grid().
What am I doing wrong? or (worse) miss-understanding ?
pack()
, grid()
, and place()
return None
, therefore, assigning frameA = Frame(bla bla).place(bla)
, is assigning None
to frameA
You will need to do this in two steps to get what you need:
- First create the widget.
- Second place it.
Something along these lines:
mainFrame = Frame(parent, bd=5, bg="yellow")
mainFrame.pack(expand=True,fill=BOTH)
frameA = Frame(mainFrame, bd=5, bg="green")
frameA.place(anchor=NW, relx=0, rely=0 , relwidth=1.0, height=40)
my_label = Label(mainFrame, text="Hello world ", bg="red")
my_label.place(in_=frameA, relx=0, anchor=NW, rely=0, relwidth=0.5)
frameB = Frame(frameA, bd=5, bg="gold")
frameB.place(in_=frameA , anchor=N, relx=0.5, rely=0.8, width=30, height=50)
这篇关于小部件的tkinter放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!