为什么tkinter.grid()方法中的"ipady"选项仅在小部件下方添加空间? [英] Why does the 'ipady' option in the tkinter.grid() method only add space below a widget?

查看:59
本文介绍了为什么tkinter.grid()方法中的"ipady"选项仅在小部件下方添加空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是显示问题的代码. Label1 显示我需要使用 pady Label1 位移10个像素. Label2 显示了 ipady 的默认效果.为什么 ipady 在小部件上方和下方不留空格?我如何让 ipady 进行使用 pady 进行的假设操作?谢谢.

Below is a code to show the issue. Label1 shows that I need to use pady to displace Label1 by 10 pixels. Label2 shows the default effect of ipady. Why does ipady not leave space above and below the widgets? How do I get ipady to do what it is suppose to do w/o using pady? Thank you.

引用 tcl文档:

-ipady金额

-ipady amount

该数量指定要在从站的顶部和底部保留多少垂直内部填充.此空间已添加在奴隶边界内.数量默认为0.

The amount specifies how much vertical internal padding to leave on the top and bottom of the slave(s). This space is added inside the slave(s) border. The amount defaults to 0.

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

s = ttk.Style()
s.configure('one.TFrame', background='light blue', relief='raised',borderwidth=10)
s.configure('two.TFrame', background='light green', relief='sunken', borderwidth=10)

frame1 = ttk.Frame(root,style='one.TFrame')
frame1.grid(row=0, column=0, sticky='nsew', ipady=10,)

label1 = ttk.Label(frame1, text='Label1')
label1.grid(sticky='nsew',padx=10, pady=10,)

frame2 = ttk.Frame(frame1,style='two.TFrame')
frame2.grid(row=1, column=1, sticky='nsew', ipady=50,)

label2 = ttk.Label(frame2, text='Label2')
label2.grid()

推荐答案

我认为 ipady 并不完全符合您的想法.诚然,在这一点上文档尚不清楚.它不会添加相当于内部边距的值.取而代之的是,它只是向小部件的高度添加了许多像素,并且像素被添加在边框的内部而不是外部.它不会阻止"这些像素被使用.

I don't think ipady quite does what you think it does. Admittedly, the documentation is a bit unclear on this point. It doesn't add the equivalent of an interior margin. Instead, it simply adds that many pixels to the height of the widget, and the pixels are added on the inside of the border rather than the outside. It doesn't "block off" these pixels from being used.

如果您的框架通常为20像素高,且 ipady = 50 ,则您的小部件现在将为120像素高(50 * 2 + 20).在此框架内添加的任何小部件都可以使用框架内的所有空间.因此,当您使用 grid 将某物放置在第零行时,它仍将出现在框架的顶部,紧挨边框的下方.

If your frame normally would be 20 pixels tall, with ipady=50, your widget will now be 120 pixels tall (50*2 + 20). Any widgets added inside of this frame are able to use all of the space inside the frame. Thus, when you use grid to place something in row zero, it will still appear at the very top of the frame, just below the border.

这正是我在您的代码中看到的.在我的机器上,没有 ipady = 50 的frame2最终高20像素.当我添加 ipady = 50 时,框架的高度变为120像素.由于标签已放置在第0行中,因此该标签显示在框架的顶部.

This is exactly what I see with your code. On my machine, frame2 without ipady=50 ends up being 20 pixels tall. When I add ipady=50, the frame becomes 120 pixels tall. The label appears at the very top of the frame since it has been placed in row zero.

TL; DR:不要以为ipady是在说我需要这么多的内部空白",而是我需要更多的内部空白".

TL;DR: don't think of ipady as saying "I need this much of an inside margin", but rather "I need this much more extra space inside".

这篇关于为什么tkinter.grid()方法中的"ipady"选项仅在小部件下方添加空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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