Reputation: 9
I was doing my programming homework and I wrote the following code.
# Modules
from tkinter import *
import random
# Variables
colors = ["red", "blue", "green", "pink", "black", "yellow", "orange", "white", "purple", "brown"]
score = 0
max_time = 30
time_left = max_time
# Functions
def start_game(event):
if time_left == max_time:
if time_left > 0:
countdown()
next_color()
def countdown():
global time_left
if time_left > 0:
time_left -= 1
time_label.config(text=f"Time Left: {time_left}")
time_label.after(1000, countdown)
if time_left <= 0:
print("\n**************")
print("* Game over! *")
print("**************")
quit()
def next_color():
global user_input
global colors
global label
global score_l
global score
entry_box.delete(0, "end")
first_color = random.choice(colors)
second_color = random.choice(colors)
label.config(text=first_color.title(), fg=second_color)
print(score)
print(user_input.get())
if user_input.get() == second_color:
score += 1
score_l.config(f"Score: {score}")
# Main Window
root = Tk()
root.title("Color Reaction")
root.geometry("375x220")
instructions = Label(root, text="Type in the color of the word, not the text.", font=("Helvetica", 12))
instructions.pack()
score_label = Label(root, text="Press [ENTER] to start.", font=("Helvetica", 12))
score_label.pack()
time_label = Label(root, text=f"Time Left: {time_left}", font=("Helvetica", 12))
time_label.pack()
score_l = Label(root, text=f"Score: {score}", font=("Helvetica", 12), fg="black")
score_l.pack()
label = Label(root, font=("Helvetica", 60))
label.pack()
user_input = StringVar()
entry_box = Entry(root, textvariable=user_input)
root.bind("<Return>", start_game)
entry_box.pack()
entry_box.focus_set()
root.mainloop()
And then, this code never throwed out any error. But if you try to run the code, when I expected a string value, it printed out a blank value instead.
Upvotes: 0
Views: 58
Reputation: 299
First of all, You don't need StringVar()
for a tkinter entry box. You just have to use entry.get()
method. Second of all you are deleting the text before you use it from the entry box!!! Third of all you are changing the colours before checking them. Then the answer always be wrong. Fourth of all you cannot do something like this: score_l.config(f"Score: {score}")
, You must specify its a text attribute!! I have completely solved all the errors of your code. It looks like this:
# Modules
from tkinter import *
import random
# Variables
colors = ["red", "blue", "green", "pink", "black", "yellow", "orange", "white", "purple", "brown"]
score = 0
max_time = 30
time_left = max_time
# Functions
def start_game(event):
if time_left == max_time:
if time_left > 0:
countdown()
next_color()
def countdown():
global time_left
if time_left > 0:
time_left -= 1
time_label.config(text=f"Time Left: {time_left}")
time_label.after(1000, countdown)
if time_left <= 0:
print("\n**************")
print("* Game over! *")
print("**************")
quit()
def next_color():
global user_input
global colors
global label
global score_l
global score
print(entry_box.get())
print(label["fg"])
if entry_box.get() == label["fg"]:
print("adding score")
score += 1
score_l.config(text=f"Score: {score}")
entry_box.delete(0, "end")
first_color = random.choice(colors)
second_color = random.choice(colors)
label.config(text=first_color.title(), fg=second_color)
# Main Window
root = Tk()
root.title("Color Reaction")
root.geometry("375x220")
instructions = Label(root, text="Type in the color of the word, not the text.", font=("Helvetica", 12))
instructions.pack()
score_label = Label(root, text="Press [ENTER] to start.", font=("Helvetica", 12))
score_label.pack()
time_label = Label(root, text=f"Time Left: {time_left}", font=("Helvetica", 12))
time_label.pack()
score_l = Label(root, text=f"Score: {score}", font=("Helvetica", 12), fg="black")
score_l.pack()
label = Label(root, font=("Helvetica", 60))
label.pack()
user_input = StringVar()
entry_box = Entry(root, textvariable=user_input)
root.bind("<Return>", start_game)
entry_box.pack()
entry_box.focus_set()
root.mainloop()
I hope this code above works for you. I am sorry about your deadline, I did not see your question when you posted. Next time, post it earlier before your deadline. And please do understand where you made your errors.
Upvotes: 1