2016-09-29 10 views
-3

私のコードは以下の通りです。Entryウィジェットから変数 'p'を取得し、新しい変数名として設定し、それを印刷する必要があります。何らかの理由で、次のエラーが表示されます。 'NameError:name' p 'が定義されていません。私はそれを修正する方法は全く考えていません。これが私の最後の手段です。私を助けてください。なぜ変数が見つからないのですか?

コード:

import tkinter as tk # python3 
#import Tkinter as tk # python 

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

#-------------------FUNCTIONS-------------------# 
def EnterP(): 
    b1 = p.get() 
    print (p.get()) 

def EnterS(*self): 
    print (self.s.get()) 

def EnterB(*args): 
    print (b.get()) 

def EnterN(*args): 
    print (n.get()) 
#-----------------------------------------------# 

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, 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 Page", 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) 

     #Population Number 
     w = tk.Label(self, text="Enter the value for the Population") 
     w.pack() 

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

     pb = tk.Button(self, text="OK", command = EnterP) 
     pb.pack() 

     #Survival Rates 
     w = tk.Label(self, text="Enter the value of Survival Rates") 
     w.pack() 

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

     sb = tk.Button(self, text="OK", command = EnterS) 
     sb.pack() 

     #Birth Rates 
     w = tk.Label(self, text="ENter the value for the Birth Rate") 
     w.pack() 

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

     bb = tk.Button(self, text="OK", command = EnterB) 
     bb.pack() 

     #Number of New Generations To Model 
     w = tk.Label(self, text="Enter the number of New Generatiions") 
     w.pack() 

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

     nb = tk.Button(self, text="OK", command = EnterN) 
     nb.pack() 


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



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") 
     yes.pack() 

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



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

答えて

-1

はこのようにコードを変更します。私は(あなたのプログラムで、よりその1つのエラーがあるかもしれないため)完全な答えを与えることをしようとはしません

class Population(tk.Frame): 

    def __init__(self, parent, controller): 
     def EnterP(): 
      b1 = p.get() 
      print (p.get()) 
+0

OMG。大変ありがとうございました。あなたの命の恩人。それで、関数はそれらを必要とするクラスと一緒にいなければなりませんか? – DavalliTV

+0

不要なので、クラスのメソッドとして定義することができます。あなたはそれを理解するためにもっと学ぶ必要があります。実際には、あなたのコードはあまり良くありませんし、質問は良い質問ではありません。だから誰かがあなたに否定的な投票を与えるのです。 – Howardyan

+0

私はそのようなことをどこで学ぶことができるのか知っていますか? – DavalliTV

0

可変スコープを考慮していません。変数に名前を付けるときはいつでも、コードの特定の領域でしかアクセスできません。

コードでは、例えばpclass Population.__init__スコープ内に定義されています。 EnterPは、の範囲外であり、と定義されているため、pにアクセスすることはできません。

各言語には、このようなスコープを処理する独自の方法があります。

https://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html

幸運:ここでは、このトピックに読むべき何かがあります!

+0

Ahh ok。ありがとう。私はそれについて知らなかったが、Webリンクを見て、それはより明確になっている。 – DavalliTV

関連する問題