当我最近使用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
请原谅我的代码还没完全完成,有点乱......
感谢您的帮助:)
答案 0 :(得分:5)
这不是函数的工作方式。您可能希望调用该函数,然后将结果解压缩到调用范围。
var, noteBox, showLabel, bookinID = taskUpdateB()
虽然函数是Python中的对象,但它们不是有状态处理器或其他东西,它们只是直接返回结果然后消失(好吧,除非你做了一些奇特的事情,但事实并非如此)。< / p>