当我将标签关联到它时,Tkinter 框架背景消失 [英] Tkinter Frame background disappear when I associate a Label to it

查看:24
本文介绍了当我将标签关联到它时,Tkinter 框架背景消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Tkinter 中制作 GUI.但是当我在任何 Frame 中关联一个 Label 时,它的背景颜色就会消失.而我希望标签的整个背景都是绿色的.

I am trying to make a GUI in Tkinter. But when I associate a Label in any Frame, its background color disappers. Whereas, I expect this the whole background of the label to be green.

我的代码粘贴在下面

from Tkinter import *
from time import *

root = Tk()
root.rowconfigure(1, weight=1)
root.columnconfigure(1, weight=1)

leftFrame = Frame(root, width = 200, height = 200, bg = 'green')
leftFrame.grid(row = 1, column = 1)

centerFrame = Frame(root, width = 200, height = 200, bg = 'black')
centerFrame.grid(row = 1, column = 2)

rightFrame = Frame(root, width = 200, height = 200, bg = 'blue')
rightFrame.grid(row = 1, column = 3) 

b = Label(leftFrame, text = "Press me!", bg = 'green')
b.pack(side = TOP, expand = YES)

root.mainloop()

推荐答案

除非对框架施加约束,否则框架通常会缩小以适应"其内容.这是正常的、预期的并且通常是可取的.因此,左边的框架会要求它的宽度刚好适合标签,高度刚好适合标签.其他列中的框架,因为它们没有子项,将是您请求的大小.

Frames typically "shrink to fit" their contents unless there's a constraint put on the frame. This is normal, expected, and usually desirable. So, the left frame will request that it is just wide enough to fit the label, and just tall enough to fit the label. The frames in the other columns, because they have no children, will be the size that you request.

因此,您有一列想要与标签一样宽和一样高,另外两列想要 200 像素宽.您有一行想要足够高以容纳所有内容,这意味着它与最高的小部件一样高,或 200 像素.这最终会在最左边的列中留下额外的空间.

So, you have one column that wants to be as wide and as tall as the label, and two other columns that want to be 200 pixels wide. You have one row that wants to be tall enough to fit everything, which means it is as tall as the tallest widget, or 200 pixels. This ends up leaving extra space in the leftmost column.

不指定任何选项,每行将与行中最高的小部件一样高,在本例中为 200 像素.每列将与最宽的小部件一样宽.最左边的列希望是框架的宽度,它本身只希望与标签一样宽.因此,最左边的列将仅与标签一样宽.其他列的宽度为 200 像素,因为这是您告诉它们的宽度.

Without specifying any options, each row will be as tall as the tallest widget in the row, in this case 200 pixels. Each column will be as wide as the widest widget. The leftmost column wants to be the width of the frame, which itself wants to be only as wide as the label. So, the leftmost column will be only as wide as the label. The other columns will be 200 pixels wide because that's what you told them to be.

现在,您希望绿色框架填充它所在的单元格.在您的情况下,您没有要求框架填充分配的空间,以便缩小以适应.您需要将 sticky 选项添加到框架中,以强制它粘"到给定空间的所有四个边缘.

Now, you want the green frame to fill the cell that it is in. In your case you haven't asked that the frame fill the space allotted so it shrinks to fit. You need to add the sticky option to the frame to force it to "stick" to all four edges of the space it was given.

leftFrame.grid(row=1, column=0, sticky="nsew")

当然,假设您打算将小部件放入其他框架中,您也会遇到相同的问题.

Of course, you'll have the same problems with the other frames as well, assuming you plan to put widgets in them.

这篇关于当我将标签关联到它时,Tkinter 框架背景消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆