类型选择获取()不工作

2024-06-06 15:08:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

我正在尝试将项目保存到与选定的名为typeSel的单选值相对应的文件中。我试着通过类型选择获取()但什么也没发生。为什么?当我把函数本身拉出来时,它工作得很好,但在这里就不行了。在


Tags: textaddgetifdefcreateitemwin
1条回答
网友
1楼 · 发布于 2024-06-06 15:08:03

实际发生的是,它无法获得radiobutton的值,因此打印了一个空白。也许你没有注意到,但如果你用这种方法打印其他东西,你会注意到的

因为我不能使用fridgePK.item_recognition(item)。我为他们做了测试。告诉我这是否有效:

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()

相关问题 更多 >