Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/03/2013, 16:30
Avatar de Chezzy
Chezzy
 
Fecha de Ingreso: marzo-2013
Ubicación: Mérida, Yucatán, México
Mensajes: 2
Antigüedad: 11 años, 2 meses
Puntos: 0
Variables de una función en otra. Listeners

Estoy tratando de hacer un formulario con GUI (tkinter) pues es el tema que estoy viendo en Java actualmente, y me gustaría codificar mis tareas también en Python. Mi pregunta es ¿cómo puedo llamar a las variables de una función (método), u obtener su valor dentro de otra función? Aquí dejo mi script:

Código:
from tkinter import *
from tkinter.ttk import *

class Formulario(Frame):

    def __init__(self, Contenedor):

        Frame.__init__(self, Contenedor)

        self.Contenedor = Contenedor

        self.CreaGUI()

    def CreaGUI(self):

        self.Contenedor.title("Formulario de Inscripción")
        self.style = Style()
        self.style.theme_use("default")

        Nombre = Label(self.Contenedor, text = "Nombre: ").grid(row = 0, sticky = W)
        CampoTexto = Entry(self.Contenedor, width = 40)
        CampoTexto.grid(row = 0, column = 1)

        X = IntVar()
        Sexo = Label(self.Contenedor, text = "Sexo: ").grid(row = 1, pady = 30, sticky = W)
        M = Radiobutton(self.Contenedor, text = "M", variable = X, value = 1)
        M.grid(row = 1, column = 1, sticky = W)
        F = Radiobutton(self.Contenedor, text = "F", variable = X, value = 2)
        F.grid(row = 1, column = 1, sticky = W, padx = 50)

        Aceptar = Button(self.Contenedor)
        Aceptar.configure(text = "Aceptar", command = self.Listener)
        Aceptar.grid(row = 2, column = 1)

    def Listener(self):

        self.CreaGUI()

        Name = self.CampoTexto.get()
        Sex = 0

        if Sex == self.M.selection_get():

            print("Masculino")

        else:

            print("Femenino")

        print(Name, ", ", Sex)




if __name__ == '__main__':

    root = Tk()
    root.geometry("340x150")

    obj = Formulario(root)
    root.mainloop()
Lo que quiero es llamar a las variables CampoTexto, M y F (de la función CreaGUI), u obtener su valor en la función Listener.

También me gustaría saber si estoy empleando de forma correcta el listener (no se si se le conozca de igual forma en Python). Mi método listener es:

Código:
def Listener(self):

        self.CreaGUI()

        Name = self.CampoTexto.get()
        Sex = 0

        if Sex == self.M.selection_get():

            print("Masculino")

        else:

            print("Femenino")

        print(Name, ", ", Sex)
Y se lo quiero asignar a mi botón aceptar mediante ésta línea:
Código:
Aceptar = Button(self.Contenedor)
        Aceptar.configure(text = "Aceptar", command = self.Listener)
¿Así se asigna un listener?

Gracias de antemano.

Última edición por Chezzy; 02/03/2013 a las 16:44