0

最近在使用 Python tkinter GUI 和 sqlite 3 构建项目时,我在 Python 编程中发现了很多问题。其中一个问题是从 python 中的另一个函数调用函数中的多个值的最佳方法是什么?同时,我对函数中的重调和调用值进行了一些研究,我知道在python函数中它可以允许返回多个值,但是,在调用时,我希望它专门调用我返回的值,例如return (x,y,z),我真的不知道我该怎么称呼它。

这是我项目中的一些代码,请随时给我关于我的代码和我上面提出的问题的任何建议

第一个功能

def taskUpdateB():
    conn = sqlite3.connect("zzzsqlite.db")
    booking = conn.cursor()

    index = sOLB.curselection()
    selTask = sOLB.get(index)
    bookinID = selTask[-2]

    getBookID = booking.execute('''SELECT bookingID FROM booking 
                        WHERE bookingID=?''', (bookinID,))    

    taUp = Toplevel()
    taUp.title("Task Update")
    taskLabel = Label(taUp, text ="Task Update", font=('Times', 20))
    taskLabel.grid(row=0)

    showLabel = Label(taUp, text ="Please Select and Enter Infomation Below", font=('Times', 18))
    showLabel.grid(row=1)

    var = IntVar()
    var = 0
    fullRadio = Radiobutton(taUp, text="Fully", variable=var, value=1, command = taskUpdCom)
    fullRadio.grid(row=2, sticky=W)

    partRadio = Radiobutton(taUp, text="Partly", variable=var, value=2, command = taskUpdCom)
    partRadio.grid(row=3, sticky=W)

    notRadio = Radiobutton(taUp, text="Unable", variable=var, value=3, command = taskUpdCom)
    notRadio.grid(row=4, sticky=W)

    noteLabel = Label(taUp, text ="Note:", font=('Times', 16))
    noteLabel.grid(row=5, sticky=W)
    noteBox = Text(taUp, width=30, height =20, font=('Arial', 12,), highlightbackground='black')
    noteBox.grid(row=6)

    comButton = Button(taUp, text ="Task Complete and Send Invoice", command = taskUpdCom)
    comButton.grid(row =7)

    booking.close()
    return (var, noteBox, showLabel, bookinID)

第二个功能

 def taskUpdCom():
        a = taskUpdateB
        #get the data from the update list
        var = taskUpdateB.var()
        noteBox = taskUpdateB.noteBox()
        showLabel = taskUpdateB.showLabel()
        bookinID = taskUpdateB.bookinID()

    selProg = str(var.get())
    if selProg == 0:
        showLabel["text"] = ("***Please Select Task Progress Below***")
    elif noteBox.get() == "":
        showLabel["text"] = ("***Please Enter Task Note Below***")
    else:
        conn = sqlite3.connect("zzzsqlite.db")
        update = conn.cursor()
        wriUpda = zzzsqliteTable.addUpdate(bookinID, selProg, noteBox)
        conn.commit()
        updata.close()
        taskUpdateB.noteBox.delete(0, END)
        tkinter.messagebox.showinfo("Notification","Your task has been updated and removed from the list")
        try:
            deIndex = taskUpdateB.index()#The item selected from task update delete it from the list.... will still in the database.
            sOLB.delete(deIndex)
        except IndexError:
            pass

请原谅我的代码还没有完全完成,有点凌乱......

谢谢你的帮助 :)

4

1 回答 1

5

这不是函数的工作方式。您可能想要调用该函数,然后在调用范围内解压缩结果。

var, noteBox, showLabel, bookinID = taskUpdateB()

虽然函数确实是 Python 中的对象,但它们不是有状态的处理器或其他东西,它们只是直接返回结果然后消失(好吧,除非你在做一些花哨的事情,但你不是)。

于 2013-04-15T20:37:53.433 回答