I’m trying to learn python but no matter where I look I can’t seem to find an answer for this or anyone else having this problem at all, so I’m wondering if I’m just doing something wrong myself as even when my online instructor does the exact same thing, they do not get this problem. We’re trying to make flash cards for study with a built in timer to flip them after 3 seconds using Tkinter. We’re using pycharm if that makes any difference.
Here’s my code.
BACKGROUND_COLOR = "#B1DDC6"
import pandas
file = pandas.read_csv("data/french_words.csv")
from tkinter import *
import random
start = 100
def new_flash_card():
x = random.randint(1, start)
canvas_front = Canvas(width=800, height=526, bg=BACKGROUND_COLOR, highlightthickness=0)
canvas_front.grid(column=0, row=0, columnspan=2)
canvas_front.create_image(410, 263, image=front_image)
canvas_front.create_text(400, 150, text="French", font=("Ariel", 40, "italic"))
canvas_front.create_text(400, 263, text=f"{file.French[x]}", font=("Ariel", 60, "bold"))
def flip(y):
canvas_front.itemconfig(card_title, text="English")
canvas_front.itemconfig(card_pic, image=back_image)
canvas_front.itemconfig(word_title, text=f"{file.English[x]}")
window = Tk()
window.config(height=800, width=800, bg=BACKGROUND_COLOR, padx=50, pady=50)
back_image = PhotoImage(file="images/card_back.png")
front_image = PhotoImage(file="images/card_front.png")
right_image = PhotoImage(file="images/right.png")
wrong_image = PhotoImage(file="images/wrong.png")
canvas = Canvas()
wrong_button = Button(image=wrong_image, highlightthickness=0)
wrong_button.grid(column=1, row=1)
right_button = Button(image=right_image, highlightthickness=0, command=new_flash_card)
right_button.grid(column=0, row=1)
canvas_front = Canvas(width=800, height=526, bg=BACKGROUND_COLOR, highlightthickness=0)
canvas_front.grid(column=0, row=0, columnspan=2)
global start = 100
x = random.randint(0, start)
card_pic = canvas_front.create_image(410, 263, image=front_image)
card_title = canvas_front.create_text(400, 150, text="French", font=("Ariel", 40, "italic"))
word_title = canvas_front.create_text(400, 263, text=f"{file.French[x]}", font=("Ariel", 60, "bold"))
canvas_front.after(3000, flip(x))
window.mainloop()
I have yet to make the code that will remove the card from the deck if they get it right, but without providing files, I have a list of 100 french words and their english translations and each card should show the french side first for 3 seconds then flip to the english side until the user presses one of two buttons. Here in lies the problem, if I leave out the code that includes canvas_front.after(3000, flip(x)),
the program runs by instantly giving me a french word, but no english word. If I press the button, I get a new french word. If I leave it as it is, it waits for 3 seconds and gives me only an english word, and the button press waits 3 seconds and only gives me english words, I never see the french. I don’t know what’s happening but my after command is for some reason cutting out all code before itself and I cant figure out how to bypass it. I’ve had this same problem with the after keyword in tkinter before but was allowed to bypass it by using the import time method but I am specifically not allowed to do that for this project.
>Solution :
If you want to pass a variable to a function call scheduled with after
, you need to use a lambda like so:
canvas_front.after(3000, lambda: flip(x))
As written, canvas_front.after(3000, flip(x))
calls flip
immediately, which isn’t what you want. Using a lambda
instead allows you to call an anonymous function that then calls flip(x)
for you as scheduled.