0
I will first remove the problem and then explain what I am trying to do. If you know how to answer the short problem, you don’t need to see the rest. Is there a way to pass the elements of a tuple as arguments of 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 for args to be a tuple with the 3 elements. I mean, it will:
(arg1,arg2,arg3)
But I want you to print:
arg1
arg2
arg3
Now I’ll tell you what I want to do. Working with Tkinter, I thought of creating a single function to change windows. I initially 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’d like to do something like:
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()
Just call the function as
f(*tupla)
. The*
converts the tuple to the function parameters.– Woss