'How do I align the radiobuttons to left in tkinter?
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)
Solution 1:[1]
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)
Solution 2:[2]
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
Solution 3:[3]
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 apllication.
Solution 4:[4]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | akash |
Solution 2 | |
Solution 3 | raven_seven |
Solution 4 | John Wang |