全局变量代码混淆

时间:2016-10-08 12:04:40

标签: python python-3.x global-variables

我无法将数据输入从一个类传递到另一个类,我似乎无法使全局函数工作。我需要帮助的部分是在最后一个函数中,但它只有在你拥有整个代码时才有效。

import tkinter as tk
import os

self = tk
TITLE_FONT = ("AmericanTypewriter", 18, "bold")



#exit function
def Exit():

    os._exit(0)

#Functions end

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Home, Population, Survival, Birth,NEW, data, Quit):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("Home")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class Home(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Home", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Population",
                            command=lambda: controller.show_frame("Population"))
        button5 = tk.Button(self, text = "Quit",
                            command=lambda: controller.show_frame("Quit"))
        button1.pack()
        button5.pack()


class Population(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Enter Generation 0 Values", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)


        #Function


        def EnterPopulation():
            b1 = populationa.get()
            print (str(populationa.get()))
            bj = populationj.get()
            print (str(populationj.get()))
            bs = populations.get()
            print (str(populations.get()))




        def cal(*args):
            total = population_juveniles + population_adults + population_seniles

        #Population
        population = tk.Label(self, text="Value for the Populations")
        population.pack()

        labela= tk.Label(self, text= 'Population of Adults')
        populationa = tk.Entry(self)
        labela.pack()
        populationa.pack()
        population3 = tk.Button(self, text="Enter", command = EnterPopulation)
        population3.pack()

        labelj= tk.Label(self, text= 'Population of Juvenile')
        populationj = tk.Entry(self)
        labelj.pack()
        populationj.pack()
        population5 = tk.Button(self, text="Enter", command = EnterPopulation)
        population5.pack()

        labels= tk.Label(self, text= 'Population of Seniles')
        populations = tk.Entry(self)
        labels.pack()
        populations.pack()
        population6 = tk.Button(self, text="Enter", command = EnterPopulation)
        population6.pack()

        buttonS = tk.Button(self, text = "Survival Rates",
                        command=lambda: controller.show_frame("Survival"))

        buttonS.pack()

class Survival(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Survival Rates", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        def EnterSurvival(*self):
            S = survivalaa.get()  
            ss = survivalj.get()
            sss =survivals.get()
            print(str(survivalaa.get()))

        #Survival


        Survival = tk.Label(self, text="Value of Survival Rates between 0-1")
        Survival.pack()

        survivala= tk.Label(self, text= 'Survival rates of Adults')
        survivalaa = tk.Entry(self)
        survivala.pack()
        survivalaa.pack()
        survival69= tk.Button(self, text="Enter", command = EnterSurvival)
        survival69.pack()

        survivaljj= tk.Label(self, text= 'Survival rates of Juvenile')
        survivalj = tk.Entry(self)
        survivaljj.pack()
        survivalj.pack()
        survival5 = tk.Button(self, text="Enter", command = EnterSurvival)
        survival5.pack()

        labelss= tk.Label(self, text= 'Survival rates of Seniles')
        survivals = tk.Entry(self)
        labelss.pack()
        survivals.pack()
        survival6 = tk.Button(self, text="Enter", command = EnterSurvival)
        survival6.pack()

        buttonS = tk.Button(self, text = "Birth Rates",
                command=lambda: controller.show_frame("Birth"))

        buttonS.pack()

class Birth(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Birth Rates", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)


        def EnterBirth(*args):
            Birth1 = Birth2.get()

        Birtha = tk.Label(self, text="Birth rates")
        Birtha.pack()
        Birth2 = tk.Entry(self)
        Birth2.pack()
        Birth3 = tk.Button(self, text="OK", command = EnterBirth)
        Birth3.pack()

        buttonB = tk.Button(self, text = "New Generatons",
                            command=lambda: controller.show_frame("NEW"))

        buttonB.pack()

        #Number of New Generations To Model

class NEW(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="New Generations", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        def EnterNew(*args):
            print (New2.get())
            news = New2.get()

        New = tk.Label(self, text="Number of New Generatiions 5 - 25")
        New.pack()

        New2 = tk.Entry(self)
        New2.pack()

        New3 = tk.Button(self, text="OK", command = EnterNew)
        New3.pack()


        button = tk.Button(self, text="BACK To Home",
                           command=lambda: controller.show_frame("data"))
        button.pack()


class data(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Data", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        no = tk.Button(self, text = "No",
                    command = lambda: controller.show_frame("Home"))
        no.pack()

        global Birth1 
        global s
        global ss
        global sss
        global b1
        global bj
        global bs
        global news

        spja= b1 * s
        spjb = bj * ss
        spjs= bs * sss
        st = spja + spjb + spjs
        born = spja* rebirth
        old = b1 + bj + bs

        labelold = tk.Label(self, text = 'Orignal populaton'+str(old))
        labelold.pack()
        labelto = tk.Label(self, text='Adults that survived = '+str(spja)+ 'Juveniles = ' +str(spjb)+ 'Seniles = '+str(spjs)+ '/n Total= '+str(st))   
        labelto.pack()
        Labelnew= tk.Label(self, text='New Juveniles = '+str(born))
        Labelnew.pack()
        # function for export data

class Quit(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Are you sure you want to quit?", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)


        yes = tk.Button(self, text="Yes", command = Exit)
        yes.pack()

        no = tk.Button(self, text = "No",
                       command = lambda: controller.show_frame("Home"))
        no.pack()



if __name__ == "__main__":
    app = SampleApp()
    app.mainloop(

1 个答案:

答案 0 :(得分:0)

如果要使用全局变量,则需要执行以下操作:

var1 = <some value>
var2 = <some other value>
s1 = <yet another value here>

# then create your classes

class Whatever(tk.whatever_you_want):
    def __init__(self, arg1, arg2, whatever_arg):
       global var1   #whis tells the function to use the variable you defined already in the module scope
       local_var_1 = var1 * 100  # 

当然,也许你不知道这一点,全局变量通常被认为是不好的,但我认为这对你来说并不重要。当你修改和阅读来自waaay太多地方的变量来跟踪时,这很重要,但我不确定你是在构建那么大的脚本。