Unwanted iteration in tkinter result

Viewed 40

Iam trying to obtain method of payment from user through tkinter radiobuttons as follows:

payment_method_label=Label(root,text="Select Payment Method:")
payment_method=StringVar()
payment_method.set("card")
cards=Radiobutton(root,text="Debit/Credit Card",variable=payment_method,value="card",anchor='w')
wallet=Radiobutton(root,text="Payment Wallet",variable=payment_method,value="wallet",anchor='w')
netbanking=Radiobutton(root,text="Net Banking",variable=payment_method,value="net banking",anchor='w')

Here's the result: Payment methods

You can clearly see that there is unforeseen iteration (the radiobuttons are going in a slanting order)

As you can see from the code, I tried using the anchor="w", but that doesn't make any difference.

Can anyone please pinpoint whats going wrong here?

All help is appreciated Thanks!

2 Answers
from tkinter import *
import tkinter as tk

root = Tk()

payment_method_label=Label(root,text="Select Payment Method:")
payment_method=StringVar()
payment_method.set("card")
cards=Radiobutton(root,text="Debit/Credit Card",variable=payment_method,value="card").pack(anchor=tk.W)
wallet=Radiobutton(root,text="Payment Wallet",variable=payment_method,value="wallet").pack(anchor=tk.W)
netbanking=Radiobutton(root,text="Net Banking",variable=payment_method,value="net banking").pack(anchor=tk.W)

root.mainloop()

Use .pack(anchor=tk.W) at the end of each radiobutton. You don't this anchor='w' parameter for the radiobutton.

enter image description here

I guess this was the output you expected.

Grid Method

from tkinter import *
import tkinter as tk

root = Tk()

payment_method_label=Label(root,text="Select Payment Method:")
payment_method=StringVar()
payment_method.set("card")
cards=Radiobutton(root,text="Debit/Credit Card",variable=payment_method,value="card").grid(row=1, column=1, sticky="w")
wallet=Radiobutton(root,text="Payment Wallet",variable=payment_method,value="wallet").grid(row=2, column=1, sticky="w")
netbanking=Radiobutton(root,text="Net Banking",variable=payment_method,value="net banking").grid(row=3, column=1, sticky="w")

root.mainloop()

Use sticky="W"

When you use anchor parameter while creating the radiobutton, the anchor parameter determines where the radiobutton will be, if it occupies a space larger than it needs. For instance:

tk.Radiobutton(root,text="foo",width=100,anchor="e")

will create a radiobutton which is 100 characters large and the text will be attached to east.

In your case it is not what you want. You want your radiobutton to be attached to the west side of the column. Thus, you need to use the sticky parameter with .grid().

So, let's get back to your example, what you need to do is:

cards=Radiobutton(root,text="Debit/Credit Card",variable=payment_method,value="card")
wallet=Radiobutton(root,text="Payment Wallet",variable=payment_method,value="wallet")
netbanking=Radiobutton(root,text="Net Banking",variable=payment_method,value="net banking")

cards.grid(row=0,column=0,sticky="w")
wallet.grid(row=1,column=0,sticky="w")
netbanking.grid(row=2,column=0,sticky="w")
Related