0

As I working with the Python tkinter GUI and sqlite 3 recently to build a project, I discovered a lots of questions in Python programming. One of the questions is what would be the best way of calling multiple values in a function from another function in python? Meanwhile, I've done some research about retuning and calling value in function, and I know that in python function it can allows to return multiple value, however, when it comes to calling and I want it to call specifically the value I return e.g return (x,y,z), I really not sure how could I call it.

Here are some code that are in my project, please feel free to give me any suggestion about my code and the question that I ask above

First function

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)

Second function

 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

Please forgive my code has not been fully complete yet, and kind of messy...

Thanks for your help :)

6
  • 1
    What do you mean by "calling a value"? Commented Apr 15, 2013 at 20:37
  • @Joel Cornett sorry for my language, calling a function which contain x value Commented Apr 15, 2013 at 20:38
  • 1
    Functions don't "contain" values. Functions produce return values. Commented Apr 15, 2013 at 20:39
  • @sr2222 yes, thats what i mean :) Commented Apr 15, 2013 at 20:39
  • I'm not sure you quite get that difference, actually. You seem to have the idea that the function is an execution container that holds the results within itself that you can then access. That's a fundamental conceptual misunderstanding. Commented Apr 15, 2013 at 20:43

1 Answer 1

5

That's not how functions work. You probably want to call the function then unpack the results in the calling scope.

var, noteBox, showLabel, bookinID = taskUpdateB()

While it is true that functions are objects in Python, they aren't stateful processors or something, they just return the result directly and then disappear (well, unless you are doing something fancy, but you aren't).

Sign up to request clarification or add additional context in comments.

9 Comments

You could also use the splat operator and unpack the values of taskUpdateB() as the arguments for taskUpdCom().
True, though he'd have to restructure a little bit and do that from the driver instead of in taskUpdCom.
@sr2222 Thanks for your solution, is that all it needs? do I need to covert it to an variable e.g value = var in order to call it in the second function?
He means the asterisk. Python has a shorthand for unpacking an iterable in to an argument list for a function. If you moved the call to taskUpdateB out of taskUpdCom, you could change the signature of taskUpdCom to expect 4 arguments, then just say taskUpdCom(*taskUpdateB()) from the outer calling scope.
That's a different, and less specific question. You should really focus on getting down the fundamentals of how functions work. I think you've built up an incorrect understanding from your experience with object oriented libraries for DB and UI interaction before getting a firm foundation in basic functional programming. You need to understand the basics of passing values to and from functions before worrying about syntactical sugar like argument unpacking.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.