Question

from Tkinter import *

def add_item():

    def create_rec(item):

        def saveREC():

            print typeSel.get()

        label2=Label(add_win, text="select item type:").pack()

        #radio buttons creation
        typeSel = StringVar()
        R1 = Radiobutton(add_win, text="meat", variable=typeSel, value="meat")                  
        R1.pack( anchor = W)

        saveBttn=Button(add_win, text="recognize this item in the future", command=saveREC).pack()

    def saveEntry():
        item=entry.get()
        a,typeVar=fridgePK.item_recognition(item)
        if a==True:
            print item, " is saved to ", typeVar

        #if item isn't recognized then user can add item to recognition list        
        if a==False:
            create_rec(item)

    add_win=Tk()

    entry=Entry(add_win, width=30)
    entry.pack()
    entry.focus_set()

    saveBttn=Button(add_win, text="add item", width=25, command=saveEntry)
    saveBttn.pack()

    add_win.mainloop()

I'm trying to save an item to a file corresponding to the selected radio value named typeSel. I attempt to call the radio value through typeSel.get() but nothing is happening. Why is that? When I pull the function out by itself it works fine but not in here.

Était-ce utile?

La solution

What actually was happening was that it was not able to get the value of radiobutton & hence printing a blank. Maybe you didn't notice it but if you print something else in that method,you would have noticed

Since I couldn't use fridgePK.item_recognition(item) . I took test vales for them. Tell me if this works:

from Tkinter import *

def add_item():

    def create_rec(item):

        def saveREC():
            print "hola"#for test purpose
            print typeSel.get()

        label2=Label(add_win, text="select item type:").pack()

        #radio buttons creation
        typeSel = StringVar()
        R1 = Radiobutton(add_win, text="meat", variable=typeSel, value="meat")                  
        R1.pack( anchor = W)
        typeSel.set('meat')

        saveBttn=Button(add_win, text="recognize this item in the future", command=saveREC).pack()

    def saveEntry():
        item=entry.get()
        a=False
        typeVar="hello"
        if a==True:
            print item, " is saved to ", typeVar

        #if item isn't recognized then user can add item to recognition list        
        if a==False:
            create_rec(item)

    add_win=Tk()

    entry=Entry(add_win, width=30)
    entry.pack()
    entry.focus_set()

    saveBttn=Button(add_win, text="add item", width=25, command=saveEntry)
    saveBttn.pack()

    add_win.mainloop()

add_item()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top