0
    from tkinter import *
    from tkinter import ttk
    win=Tk()
    Ent12=StringVar()
    wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
    wrapper.place(x=0, y=80, width=465, height=625)
    def print1_selection():
        for widget in entry_frame.winfo_children():
            widget.destroy()
        for widget in entry_frame2.winfo_children():
            widget.destroy()
        value = var1.get()

        if value == "Alphabet":
            label12=Label(entry_frame, text="Alphabet", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=10, pady=5, sticky='w')
            Ent12 = Entry(entry_frame, width=20)
            Ent12.grid(row=6, column=1,padx=10, pady=5, sticky='w')

        elif value == "Number":
            label13=Label(entry_frame2, text="Number", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=10, pady=5, sticky='w')
            Ent12 = Entry(entry_frame2, width=20)
            Ent12.grid(row=7, column=1,padx=10, pady=5, sticky='w')
            

    var1 = StringVar(value=0)

    entry_frame = Frame(wrapper, bg="crimson")
    entry_frame.grid(row=8, column=0, columnspan=2,padx=10, pady=5, sticky='w')

    entry_frame2 = Frame(wrapper, bg="crimson")
    entry_frame2.grid(row=9, column=0, columnspan=2,padx=10, pady=5, sticky='w')
    

    def lookup():
        print(Ent12.get())
        

    lbl4=Label(wrapper, text="Find", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=5, column=0, padx=10, pady=10, sticky='w')
    r1 = Radiobutton(wrapper, text='Alphabet',variable=var1, value='Alphabet', command=print1_selection, width=22).grid(row=6,column=0,padx=10, pady=10, sticky='w')
    r2 = Radiobutton(wrapper, text='Number', variable=var1, value='Number', command=print1_selection, width=22).grid(row=6,column=1,padx=10, pady=10, sticky='w')

    btn = Button(wrapper, text = 'Find',command=lookup , bd = '5', width=15, height=2)
    btn.grid(row=10, column=1, padx=20, pady=10)

    win.mainloop()

In this code, I want to print values of alphabet or number whatever is input depending on radiobutton. Moreover, is it OK if I have given both entries same name i.e.Ent12 ? This command

def lookup():
     print(Ent12.get())

gives nothing in output.

Ricky
  • 69
  • 1
  • 7
  • Look at [this](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – TheLizzard May 02 '21 at 11:23
  • @TheLizzard tried it but no progress – Ricky May 02 '21 at 11:30
  • Can you please update the code in the question with the suggestion? – TheLizzard May 02 '21 at 11:32
  • @TheLizzard done, kindly have a look – Ricky May 02 '21 at 11:38
  • You still have the same problem with `label12`, `label13`, `lbl4`, `r1`, and `r2`. Also I think I figured out the problem. – TheLizzard May 02 '21 at 11:41
  • @TheLizzard thnxx the problem solved. Can u have a look at this question also and shed some light? https://stackoverflow.com/questions/67346170/how-to-import-specific-cell-values-from-excel-by-giving-input-from-tkinter-dropd – Ricky May 02 '21 at 14:57
  • About that other question: Sorry but I don't know anything about `openpyxl`. I also couldn't find any problems with how you handled `tkinter`. I will look at the code again but I really don't think the problem is with `tkinter`. – TheLizzard May 02 '21 at 15:06

1 Answers1

3

The print(Ent12.get()) gets the value of the StringVar that you defined at the start: Ent12 = StringVar(). And that StringVar doesn't have anything to do with Ent12 = Entry(...). To fix the problem you will need to make the Ent12 variable global in print1_selection like this:

from tkinter import *
from tkinter import ttk

win = Tk()
wrapper = Frame(win, bd=4, relief="ridge", bg="crimson")
wrapper.pack()

def print1_selection():
    global Ent12, Ent13 # Make the variables global
    for widget in entry_frame.winfo_children():
        widget.destroy()
    for widget in entry_frame2.winfo_children():
        widget.destroy()
    value = var1.get()

    if value == "Alphabet":
        label12 = Label(entry_frame, text="Alphabet", bg="crimson", fg="white",
                        font=("times new roman", 15, "bold"))
        label12.grid(row=6, column=0, padx=10, pady=5, sticky="w")
        Ent12 = Entry(entry_frame, width=20)
        Ent12.grid(row=6, column=1, padx=10, pady=5, sticky="w")

    elif value == "Number":
        label13 = Label(entry_frame2, text="Number", bg="crimson", fg="white",
                        font=("times new roman", 15, "bold"))
        label13.grid(row=7, column=0, padx=10, pady=5, sticky="w")
        Ent12 = Entry(entry_frame2, width=20)
        Ent12.grid(row=7, column=1, padx=10, pady=5, sticky="w")
        

var1 = StringVar(value=0)

entry_frame = Frame(wrapper, bg="crimson")
entry_frame.grid(row=8, column=0, columnspan=2 ,padx=10, pady=5, sticky="w")

entry_frame2 = Frame(wrapper, bg="crimson")
entry_frame2.grid(row=9, column=0, columnspan=2, padx=10, pady=5, sticky="w")


def lookup():
    print(Ent12.get())
    

lbl4 = Label(wrapper, text="Find", bg="crimson", fg="white",
             font=("times new roman", 15, "bold"))
lbl4.grid(row=5, column=0, padx=10, pady=10, sticky="w")

r1 = Radiobutton(wrapper, text="Alphabet",variable=var1, value="Alphabet",
                 command=print1_selection, width=22)
r1.grid(row=6,column=0,padx=10, pady=10, sticky="w")

r2 = Radiobutton(wrapper, text="Number", variable=var1, value="Number",
                 command=print1_selection, width=22)
r2.grid(row=6,column=1,padx=10, pady=10, sticky="w")

btn = Button(wrapper, text="Find",command=lookup, bd="5", width=15, height=2)
btn.grid(row=10, column=1, padx=20, pady=10)

win.mainloop()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31