2

I am not able to find a way to align the radio button option to the first line of the multi line text.

For e.g in the attached image i want the option 1 and option 3 to align with there respective radio buttons.

Sample Code:

import tkinter as tk


root = tk.Tk()
var = tk.IntVar()
R1 = tk.Radiobutton(root, text="Option 1 with\n multline", variable=var, value=1)
R1.pack( anchor = 'w' )

R2 = tk.Radiobutton(root, text="Option 2", variable=var, value=2)
R2.pack( anchor = 'w' )

R3 = tk.Radiobutton(root, text="Option 3 with\n multine", variable=var, value=3)
R3.pack( anchor = 'w' )

R4 = tk.Radiobutton(root, text="Option 4", variable=var, value=4)
R4.pack( anchor = 'w' )


root.mainloop()

enter image description here

Vivek
  • 89
  • 9
  • I don't think the predefined `Radiobutton` widget supports what you want to do. However, it would probably be possible define your own custom widget that did. – martineau Nov 19 '20 at 06:21

1 Answers1

2

As I said in a comment, I don't think the stock tkinter Radiobutton supports what you want to do. Fortunately it's not too difficult to craft a custom widget of your own that does.

Note that putting all the component widgets inside a Frame subclass makes the layout manager being used with it not affect the one that the user of the new class uses to position it (since normally one would want to avoid mixing them — see creating a custom widget in tkinter for more details).

import tkinter as tk


class MyRadioButton(tk.Frame):
    def __init__(self, parent, text, variable, value):
        tk.Frame.__init__(self, parent)

        self.rbtn = tk.Radiobutton(self, variable=variable, value=value)
        self.rbtn.grid(row=0, column=0, sticky="nw")

        self.label = tk.Label(self, text=text, anchor="w", justify="left")
        self.label.grid(row=0, column=1, sticky="nw")


if __name__ == '__main__':

    root = tk.Tk()
    var = tk.IntVar()
    R1 = MyRadioButton(root, text="Option 1 with\n multline", variable=var, value=1)
    R1.pack(anchor='w')

    R2 = MyRadioButton(root, text="Option 2", variable=var, value=2)
    R2.pack(anchor='w')

    R3 = MyRadioButton(root, text="Option 3 with\n multine", variable=var, value=3)
    R3.pack(anchor='w')

    R4 = MyRadioButton(root, text="Option 4", variable=var, value=4)
    R4.pack(anchor='w')

    root.mainloop()

Result:

screenshot showing custom radiobutton widget in action

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I am not sure whether to ask a new question, but how can i make this custom widget more generic ? In the sense i would take multiple inputs from user just like normal widget and then pass it to the actual checkbutton ? – Vivek Dec 01 '20 at 14:15
  • Vivek: I don't understand what you mean by "more generic" and about how a "normal widget" works — so you probably need to ask a new question and show your own attempt(s) at trying do it. – martineau Dec 01 '20 at 19:05
  • Vivek: Did you mean the actual radiobutton, not the "actual checkbutton"? – martineau Dec 01 '20 at 20:25