python - displaying values from entry boxes in tkinter -
i doing gcse computer science , current piece of work have make population model. have made several entry boxes , want able store , information print on window , later use in calculations. please me values print? have tried multiple ways without luck. apologise, of other code have done may not right! when try run program, error saying 'question 1 not defined' in code have @ minute try , information print appears.
# import tkinter module import tkinter def quitfile (): window.destroy() def setvalues(): # create new window window = tkinter.tk() # add title window.title("population model") # make size of window window.geometry("600x600") # set background of window window.configure(background = "#ffffff") # title lbquestion = tkinter.label(window, text = "set generation 0 values", fg = "#5855fa", bg = "#ffffff" , font = "verdana 12 bold") # place widget window lbquestion.place(x = 10, y = 10) ######################### # question widgets ######################### # question 1 lbquestion1 = tkinter.label(window, text = "what population number of juveniles?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion1.place(x = 10, y =60) # create entry widget question 1 entquestion1 = tkinter.entry(window) # place widget window entquestion1.place (x = 10, y = 80) # question 2 lbquestion2 = tkinter.label(window, text = "what population number of adults?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion2.place(x = 10, y =110) # create entry widget question 2 entquestion2 = tkinter.entry(window) # place widget window entquestion2.place (x = 10, y = 130) # question 3 lbquestion3 = tkinter.label(window, text = "what population number of seniles?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion3.place(x = 10, y =160) # create entry widget question 3 entquestion3 = tkinter.entry(window) # place widget window entquestion3.place (x = 10, y = 180) # question 4 lbquestion4 = tkinter.label(window, text = "what survival rate juveniles?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion4.place(x = 10, y =210) # create entry widget question 4 entquestion4 = tkinter.entry(window) # place widget window entquestion4.place (x = 10, y = 230) # question 5 lbquestion5 = tkinter.label(window, text = "what survival rate adults?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion5.place(x = 10, y =260) # create entry widget question 5 entquestion5 = tkinter.entry(window) # place widget window entquestion5.place (x = 10, y = 280) # question 6 lbquestion6 = tkinter.label(window, text = "what survival rate seniles?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion6.place(x = 10, y =310) # create entry widget question 6 entquestion6 = tkinter.entry(window) # place widget window entquestion6.place (x = 10, y = 330) # question 7 lbquestion7 = tkinter.label(window, text = "what birth rate?", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion7.place(x = 10, y =360) # create entry widget question 7 entquestion7 = tkinter.entry(window) # place widget window entquestion7.place (x = 10, y = 380) # question 8 lbquestion8 = tkinter.label(window, text = "what number of new generations model? (should between 5 , 25)", fg = "#000000", bg = "#ffffff" , font = "verdana 8 bold") # place widget window lbquestion8.place(x = 10, y =410) # create entry widget question 8 entquestion8 = tkinter.entry(window) # place widget window entquestion8.place (x = 10, y = 430) ########################## # checking widgets ########################## # create button widget check answers ############ question 1 ############### logbtn = tkinter.button(window, text = "set value", command = entquestion1.get(),font = "verdana 8 bold" ) # place widget in window logbtn.place (x = 350, y = 60) # create check answers label lbcheck = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck.place(x = 350, y = 80) # create button widget check answers ############# question 2 ############### logbtn1 = tkinter.button(window, text = "set value",command = entquestion2.get(), font = "verdana 8 bold" ) # place widget in window logbtn1.place(x = 350, y = 110) # create check answers label lbcheck1 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck1.place(x = 350, y = 130) # create button widget check answers ############# question 3 ############## logbtn2 = tkinter.button(window, text = "set value", command = entquestion3.get(), font = "verdana 8 bold" ) # place widget in window logbtn2.place(x = 350, y = 160) # create check answers label lbcheck2 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck2.place(x = 350, y = 180) # create button widget check answers ############ question 4 ############### logbtn3 = tkinter.button(window, text = "set value", font = "verdana 8 bold" ) # place widget in window logbtn3.place(x = 350, y = 210) # create check answers label lbcheck3 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck3.place(x = 350, y = 230) # create button widget check answers ############ question 5 ############### logbtn4 = tkinter.button(window, text = "set value", command = entquestion5.get(), font = "verdana 8 bold" ) # place widget in window logbtn4.place(x = 350, y = 260) # create check answers label lbcheck4 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck4.place(x = 350, y = 280) # create button widget check answers ############ question 6 ############### logbtn5 = tkinter.button(window, text = "set value", command = entquestion6.get(), font = "verdana 8 bold" ) # place widget in window logbtn5.place(x = 350, y = 310) # create check answers label lbcheck5 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck5.place(x = 350, y = 330) # create button widget check answers ############ question 7 ############### logbtn6 = tkinter.button(window, text = "set value", command = entquestion7.get(), font = "verdana 8 bold" ) # place widget in window logbtn6.place(x = 350, y = 360) # create check answers label lbcheck6 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck6.place(x = 350, y = 380) # create button widget check answers ############ question 8 ############### logbtn7 = tkinter.button(window, text = "set value", command = entquestion8.get(), font = "verdana 8 bold" ) # place widget in window logbtn7.place(x = 540, y = 410) # create check answers label lbcheck7 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window lbcheck7.place(x = 540, y = 410) # create button widget return main menu logbtn8 = tkinter.button(window, text = "main menu", font = "verdana 8 bold" ) # place widget in window # 250 logbtn8.place(x = 180, y = 560) # create check answers label lbcheck8 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget window # 200 lbcheck8.place(x = 180, y = 560) # create button widget display generation 0 values logbtn9 = tkinter.button(window, text = "display generation 0 values", command = displayvalues, font = "verdana 8 bold" ) # place widget in window # 250 logbtn9.place(x = 270, y = 560) # create check answers label lbcheck9 = tkinter.label(window, fg = "#000000", bg = "#ffffff", font = "verdana 8 bold") # place widget 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 new window window = tkinter.tk() # add title window.title("population model") # make size of window window.geometry("600x600") # set background of window window.configure(background = "#ffffff") # title lbquestion = tkinter.label(window, text = "display generation 0 values", fg = "#5855fa", bg = "#ffffff", font = "verdana 12 bold") # place widget 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())
first, command
option of button expects function, code wrong when write command=entquestion7.get()
, should command=entquestion7.get
. way, nothing happend because don't value.
secondly, think should easier , tidier make class instead of function population model:
import tkinter class populationmodel: def __init__(self): self.window = tkinter.tk() # question 1 tkinter.label(self.window, text="what population number of juveniles?").grid(row=0, column=0) # create entry widget question 1 self.entquestion1 = tkinter.entry(self.window) # place widget window self.entquestion1.grid(row=1, column=0) # default answers self.question1 = "" # create button widget check answers ############ question 1 ############### tkinter.button(self.window, text="set value", command=self.get_1).grid(row=1, column=1) # create button widget 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): """ answer 1 """ self.question1 = self.entquestion1.get() def displayvalues(self): """ display 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()
Comments
Post a Comment