【问题标题】:The best way to call multiple values in a function from another function in Python从 Python 中的另一个函数调用函数中多个值的最佳方法
【发布时间】:2013-04-08 02:31:32
【问题描述】:

最近在使用 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

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

感谢您的帮助:)

【问题讨论】:

  • “调用值”是什么意思?
  • @Joel Cornett 对不起我的语言,调用一个包含 x 值的函数
  • 函数不“包含”值。函数产生返回值。
  • @sr2222 是的,这就是我的意思:)
  • 实际上,我不确定您是否完全理解这种差异。您似乎认为该函数是一个执行容器,其中包含您可以访问的结果。这是一个基本的概念误解。

标签: python function return call


【解决方案1】:

函数不是这样工作的。您可能希望调用该函数,然后在调用范围内解压缩结果。

var, noteBox, showLabel, bookinID = taskUpdateB()

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

【讨论】:

  • 您还可以使用 splat 运算符并将 taskUpdateB() 的值解压缩为 taskUpdCom() 的参数。
  • 没错,尽管他必须进行一些重组并从驱动程序而不是 taskUpdCom 中执行此操作。
  • @sr2222 感谢您的解决方案,这就是它所需要的吗?我是否需要将其转换为变量,例如 value = var 以便在第二个函数中调用它?
  • 他的意思是星号。 Python 有一种将可迭代对象解包到函数参数列表中的简写。如果您将调用移至taskUpdCom 之外的taskUpdateB,则可以将taskUpdCom 的签名更改为期望4 个参数,然后从外部调用范围中说taskUpdCom(*taskUpdateB())
  • 这是一个不同的、不太具体的问题。您应该真正专注于了解函数如何工作的基础知识。我认为您在获得基本函数式编程的坚实基础之前,已经从您对 DB 和 UI 交互的面向对象库的经验中建立了不正确的理解。在担心诸如参数解包之类的语法糖之前,您需要了解在函数之间传递值的基础知识。
猜你喜欢
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多