Skip to content
Advertisement

How to get only a corresponding entry box regardless of how many times I change radiobutton?

from tkinter import *

win=Tk()

var = StringVar()

l = Label(win, bg='white', width=15)

l.grid(row=17,column=1,padx=10, pady=10, sticky='w')

def print1_selection():

    if var.get()=="Number":

        lab1= Label(win, text="Enter a number").grid(row=4, column=0)

        ent1=Entry(win).grid(row=4, column=1)

        l.config(text='you have selected ' + var.get())   

    elif var.get()=="Alphabet":

        lab21= Label(win, text="Enter an alphabet").grid(row=5, column=0)

        ent21=Entry(win).grid(row=5, column=1)

        l.config(text='you have selected ' + var.get())

lbl4=Label(win, text="Select One", bg="crimson", fg="white", font=("times new 
roman",15,"bold")).grid(row=1, column=0, padx=10, pady=10, sticky='w')

r1 = Radiobutton(win, text='Number',variable=var, value='Number', command=print1_selection, width=22)

r1.grid(row=2,column=0,padx=10, pady=10)

r2 = Radiobutton(win, text='Alphabet', variable=var, value='Alphabet', command=print1_selection, width=22)

r2.grid(row=2,column=1,padx=10, pady=10)

win.mainloop()

In this code I want that when I select radiobutton number, only enter a number should appear and same for the other. But the problem is that when I select number after selecting alphabet, it shows both. I need only the selected one and eliminate the other instantly.

Advertisement

Answer

This is how I would approach this issue:

from tkinter import Tk, StringVar, Label, Frame, Entry, Radiobutton


def print1_selection():
    for widget in entry_frame.winfo_children():
        widget.destroy()

    value = var.get()
    lbl.config(text='You have selected ' + value)

    if value == "Number":
        Label(entry_frame, text="Enter a number").grid(row=0, column=0)
        Entry(entry_frame).grid(row=0, column=1)

    elif value == "Alphabet":
        Label(entry_frame, text="Enter an alphabet").grid(row=0, column=0)
        Entry(entry_frame).grid(row=0, column=1)


win = Tk()

var = StringVar(value=0)

entry_frame = Frame(win)
entry_frame.grid(row=2, column=0, columnspan=2)

lbl = Label(win, bg='white', width=20)
lbl.grid(row=3, column=0, columnspan=2, padx=10, pady=10, sticky='w')

Label(win, text="Select One", bg="crimson", fg="white", font=("times new roman", 15, "bold")).grid(row=0, column=0, padx=10, pady=10, sticky='w')
Radiobutton(win, text='Number', variable=var, value='Number', command=print1_selection, width=22).grid(row=1, column=0, padx=10, pady=10)
Radiobutton(win, text='Alphabet', variable=var, value='Alphabet', command=print1_selection, width=22).grid(row=1, column=1, padx=10, pady=10)

win.mainloop()

As You can see if You don’t plan on using the widgets instance anywhere You don’t have to assign it to a variable. Also no need to configure label in both statements since that will be done anyways so just do it at the beginning, also rows start from 0 too. Frames help with organizing widgets. Also if You want neither of the radiobuttons selected set the variable to 0.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement