如何在类中的另一个函数中调用函数的结果?

时间:2017-04-26 15:03:19

标签: python python-2.7 function class

我在Python 2.7中有这样的代码:

class App(ttk.frame):
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.okButton = ttk.Button(self, text = "OK", command = self.function2)
        self.okButton.grid(column = 1, row = 1)

    def function1(self, arg1, arg2): # function create fields in frame
        self.arg1 = arg1
        self.arg2 = arg2

    def function2(self):  #function calcule things with values of fields when Ok button is click
        doing_thing_to(x, y, z, w)

app = App()
app.function1("x", "y")  # Create first field
app.function1("z", "w")  # Create another field
mainloop()

当function2正在调用时,我有一条错误消息:全局名称x,y未定义。

我试着把

x.get(); y.get()
在function2中

但是有相同的错误。

我试着把

return arg1, arg2
在function1中

但是有同样的问题。

如何在类中的另一个函数中调用函数的结果?

编辑:完整代码,因为我不知道如何简化理解:( function champ et champdouble有function1角色和函数回调有function2角色。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from os import getcwd, path
import Tkinter as tk
from Tkinter import *
import tkFileDialog as filedialog
import shutil
import ttk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.style.use('ggplot')
wd = getcwd() # working directory

class Application(ttk.Frame):
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.quitButton = ttk.Button(self, text='Quitter', command=self.quit)
        self.quitButton.grid(column=5, row=10, sticky=W)
        self.okButton = ttk.Button(self, text="clic !", command=self.callback)
        self.okButton.grid(column=4, row=10, sticky=W)

    def champ(self, nom, defaut, col, ran, lab, collab, ranlab, largeur=7):
        self.nom = nom
        self.defaut = defaut
        self.col = col
        self.ran = ran
        self.lab = lab
        self.collab = collab
        self.ranlab = ranlab
        self.largeur = largeur
        self.nom = StringVar()
        nom = ttk.Entry(mainframe, width=largeur, textvariable=nom)
        nom.insert(0, defaut)
        if nom.bind('<FocusIn>'):
            nom.delete(0, "end")
        nom.grid(column=col, row=ran, sticky=W)
        ttk.Label(mainframe, text=lab).grid(column=collab, row=ranlab, sticky=E)

    def champdouble(self, nom1, defaut1, nom2, defaut2, col, ran, lab, lab2, collab, ranlab, largeur=7):
        self.nom1 = nom1
        self.defaut1 = defaut1
        self.nom2 = nom2
        self.defaut2 = defaut2
        self.col = col
        self.ran = ran
        self.lab = lab
        self.lab2 = lab2
        self.collab = collab
        self.ranlab = ranlab
        self.largeur = largeur
        nom1 = StringVar()
        nom1 = ttk.Entry(mainframe, width=largeur, textvariable=nom1)
        nom1.insert(0, defaut1)
        nom1.grid(column=col, row=ran, sticky=W)
        ttk.Label(mainframe, text=lab).grid(column=collab, row=ranlab, sticky=E)
        nom2 = StringVar()
        nom2 = ttk.Entry(mainframe, width=largeur, textvariable=nom2)
        nom2.insert(0, defaut2)
        nom2.grid(column=col+2, row=ran, sticky=W)
        ttk.Label(mainframe, text=lab2).grid(column=collab+2, row=ranlab, sticky=E)


    def on_entry_click(self, event):
        """function that gets called whenever entry is clicked"""
        global dirname
        if file1.get() == 'Choisissez un fichier...':
            file1.delete(0, "end") # delete all the text in the entry
            dirinit = r'C:/'
            dirname = filedialog.askopenfilename(parent=mainframe, initialdir=dirinit, title='Sélectionnez le fichier')
            file1.insert(0, dirname) #Insert blank for user input

    def on_entry_click1(self, event):
        """function that gets called whenever entry is clicked"""
        global dirname2
        if file2.get() == 'Choisissez un fichier...':
           file2.delete(0, "end") # delete all the text in the entry
           dirinit = r'C:/'
           dirname2 = filedialog.askopenfilename(parent=mainframe, initialdir=dirinit, title='Sélectionnez le fichier')
           file2.insert(0, dirname2) #Insert blank for user input

    def callback(self):
        def traitement(fichier, debut, nif):
            deb = int(debut.get())
            fin = int(nif.get())
            df = pd.read_csv(fichier, sep = '\t', engine = 'python', header = deb, skipfooter = fin)  # Lecture des fichiers
            df = df.rename(columns={'$Relations :NumZoneE': 'NumZoneE'})  # Renommage des entêtes de colonnes
            df = df[(df.NumZoneE != df.NumZoneA)]  # supression des intrazonaux
            df = df[(df.NumZoneE <= 1289)]  # supression des zones superieures a 1289
            df = df[(df.NumZoneA <= 1289)]
            df['OD_possible']=np.where(df['JRTA'] < 999999, 'oui', 'non')  # creation d'une colonne OD_possible
            df = pd.merge(df, dvol, on = ['NumZoneE', 'NumZoneA'])  # jointure des tables avec dvol
            dfg = df.groupby('OD_possible')  # groupage selon oui ou non
            return dfg

        # Chemin d'acces vers les fichiers à traiter
        dvol = r'c:\ceat_echange\1704_Test_maj_horaire_RERD_Sc2012\090721_DVOL_km.txt'

        # Traitement de dvol
        dvol = pd.read_csv(dvol, sep = '\t')  # Lecture
        dvol = dvol.rename(columns = {'ZONEO': 'NumZoneE', 'ZONED': 'NumZoneA'})  # Renommage entete
        dvol = dvol[(dvol.DVOL != 0)]  # Suppression intrazonaux

        fig = plt.figure()
        gss_oui = traitement(dirname, file1_deb, file1_fin).get_group('oui')
        gss_non = traitement(dirname, file1_deb, file1_fin).get_group('non')
        gac_oui = traitement(dirname2, file2_deb, file2_fin).get_group('oui')
        gac_non = traitement(dirname2, file2_deb, file2_fin).get_group('non')

        plt.hist([gss_oui[self.cettecolonne], gac_oui[self.cettecolonne]], range = (int(self.range1), int(self.range2)), bins = int(self.bins), label = [self.legend1l, self.legend2l])
        plt.legend(loc = 'best')
        plt.title(self.titre)
        plt.xlabel(self.axeXl, labelpad = 5)
        plt.ylabel(self.axeYl)
        plt.savefig(path.join(wd, self.sortiel))
        plt.show()
        plt.close()

if __name__ == '__main__':
    app = Application()
    style = ttk.Style()
    style.configure("BW.TEntry", foreground="grey", background="white")
    style.configure("BW1.TEntry", foreground="black", background="white")
    app.master.title('Comparaison de fichiers')

    mainframe = ttk.Frame(app, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)

    # construction du champ file1
    file1 = StringVar()
    file1_deb = StringVar()
    file1_fin = StringVar()
    file1 = ttk.Entry(mainframe, width=20, style="BW.TEntry")
    file1.insert(0, 'Choisissez un fichier...')
    file1.grid(column=2, row=1, sticky=W)
    file1.bind('<FocusIn>', app.on_entry_click)
    ttk.Label(mainframe, text="Fichier n° 1 : ").grid(column=1, row=1, sticky=E)
    file1_deb = ttk.Entry(mainframe, width=5, textvariable=file1_deb)
    file1_deb.insert(0, "26")
    file1_deb.grid(column=4, row=1, sticky=W)
    ttk.Label(mainframe, text="ligne de début").grid(column=3, row=1, sticky=E)
    file1_fin = ttk.Entry(mainframe, width=5, textvariable=file1_fin)
    file1_fin.insert(0, "1307")
    file1_fin.grid(column=6, row=1, sticky=W)
    ttk.Label(mainframe, text="lignes de fin à supprimer").grid(column=5, row=1, sticky=E)

    # construction du champ file2
    file2 = StringVar()
    file2_deb = StringVar()
    file2_fin = StringVar()
    file2 = ttk.Entry(mainframe, width=20, style="BW.TEntry")
    file2.insert(0, 'Choisissez un fichier...')
    file2.grid(column=2, row=2, sticky=W)
    file2.bind('<FocusIn>', app.on_entry_click1)
    ttk.Label(mainframe, text="Fichier n° 2 : ").grid(column=1, row=2, sticky=E)
    file2_deb = ttk.Entry(mainframe, width=5, textvariable=file2_deb)
    file2_deb.insert(0, "26")
    file2_deb.grid(column=4, row=2, sticky=W)
    ttk.Label(mainframe, text="ligne de début").grid(column=3, row=2, sticky=E)
    file2_fin = ttk.Entry(mainframe, width=5, textvariable=file2_fin)
    file2_fin.insert(0, "1307")
    file2_fin.grid(column=6, row=2, sticky=W)
    ttk.Label(mainframe, text="lignes de fin à supprimer").grid(column=5, row=2, sticky=E)

    app.champ("cettecolonne", "JRTA", 2, 3, "Champ à comparer :", 1, 3, 20)

    app.champ("titre", "Titre du graphique", 2, 4, "Titre du graphique :", 1, 4, 20)

    app.champdouble("range1", 0, "range2", 100, 2, 5, "Xmin :", "Xmax :", 1, 5)
    app.champ("bins", 20, 2, 6, "Nombre d'intervalle :", 1, 6, 5)
    app.champdouble("legend1", "file1", "legend2", "file2", 2, 7, "Légende du fichier n°1 :", "Légende du fichier n°2 :", 1, 7, 20)
    app.champ("axeX", "Axe des X", 2, 8, "Nom de l'axe des x :", 1, 8, 20)
    app.champ("axeY", "Axe des Y", 2, 9, "Nom de l'axe des y :", 1, 9, 20)
    app.champ("sortie", "image.png", 2, 10, "Nom du .png sauvegardé :", 1, 10, 20)

    for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
    mainloop()

1 个答案:

答案 0 :(得分:2)

您将值"x""y"保存到self.arg1中的self.arg2function1(),因此您必须通过{{1}中的名称来引用它们还有:

function2()

请注意,由于您正在使用某个类,因此必须首先使用class App(Object) def function1(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2 def function2(self): print(self.arg1, self.arg2) app = App() app.function1("x", "y") app.function2() 创建实例(我将您的app = App()类重命名为app,它是一个好主意用大写字母启动类名。

此时最好使用Python的“魔法”App,它允许您在创建实例时将它们传递给实例,而不是调用单独的__init__

function1()