2016-10-08 6 views
-3

あるクラスから別のクラスへのデータ入力に問題があり、グローバル関数が機能しないようです。私の助けが必要な部分は最後の機能ですが、コード全体があれば機能します。グローバル変数コードの混乱

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(
+4

ここにコードが多すぎます。問題を表示するのに必要な最小限に抑えてください。 –

+0

あなたはどのようなグローバルな機能について話していますか?また....あなたが助けが必要な問題は何ですか?より具体的にしてください。 –

+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 # 

は、グローバル変数は通常、で開始する悪いと考えられていますしかし、私はそれがあなたのために大事だとは思わない。変数を変更して読み込んだら、それを追跡するのはあまりにも多くの場面で問題になるでしょうが、その大きなスクリプトを構築しているかどうかはわかりません。

+0

OPがクラスを使用しているので、グローバルを使用する理由はほとんどありません。彼の質問は、コーディング方法の深い誤解の兆候です。 –

+0

ええ...いくつかの人々は極端な初心者であり、OPは信じられないほど説明するのが難しいです....だから、私はちょうど答えを助け、私はそれを忘れました。私は多分速く何かをコーディングしたいと思うかもしれないし、pythonic方法をプログラミングする秘訣の知恵に決して関心を持たないかもしれないことを意味します。 –