(I'm new to programming in general)

I created a program in python without an interface and part of it used a raw_input to get a users answer and compare with a stored answer.
This was in a loop that limited the number of attempts the user could have.

This worked because every time the program looped round to the input the user could change there answer from whatever they had before.
however when I tried to write this with a tkinter interface I used the EntryWidget.get() function, this seems to automatically get what ever is in the entrywidget without allowing the user to change this.

How can I get the program to allow the user to change there answer before the loop continues?

I tried to create a small program that illastrates the problem here

from Tkinter import *
returned_values = {}
class App:

    def __init__(self,master):

        frame = Frame(master)
        frame.grid()

        self.Answer_entry = Entry(master)
        self.Answer_entry.grid(row = 0, column = 1, sticky=W)
        User_Answer=self.Answer_entry.get()

        self.exampleAnswer(master)


    def exampleAnswer(self,master):
        Answer = 'exampleAnswer'
        self.CheckAnswerButton = Button(master, text="Check Answer", command=lambda: self.Check_Answer(Answer))
        self.CheckAnswerButton.grid(row = 1, column = 3, sticky = S)
    # answer defined in seperate function as it would be in prog
    

    def Check_Answer(self,Answer):
        attempts = 0 #sets the attempts = to 0 to start the loop
                     
        while 1: #starts infinite loop that will stop when break point hit
            
            User_answer= self.Answer_entry.get()#gets users Answer entry
            print User_answer    
            
            if User_answer != Answer: #checks if the user answer = the stored answer
                
                if attempts >= 10:
                    returned_values['attempts'] = attempts
                    break #breaks the loop if the user has had 10 attempts
                else:
                    attempts += 1 #adds one to attempts and repeats the loop
                    
            else: #only reaches this if there input is correct
                returned_values['attempts'] = attempts #global variable ti give other functions access
                break    #breaks the loop
        
root = Tk()
app = App(root)
root.mainloop()

That's because of the while() loop in the function. Instead keep track of the number of times the button is clicked.

from Tkinter import *
class App:
 
    def __init__(self,master):
 
        self.master=master
        self.returned_values = {}
        frame = Frame(master)
        frame.grid()
 
        self.Answer_entry = Entry(master)
        self.Answer_entry.grid(row = 0, column = 1, sticky=W)
        self.Answer_entry.focus_set()
        User_Answer=self.Answer_entry.get()
        self.attempts = 0 #sets the attempts = to 0 to start the loop
 
        self.exampleAnswer(master)
 
 
    def exampleAnswer(self,master):
        Answer = 'exampleAnswer'
        self.CheckAnswerButton = Button(master, text="Check Answer", command=lambda: self.Check_Answer(Answer))
        self.CheckAnswerButton.grid(row = 1, column = 3, sticky = S)
    # answer defined in seperate function as it would be in prog
 
 
    def Check_Answer(self,Answer):
 
##        while 1: #starts infinite loop that will stop when break point hit
 
            User_answer= self.Answer_entry.get()#gets users Answer entry
            print User_answer    
 
            if User_answer != Answer: #checks if the user answer = the stored answer
 
                if self.attempts >= 10:
                    self.returned_values['attempts'] = self.attempts
                    self.master.quit()
#                    break #breaks the loop if the user has had 10 attempts
#                else:
                self.attempts += 1 #adds one to attempts and repeats the loop
 
            else: #only reaches this if there input is correct
                self.returned_values['attempts'] = attempts #global variable ti give other functions access
                self.master.quit()
#                break    #breaks the loop
 
root = Tk()
app = App(root)
root.mainloop()
print "returned_values", app.returned_values

Hi, thanks for the help this seems to work for this example, however in my program instead of using the master.quit would there be a way to return the value 'self.attempts' into the exampleAnswer function for further computing.

Thanks, Alex

Found a way around it now, doesn't seem the most elegant way but does the job, Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.