Python 仅在一侧向 tkinter 小部件添加填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4174575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:37:36  来源:igfitidea点击:

Adding padding to a tkinter widget only on one side

pythontkinterpadding

提问by Hyman S.

How can I add padding to a tkinter window, without tkinter centering the widget? I tried:

如何在 tkinter 窗口中添加填充,而无需 tkinter 将小部件居中?我试过:

 self.canvas_l = Label(self.master, text="choose a color:", font="helvetica 12")
 self.canvas_l.grid(row=9, column=1, sticky=S, ipady=30)

and

 self.canvas_l = Label(self.master, text="choose a color:", font="helvetica 12")
 self.canvas_l.grid(row=9, column=1, rowspan=2, sticky=S, pady=30)

I want 30px padding only on the top of the label.

我只想要标签顶部的 30px 填充。

采纳答案by Bryan Oakley

The padding options padxand padyof the gridand packmethods can take a 2-tuplethat represent the left/right and top/bottom padding.

填充选项padxpadygridpack方法可利用一个2元组表示左/右和上/下填充。

Here's an example:

下面是一个例子:

import tkinter as tk

class MyApp():
    def __init__(self):
        self.root = tk.Tk()
        l1 = tk.Label(self.root, text="Hello")
        l2 = tk.Label(self.root, text="World")
        l1.grid(row=0, column=0, padx=(100, 10))
        l2.grid(row=1, column=0, padx=(10, 100)) 

app = MyApp()
app.root.mainloop()