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

发布于 2024-10-02 11:28:08 字数 429 浏览 3 评论 0原文

如何向 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)

并且

 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)

我只想在标签顶部有 30 像素的填充。

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

 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡间太子 2024-10-09 11:28:08

gridpack 方法的填充选项 padxpady 可以采用 2-tuple< /strong> 代表左/右和上/下填充。

这是一个例子:

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()

The padding options padx and pady of the grid and pack methods can take a 2-tuple that represent the left/right and top/bottom padding.

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()
深海蓝天 2024-10-09 11:28:08

有多种方法可以使用 placegrid 甚至 pack 方法。

示例代码:

from tkinter import *
root = Tk()

l = Label(root, text="hello" )
l.pack(padx=6, pady=4) # where padx and pady represent the x and y axis respectively
# well you can also use side=LEFT inside the pack method of the label widget.

要根据列和行放置小部件,请使用网格方法:

but = Button(root, text="hello" )
but.grid(row=0, column=1)

There are multiple ways of doing that you can use either place or grid or even the packmethod.

Sample code:

from tkinter import *
root = Tk()

l = Label(root, text="hello" )
l.pack(padx=6, pady=4) # where padx and pady represent the x and y axis respectively
# well you can also use side=LEFT inside the pack method of the label widget.

To place a widget to on basis of columns and rows , use the grid method:

but = Button(root, text="hello" )
but.grid(row=0, column=1)
亣腦蒛氧 2024-10-09 11:28:08
-pady {10,0}

这样你就可以将顶部的填充设为 10,将下方的填充设为 0。

在 python 代码中,这可能如下所示:

l = Label(root, text="hello" )
l.pack(pady=(10, 0)) 
-pady {10,0}

this way you are mentioning padding on top as 10 and below as 0.

In python code, this might look like:

l = Label(root, text="hello" )
l.pack(pady=(10, 0)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文