Boko
Boko

Reputation: 1

tkinter - How to adjust text position relative to checkbox image?

Is it possible to move the checkbox description closer to the checkbox?

CheckBox

character_set = LabelFrame(text=" Example ")

checkbtn0 = PhotoImage(file="example_off.png")
checkbtn1 = PhotoImage(file="example_on.png")

checktestbox = IntVar()

checktest = Label(character_set, image=checkbtn0)
checktest.bind("<Button-1>", Custom)
checktest.grid(padx=8, pady=4, row=0, column=0)

checktext = Label(character_set, text="Upper case (A-Z)")
checktext.grid(row=0, column=1)

character_set.pack(fill="both", expand="yes", padx=4, pady=4)

Upvotes: 0

Views: 1805

Answers (2)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

You are using

checktest.grid(padx=8, pady=4, row=0, column=0)

this will add 8 pixels to the left and right of your Label.

To reduce or even set to 0 use this instead:

checktest.grid(padx=(8,0), pady=4, row=0, column=0)

This allows different values to padding left and right.

padx(a,b)

  • a is the padding-left
  • b is the padding-right

I'm gonna to say padx is simliar to padding-left and padding-right in HTML.
If you remove padx at all, the padding-left and padding-right will be zero.

Upvotes: 1

stovfl
stovfl

Reputation: 15513

Question: adjust text position besides checkbox

Relevant:


Reference:

  • The Tkinter Label Widget - compound=

    compound=
    Controls how to combine text and image in the label. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text.

  • The Tkinter Place Geometry Manager

    place(**options)
    Used in specialized cases, most importantly, by compound widgets to position.

  • The Tkinter Checkbutton Widget

    Checkbutton(parent, text=<str>, variable=<tk.VariableClasses>)
    The checkbutton widget is used to choose between two distinct values (usually switching something on or off).


  1. Compound your own Checkbutton widget, using Frame, Label

    class Checkbutton(tk.Frame):
        off_image = PhotoImage(...
        on_image = PhotoImage(...
    
        def __init__(self, parent, **kwargs):
            text = kwargs.pop('text', None)
            self.callback = kwargs.pop('callback', None)
            super().__init__(parent, **kwargs)
    
            self.chkbox = tk.Label(parent, image=Checkbutton.off_image)
            self.chkbox.grid(row=0, column=0)
            self.chkbox.bind("<Button-1>", self.on_click)
    
            self.label = tk.Label(parent, text=text)
            self.chkbox.grid(row=0, column=1)
    
        def on_click(self, event):
            # Implements exchanging image
            pass
    
    chkbtn = Checkbutton(root, text="Upper case (A-Z)", callback=Custom)
    chkbtn.grid(padx=8, pady=4, row=0, column=0)
    
  2. Using Label(..., compound='left')

    checktext = Label(root, image=checkbtn0, text="Upper case (A-Z)", compound='left')
    
  3. Using .place(x, y)

    checktest = Label(root, image=checkbtn0)
    checktest.place(x=..., y=...)
    
    checktext = Label(root, text="Upper case (A-Z)")
    checktext.place(x=..., y=...)
    
  4. Inherit from class tk.Checkbutton, use your own images
    See this answer: checkbutton different image

    class Checkbutton(tk.Checkbutton):
        off_image = PhotoImage(...
        on_image = PhotoImage(...
    
        def __init__(self, parent, **kwargs):
            self.var = tk.IntVar()
            super().__init__(parent, 
                             image=Checkbutton.off_image, 
                             selectimage=Checkbutton.on_image, 
                             indicatoron=False,
                             onvalue=1, offvalue=0, variable=self.var,
                             **kwargs)
    

Upvotes: 0

Related Questions