Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
TK python checkbutton RTL

I have a checkbutton:

from tkinter import *
master = Tk()
Checkbutton(master, text="Here...").grid(row=0, sticky=W)
mainloop()

Which looks like this:

enter image description here

I tried to move the checkbutton to the other side (to support RTL languages), so it'll be like:

Here...[]

I know that I can draw a label next to the checkbutton, but this way clicking the text won't effect the checkbutton.

How can I do it?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can bind the left mouse button click event of the label, to a lambda construct that toggles the checkbutton -:

label.bind("<Button-1>", lambda x : check_button.toggle())

The label can then be placed before the checkbutton using grid(as mentioned in the OP at the end) -:

from tkinter import *

master = Tk()

l1 = Label(master, text = "Here...")
cb = Checkbutton(master)
l1.grid(row = 0, column = 0)
cb.grid(row = 0, column = 1, sticky=W)

l1.bind("<Button-1>", lambda x : cb.toggle())
mainloop()

This will toggle, the checkbutton even if the label is clicked.

OUTPUT -:

OUTPUT


NOTE:

The checkbutton, has to now be fetched as an object(cb), to be used in the lambda construct for the label's bind function callback argument. Thus, it is gridded in the next line. It is generally a good practice to manage the geometry separately, which can prevent error such as this one.


Also, as mentioned in the post linked by @Alexander B. in the comments, if this assembly is to be used multiple times, it can also be made into a class of it's own that inherits from the tkinter.Frame class -:

class LabeledCheckbutton(Frame):
    def __init__(self, root, text = ""):
        Frame.__init__(self, root)
        self.checkbutton = Checkbutton(self)
        self.label = Label(self, text = text)
        self.label.grid(row = 0, column = 0)
        self.checkbutton.grid(row = 0, column = 1)
        self.label.bind('<Button-1>', lambda x : self.checkbutton.toggle())
        return
    
    pass

Using this with grid as the geometry manager, would make the full code look like this -:

from tkinter import *

class LabeledCheckbutton(Frame):
    def __init__(self, root, text = ""):
        Frame.__init__(self, root)
        self.checkbutton = Checkbutton(self)
        self.label = Label(self, text = text)
        self.label.grid(row = 0, column = 0)
        self.checkbutton.grid(row = 0, column = 1)
        self.label.bind('<Button-1>', lambda x : self.checkbutton.toggle())
        return
    
    pass

master = Tk()
lcb = LabeledCheckbutton(master, text = "Here...")
lcb.grid(row = 0, sticky = W)

mainloop()

The output of the above code remains consistent with that of the first approach. The only difference is that it is now more easily scalable, as an object can be created whenever needed and the same lines of code need not be repeated every time.

over 4 years ago · Santiago Trujillo Relatório

0

You can try using ttk.Checkbutton which you can change its layout using ttk.Style().layout(...):

import tkinter as tk
from tkinter import ttk

master = tk.Tk()

s = ttk.Style()
# create a custom Checkbutton style with reverse order of label and indicator
s.layout('right.TCheckbutton',
   [('Checkbutton.padding', {'sticky': 'nswe', 'children': [
      ('Checkbutton.focus', {
         'side': 'left', 'sticky': 'w', 'children': [('Checkbutton.label', {'sticky': 'nswe'})]
      }),
      ('Checkbutton.indicator', {'side': 'left', 'sticky': ''})
   ]})]
)

ttk.Checkbutton(master, text='Here...', style='right.TCheckbutton').grid(padx=20, pady=10)

master.mainloop()

Result:

enter image description here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda