raven_seven
raven_seven

Reputation: 19

How do I align the radiobuttons to left in tkinter?

enter image description here

My code shows the radiobuttons unaligned , tap picture to see, and I want to define var(1,2,3,4,5) in some better way.

I am very new to programming but I have tried using .pack(), side,anchor and justify.

var1 = tk.StringVar()
var2 = tk.StringVar()
var3 = tk.StringVar()
var4 = tk.StringVar()
var5 = tk.StringVar()
var6 = tk.StringVar()
var7 = tk.StringVar()
var8 = tk.StringVar()

v = StringVar() 
rbutton=Radiobutton(tab2, text="ggdsh", variable=var1, value="1",width=15,justify=LEFT).grid(column=0,row=1,sticky=W)
rbutton2=Radiobutton(tab2, text="fjkhslafjksh", variable=var2, value="1",width=15,justify=LEFT).grid(column=0,row=2)
rbutton3=Radiobutton(tab2, text="sdjklfhsdfj", variable=var3, value="1",width=15,justify=LEFT).grid(column=0,row=3)
rbutton4=Radiobutton(tab2, text="skjlskhsdlgkjshgklsd", variable=var4, value="1",width=15,justify=LEFT).grid(column=0,row=4)
rbutton5=Radiobutton(tab2, text="sajfkshldjfhlsf", variable=var5, value="1",width=15,justify=LEFT).grid(column=0,row=5)
rbutton6=Radiobutton(tab2, text="sdfkj;kjfdlgh", variable=var6, value="1",width=15,justify=LEFT).grid(column=0,row=6)
rbutton7=Radiobutton(tab2, text="dsfjhkldsghsdghgfdsgdg", variable=var7, value="1",width=15,justify=LEFT).grid(column=0,row=7)
rbutton8=Radiobutton(tab2, text="gsjhdgjkdshl", variable=var8, value="1",width=15,justify=LEFT).grid(column=0,row=8)

Upvotes: 0

Views: 8015

Answers (6)

Mohamed Azimal
Mohamed Azimal

Reputation: 1

val_options = [
    "val1",
    "val2",
    "val3",
    "val4",
    "val5",
    "val6",
    "val7",
    "val8",
]

val_var = tk.StringVar()

for idx, val_option in enumerate(val_options):
    tk.Radiobutton(root, text=val_option, variable=val_var, value=val_option).grid(row=idx + 1, columnspan=2, sticky=tk.W)

Upvotes: 0

raven_seven
raven_seven

Reputation: 19

rbutton=Radiobutton(tab2, text="ggdsh", variable=var1, value="1").grid(column=0,row=1,sticky=tk.W)

This does the job for me. turns out the problem was specifying the radiobutton width and mixing .pack() and .grid() in my application.

Upvotes: 0

toyota Supra
toyota Supra

Reputation: 4568

I want to define var(1,2,3,4,5) in some better way.

No need to add the Radiobutton parameter width=15, justify=LEFT

Easier way to use iterating to reduce widgets, items. and StringVar.

Snippet:

import tkinter as tk


tab2 = tk.Tk()
 
v = tk.StringVar(tab2, "1")
 
# Dictionary to create multiple buttons
values = {"ggdsh" : "1",
          "fjkhslafjksh" : "2",
          "sdjklfhsdfj" : "3",
          "sajfkshldjfhlsf" : "4",
          "sdfkj;kjfdlgh": "5",
          "dsfjhkldsghsdghgfdsgdg": "6",
          "gsjhdgjkdshl": "7"}

for (text, value) in values.items():
    tk.Radiobutton(tab2, text=text, variable=v,
                   value=value).grid(column=0, row=value, sticky=tk.W)

tab2.mainloop()

Screenshot:

enter image description here

Upvotes: 0

John Wang
John Wang

Reputation: 1

root = Tk()
root.geometry("600x400")
root.title("Radio Button")
colorVar = StringVar()
colorVar.set('#FF0000')
bgColors = [("blue", '#0000FF'),("red", '#FF0000'),("yellow", '#FFFF00'),("cyan", '#00FFFF'),("gray", "#888888")]
frame = Frame(root, width=30, height=70)
frame.pack()
for text, color in bgColors:
    Radiobutton(frame, text=text, variable=colorVar, value=color).pack(anchor=W)

Upvotes: 0

akash
akash

Reputation: 829

Just use .pack(anchor=W)

Radiobutton(master=frame,text='Easy',padx = 20,variable=level,value=0,command=lambda : self.settings.setLevel(level.get())).pack(anchor=W)

Upvotes: 4

Aaron
Aaron

Reputation: 73

Use loops for all things that repeat. They will create the buttons for you in just a few lines of code. A screenshot would have been nice. It's hard for me to grasp what you are trying to accomplish.

Thats how you would position a lable on the left side with pack:

w = Label(root, text="Blue", bg="blue", fg="white")
w.pack(side=LEFT)

Using Grid you would put them in the first column

Upvotes: 0

Related Questions