2016-07-15 31 views
-1

現在私はgcseコンピュータサイエンスを行っていますが、私の現在の作業では人口モデルを作成する必要があります。私はいくつかの入力ボックスを作っていて、それを保存して別のウィンドウに印刷し、後で計算でそれを使うことができるようにしたいと思っています。誰かが値を印刷するのを助けてくれますか?私は多くの運を使わずに複数の方法を試みました。私は謝った、私が行った他のコードのいくつかは正しくないかもしれません!プログラムを実行しようとすると、「質問1は定義されていません」というエラーが、分かりにくいコードで表示され、印刷する情報が表示されます。tkinterの入力ボックスの値を表示

# import the tkinter module 
import tkinter 


def quitfile(): 
    window.destroy() 


def setvalues(): 

    # create a new window 
    window = tkinter.Tk() 
    # add a title 
    window.title("Population Model") 
    # make the size of the window 
    window.geometry("600x600") 
    # set the background of the window 
    window.configure(background = "#FFFFFF") 

    # title 
    lbQuestion = tkinter.Label(window, text = "SET GENERATION 0 VALUES", fg = "#5855FA", bg = "#FFFFFF" , font = "verdana 12 bold") 
    # place the widget into the window 
    lbQuestion.place(x = 10, y = 10) 

    ######################### 
    # question widgets 
    ######################### 

    # question 1 
    lbQuestion1 = tkinter.Label(window, text = "What is the population number of juveniles?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion1.place(x = 10, y =60) 

    # create an entry widget for question 1 
    entQuestion1 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion1.place (x = 10, y = 80) 

    # question 2 
    lbQuestion2 = tkinter.Label(window, text = "What is the population number of adults?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion2.place(x = 10, y =110) 

    # create an entry widget for question 2 
    entQuestion2 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion2.place (x = 10, y = 130) 

    # question 3 
    lbQuestion3 = tkinter.Label(window, text = "What is the population number of seniles?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion3.place(x = 10, y =160) 

    # create an entry widget for question 3 
    entQuestion3 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion3.place (x = 10, y = 180) 

    # question 4 
    lbQuestion4 = tkinter.Label(window, text = "What is the survival rate for juveniles?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion4.place(x = 10, y =210) 

    # create an entry widget for question 4 
    entQuestion4 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion4.place (x = 10, y = 230) 

    # question 5 
    lbQuestion5 = tkinter.Label(window, text = "What is the survival rate for adults?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion5.place(x = 10, y =260) 

    # create an entry widget for question 5 
    entQuestion5 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion5.place (x = 10, y = 280) 

    # question 6 
    lbQuestion6 = tkinter.Label(window, text = "What is the survival rate for seniles?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion6.place(x = 10, y =310) 

    # create an entry widget for question 6 
    entQuestion6 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion6.place (x = 10, y = 330) 

    # question 7 
    lbQuestion7 = tkinter.Label(window, text = "What is the birth rate?", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion7.place(x = 10, y =360) 

    # create an entry widget for question 7 
    entQuestion7 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion7.place (x = 10, y = 380) 

    # question 8 
    lbQuestion8 = tkinter.Label(window, text = "What is the number of new generations to model? (should be between 5 and 25)", fg = "#000000", bg = "#FFFFFF" , font = "verdana 8 bold") 
    # place the widget into the window 
    lbQuestion8.place(x = 10, y =410) 

    # create an entry widget for question 8 
    entQuestion8 = tkinter.Entry(window) 
    # place the widget into the window 
    entQuestion8.place (x = 10, y = 430) 

    ########################## 
    # checking widgets 
    ########################## 

    # create a button widget to check answers ############ QUESTION 1 ############### 
    logBtn = tkinter.Button(window, text = "Set Value", command = entQuestion1.get(),font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn.place (x = 350, y = 60) 

    # create a check answers label 
    lbCheck = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck.place(x = 350, y = 80) 



    # create a button widget to check answers ############# QUESTION 2 ############### 
    logBtn1 = tkinter.Button(window, text = "Set Value",command = entQuestion2.get(), font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn1.place(x = 350, y = 110) 

    # create a check answers label 
    lbCheck1 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck1.place(x = 350, y = 130) 

    # create a button widget to check answers ############# QUESTION 3 ############## 
    logBtn2 = tkinter.Button(window, text = "Set Value", command = entQuestion3.get(), font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn2.place(x = 350, y = 160) 

    # create a check answers label 
    lbCheck2 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck2.place(x = 350, y = 180) 

    # create a button widget to check answers ############ QUESTION 4 ############### 
    logBtn3 = tkinter.Button(window, text = "Set Value", font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn3.place(x = 350, y = 210) 

    # create a check answers label 
    lbCheck3 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck3.place(x = 350, y = 230) 

    # create a button widget to check answers ############ QUESTION 5 ############### 
    logBtn4 = tkinter.Button(window, text = "Set Value", command = entQuestion5.get(), font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn4.place(x = 350, y = 260) 

    # create a check answers label 
    lbCheck4 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck4.place(x = 350, y = 280) 

    # create a button widget to check answers ############ QUESTION 6 ############### 
    logBtn5 = tkinter.Button(window, text = "Set Value", command = entQuestion6.get(), font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn5.place(x = 350, y = 310) 

    # create a check answers label 
    lbCheck5 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck5.place(x = 350, y = 330) 

    # create a button widget to check answers ############ QUESTION 7 ############### 
    logBtn6 = tkinter.Button(window, text = "Set Value", command = entQuestion7.get(), font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn6.place(x = 350, y = 360) 

    # create a check answers label 
    lbCheck6 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck6.place(x = 350, y = 380) 

    # create a button widget to check answers ############ QUESTION 8 ############### 
    logBtn7 = tkinter.Button(window, text = "Set Value", command = entQuestion8.get(), font = "verdana 8 bold") 
    # place the widget in the window 
    logBtn7.place(x = 540, y = 410) 

    # create a check answers label 
    lbCheck7 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window 
    lbCheck7.place(x = 540, y = 410) 

    # create a button widget to return to main menu 
    logBtn8 = tkinter.Button(window, text = "Main Menu", font = "verdana 8 bold") 
    # place the widget in the window # 250 
    logBtn8.place(x = 180, y = 560) 

    # create a check answers label 
    lbCheck8 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window # 200 
    lbCheck8.place(x = 180, y = 560) 

    # create a button widget to display generation 0 values 
    logBtn9 = tkinter.Button(window, text = "Display Generation 0 values", command = displayvalues, font = "verdana 8 bold") 
    # place the widget in the window # 250 
    logBtn9.place(x = 270, y = 560) 

    # create a check answers label 
    lbCheck9 = tkinter.Label(window, fg = "#000000", bg = "#FFFFFF", font = "verdana 8 bold") 
    # place the widget into the window #200 
    lbCheck9.place(x = 270, y = 560) 

    Question1 = entQuestion1.get() 
    Question2 = entQuestion2.get() 
    Question3 = entQuestion3.get() 
    Question4 = entQuestion4.get() 
    Question5 = entQuestion5.get() 
    Question6 = entQuestion6.get() 
    Question7 = entQuestion7.get() 
    Question8 = entQuestion8.get() 



def displayvalues(): 

    # create a new window 
    window = tkinter.Tk() 
    # add a title 
    window.title("Population Model") 
    # make the size of the window 
    window.geometry("600x600") 
    # set the background of the window 
    window.configure(background = "#FFFFFF") 
    # title 
    lbQuestion = tkinter.Label(window, text = "DISPLAY GENERATION 0 VALUES", fg = "#5855FA", bg = "#FFFFFF", font = "verdana 12 bold") 
    # place the widget into the window 
    lbQuestion.place(x = 10, y = 10) 

    print(Question1.get()) 
    print(Question2.get()) 
    print(Question3.get()) 
    print(Question4.get()) 
    print(Question5.get()) 
    print(Question6.get()) 
    print(Question7.get()) 
    print(Question8.get()) 
+0

https://stackoverflow.com/help/mcveをお読みください。あなたのコード内のコメントは役に立たず、削除する必要があります。あなたがそのようなナンセンスを必要とする教授から授業を取っているなら、あなたは哀悼の意を表します。 –

答えて

0

まず、ボタンのcommandオプションは、command=entQuestion7.get()を書くときにあなたのコードが間違っている、それはcommand=entQuestion7.getする必要があり、機能を期待しています。しかし、このようにしても、価値観を何もしないので何も起こりません。

第二に、私はあなたの母集団モデルの代わりに、関数のクラスを作るために簡単かつ整然とされるべきだと思う:

import tkinter 

class PopulationModel: 
    def __init__(self): 
     self.window = tkinter.Tk() 

     # question 1 
     tkinter.Label(self.window, text="What is the population number of juveniles?").grid(row=0, column=0) 

     # create an entry widget for question 1 
     self.entQuestion1 = tkinter.Entry(self.window) 
     # place the widget into the window 
     self.entQuestion1.grid(row=1, column=0) 

     # default answers 
     self.Question1 = "" 

     # create a button widget to check answers ############ QUESTION 1 ############### 
     tkinter.Button(self.window, text="Set Value", command=self.get_1).grid(row=1, column=1) 

     # create a button widget to display generation 0 values 
     tkinter.Button(self.window, text="Display Generation 0 values", 
         command=self.displayvalues).grid(row=2, column=0, columnspan=2) 

     self.window.mainloop() 

    def get_1(self): 
     """ get answer 1 """ 
     self.Question1 = self.entQuestion1.get() 

    def displayvalues(self): 
     """ display the initial values """ 
     top = tkinter.Toplevel(self.window) 
     tkinter.Label(top, text="DISPLAY GENERATION 0 VALUES").grid(row=0) 
     tkinter.Label(top, text="Answer 1: %s" % self.Question1).grid(row=1) 

if __name__ == "__main__": 
    PopulationModel()