显示tkinter中输入框的值

时间:2016-07-15 17:58:52

标签: python tkinter

我目前正在做gcse计算机科学,对于我目前的工作,我必须制作一个人口模型。我已经制作了几个输入框,并希望能够存储然后获取该信息以在另一个窗口上打印,然后在计算中使用它。请有人帮助我获取要打印的值吗?我尝试了多种方法而没有太多运气。我道歉,我所做的其他一些代码可能不对!当我尝试运行我的程序时,错误说“问题1”未定义'在我现在的代码中尝试获取要打印的信息。

# 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())

1 个答案:

答案 0 :(得分: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()