Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding padding to a tkinter widget only on one side

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.

like image 469
Jack S. Avatar asked Nov 13 '10 20:11

Jack S.


People also ask

What does padding do in Tkinter?

Padding enhances the layout of the widgets in an application. While developing an application in Tkinter, you can set the padding in two or more ways. The geometry manager in Tkinter allows you to define padding (padx and pady) for every widget (label, text, button, etc).

How do I pad a label in Tkinter?

To add padding to the Tkinter label, you would need to make use of padx and pady properties. padx: Will add padding to the label vertically in the left and right. pady: Will add padding to the label horizontally at the top and bottom.

What is PADX and Pady in Tkinter?

The padx puts some space between the button widgets and between the closeButton and the right border of the root window. The pady puts some space between the button widgets and the borders of the frame and the borders of the root window.

What is add separator in Tkinter?

Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The Separator widget is used to partition the tkinter widgets such as label, buttons etc. Using this widget we can make our design more attractive and intuitive.


3 Answers

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()
like image 66
Bryan Oakley Avatar answered Oct 01 '22 03:10

Bryan Oakley


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)
like image 31
Anandakrishnan Avatar answered Oct 01 '22 04:10

Anandakrishnan


-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)) 
like image 39
Vishwamithra Avatar answered Oct 01 '22 05:10

Vishwamithra