On 15/08/18 01:56, Matthew Polack wrote: > from tkinter import * > > import random > > answer = "global"
You have the right concept but the wrong implementation. To declare a variable as global you declare it as normal in the outer scope then inside each function that uses it add a global line which, in this case, looks like global answer So your code should look like: answer = 0 # placeholder value def makeProblem(): global answer # access global answer .... def checkAnswer(): global answer .... BTW, Please post messages in plain text not HTML/RTF because otherwise we lose all the formatting which is critical in understanding the code. > def makeproblem(): > # deletes anything in text box from location 00 to the end > txt.delete(0.0, 'end') > sentence = "Your first problem is " > > number1 = random.randint(2,12) > number2 = random.randint(2,12) > answer = number1 * number2 without the global line at the top this create a local variable inside the function which is then lost when the function exits > txt.insert(0.0, sentence) > txt.insert(2.2, number1) > txt.insert(3.3, " x ") > txt.insert(4.4, number2) > > def checkanswer(): > # deletes anything in text box from location 00 to the end > txt.delete(0.0, 'end') > > # checks for what is written in answerbox > response = int(answerbox.get()) > if response == answer: This compares response to the global answer which has the value "global", so it is never equal. > result = "Great Job!" > score = int(score + 1) > else : > result = "Sorry...you were wrong" > txt.insert(0.0, result) > txt.insert(1,1, "Score is") > txt.insert(2,2, score) > > root = Tk() > root.geometry("640x640+0+0") > root.title("Times Tables") > > > timeslabel = Label(root, text="Times Tables Practice", fg="white", bg="blue", > font=("arial", 36, "bold")) > timeslabel.grid(columnspan=12, sticky='ew') > instruction = Label(root, text="Please click on the button to generate a > problem", fg="blue", bg="white", font=("arial", 16, "bold")) > instruction.grid(row=2, columnspan=20) > > blankline = Label(root, text = "", bg = "white") > blankline.grid(row=12, sticky='ew') > > # Makes an entry box with the variable of 'answerbox' > answerbox = Entry(root, bg="aqua", font=("arial", 24, "bold")) > answerbox.grid(row=15, columnspan=2, sticky=EW) > > # Makes a button that generate the Times Tables problem > btn = Button(root, text="GENERATE PROBLEM", bg="blue", fg="white", command= > makeproblem) > btn.grid(row=11, columnspan=2, sticky=EW) > > # Makes a button that checks the answer > btn = Button(root, text="CHECK ANSWER", bg="darkblue", fg="white", command= > checkanswer) > btn.grid(row=13, columnspan=2, sticky=EW) > > #HOW CAN I CENTRE THE TEXT??? If you mean the widget look at the fill and padding attributes of the layout manager. If you mean the text inside the widget look at the string formatting methods. Especiially center(), ljust() and rjust(). Its messy but with patience and suitable font selection it works. I don't know any other way in Tkinter, so usually stick with left aligned! > txt = Text(root, width=35, height=10, wrap=WORD, font=("arial", 24, "bold")) > txt.grid(row=12, columnspan=2, sticky=EW ) > > root.mainloop() > > > *Question 1:* > *Why cant the program check if 'answer' variable is correct?* See use of global above > *Question 2:* > Is there a way to centre text within the text frame? I can change the font > and size...but don't know how to centre it? Only with difficulty. See above. > *Question 3:* > My 'Score' check idea..may not actually work...haven't been able to get > that far yet though! It should work but you need to make score a global variable too or else the score will be thrown away when the function ends. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor