i need help for below codes, I trying to create a software using tkinter and i have a issue on passing the value from inside function inside sub function to main module. how to get the return result use in main module and insert in mylist.
Here's the codes: main module: test.py
from button_screener import screener
button created to open screen. command = screener
mylist = [ ]
mylist.insert(END, result)`
sub module: button_screener
def screener():
code.... (create tkinter layout code)
OK_button created to execute function execute() - command = execute
def execute():
code... after several calculation..get result below
result = list [a,b,c]
return result
Needs help and let me know if needed further clarifications.
Thank you
CodePudding user response:
When you call a new function, you can think of it as creating a stack (of operations). The function that is called latest will be at the top of the stack, and once it finishes executing it will return back to the calling function (and also return any value if you have specified in the return statement).
which means : when you do return result in your execute() function, it will provide that result to it's calling function : screener()
since you are calling screener() from your main module, you should capture the output of execute() in screener(), and then return that to the screener() 's calling function - which is main.
def screener():
code.... (create tkinter layout code)
#OK_button created to execute function execute() - command = execute
def execute():
code... after several calculation..get result below
result = list [a,b,c]
return result
result_from_execute = execute() #capturing response from execute()
return result_from_execute #return the result to the main module
CodePudding user response:
Since those functions are executed by a button click, whatever returned from those functions will be discarded.
As the result you want to return is a list, you can pass the required list variable to screener() as an argument which will be updated inside the function.
Below is a simple example:
import tkinter as tk
def screener(result):
def execute():
# do some calculation
a, b, c = 1, 2, 3
# then update result
result[:] = [a, b, c]
win.destroy()
win = tk.Toplevel()
tk.Button(win, text='OK', command=execute).pack(padx=50, pady=50)
win.grab_set()
mylist = []
root = tk.Tk()
tk.Button(root, text='Sceener', command=lambda:screener(mylist)).pack()
tk.Button(root, text='Show', command=lambda:print(mylist)).pack()
root.mainloop()
