Search code examples
python-3.xfunctionvariables

can someone please explain me how to pass variables between 2 functions using python 3?


i'm trying to pass one variable between 2 functions, but i don't understand where i'm wrong, because it doesn't work. here's my python3 script:

from tkinter import *
w1=Tk()
w2=Tk()
def go(which):
    print(which)
b=Button(w2,text='print',command=lambda:go(which.get()))
b.pack()
w2.withdraw()
def main(which):
    w2.deiconify()
b1=Button(w1,text='button 1',command=lambda:main('button 1'))
b1.pack()
b2=Button(w1,text='button 2',command=lambda:main('button 2'))
b2.pack()
w1.mainloop()

thanks


Solution

  • ok i just solved by myself:

    from tkinter import *
    w1=Tk()
    w2=Tk()
    w2.withdraw()
    def main(which):
        w2.deiconify()
        def go(which):
            print(which)
        b=Button(w2,text='print',command=lambda:go(which))
        b.pack()
    b1=Button(w1,text='button 1',command=lambda:main('button 1'))
    b1.pack()
    b2=Button(w1,text='button 2',command=lambda:main('button 2'))
    b2.pack()
    w1.mainloop()