How do the elements of a tuple be the arguments of a function in python?

0

First I'll summarize the problem and then explain what I'm trying to do. If you can answer the problem in a nutshell, you do not even have to see the rest. Is there a way to pass the elements of a tuple as arguments to a function? For example:

tupla=(arg1,arg2,arg3)

def f(*args):
    for i in args:
        print(i)

f(tupla)

In this script, args is a tuple that has an element that is a tuple. However, what I want is that args is a tuple with the 3 elements. That is, this will start:

(arg1,arg2,arg3)

But I want to printe:

arg1
arg2
arg3

Now I'll tell you what I want to do. Working with tkinter, I thought about creating a unique function to change windows. Initially I did this:

from tkinter import *

class Application:
    def __init__(self,master=None):
        self.master=master

        self.label=Label(self.master,text='Aqui é a Janela 1').pack()
        self.button=Button(self.master,text='Janela2')
        self.button.bind('<Button-1>',self.janela2)
        self.button.pack()

    def janela2(self,event):
        self.destroy_all_widgets(self.master)

        self.label=Label(self.master,text='Aqui é a Janela 2').pack()
        self.button=Button(self.master,text='Janela3')
        self.button.bind('<Button-1>',self.janela3)
        self.button.pack()

    def janela3(self,event):
        self.destroy_all_widgets(self.master)

        self.label=Label(self.master,text='Aqui é a Janela 3').pack()
        self.button=Button(self.master,text='Janela4')
        self.button.bind('<Button-1>',lambda event,arg1='Argumento1',arg2='Argumento2':self.janela4(event,arg1,arg2))
        self.button.pack()

    def janela4(self,event,arg1,arg2):
        self.destroy_all_widgets(self.master)

        self.label1=Label(self.master,text='Aqui é a Janela 4').pack()
        self.label2=Label(self.master,text=arg1).pack()
        self.label3=Label(self.master,text=arg2).pack()

    def destroy_all_widgets(self,frame):
        for i in frame.winfo_children():
            i.destroy()

root=Tk()
Application(root)
root.mainloop()

But I would like to do something like this:

from tkinter import *

class Application:
    def __init__(self,master=None):
        self.master=master

        self.label=Label(self.master,text='Aqui é a Janela 1').pack()
        self.button=Button(self.master,text='Janela2')
        self.button.bind('<Button-1>',lambda event,function=self.janela2:self.go_to_window(event,function))
        self.button.pack()

    def janela2(self,event):
        self.destroy_all_widgets(self.master)

        self.label=Label(self.master,text='Aqui é a Janela 2').pack()
        self.button=Button(self.master,text='Janela3')
        self.button.bind('<Button-1>',lambda event,function=self.janela3:self.go_to_window(event,function))
        self.button.pack()

    def janela3(self,event):
        self.destroy_all_widgets(self.master)

        self.label=Label(self.master,text='Aqui é a Janela 3').pack()
        self.button=Button(self.master,text='Janela4')
        self.button.bind('<Button-1>',lambda event,function=self.janela4,args=('Argumento1','Argumento2'):self.go_to_window(event,function,args))
        self.button.pack()

    def janela4(self,event,arg1,arg2):
        self.destroy_all_widgets(self.master)

        self.label1=Label(self.master,text='Aqui é a Janela 4').pack()
        self.label2=Label(self.master,text=arg1).pack()
        self.label3=Label(self.master,text=arg2).pack()

    def destroy_all_widgets(self,frame):
        for i in frame.winfo_children():
            i.destroy()

    def go_to_window(self,event,function,*args):
        self.destroy_all_widgets(self.master)

        if len(args)==0:
            function(None)
        else:
            function(None,args)

root=Tk()
Application(root)
root.mainloop()
    
asked by anonymous 08.05.2017 / 02:09

1 answer

3

Taking your first example, just do it:

tupla=(arg1,arg2,arg3)

def f(*args):
    for i in args:
        print(i)

f(*tupla)

The * operator as a prefix of a parameter, or element of a sequence or iterator, "unfolds" the contents of those arguments at that point. (And the ** operator does the same for dictionaries and named arguments)

    
08.05.2017 / 14:26