Python 3.x Tkinter遇到了一个bug

xoshrz7s  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(216)
from tkinter import *
import pandas
import random


def button_y_pressed():
    x = random.choice(data)
    canvas.itemconfig(text1, text="French")
    canvas.itemconfig(text2, text=x["French"])
    window.after(3000, flip_card)
    canvas.itemconfig(text2, text=x["English"])

def flip_card():
    canvas.itemconfig(text1, text="English")
    canvas.itemconfig(canvas_image, image=back_image)

BACKGROUND_COLOR = "#B1DDC6"
window = Tk()
window.configure(bg=BACKGROUND_COLOR)
window.geometry("900x780")
window.title("Flash Cards")

my_image_x = PhotoImage(file="images/wrong.png")
button_x = Button(image=my_image_x, highlightthickness=0)
button_x.grid(row=1, column=0, padx=10, pady=10)

my_image_y = PhotoImage(file="images/right.png")
button_y = Button(image=my_image_y, highlightthickness=0, command=button_y_pressed)
button_y.grid(row=1, column=1, padx=10, pady=10)

front_image = PhotoImage(file="images/card_front.png")
canvas = Canvas(window, width=800, height=526, highlightthickness=0, bg=BACKGROUND_COLOR)
canvas_image = canvas.create_image(400, 263, image=front_image)
canvas.grid(row=0, column=0, columnspan=2, padx=50, pady=50)
back_image = PhotoImage(file="images/card_back.png")
text1 = canvas.create_text(400, 150, text="text1", font=("Arial", 40, "italic"))
text2 = canvas.create_text(400, 263, text="text2", font=("Arial", 60, "bold"))

data = pandas.read_csv("data/french_words.csv")
data = data.to_dict(orient="records")

button_y_pressed()

window.mainloop()

我希望该程序显示该词的法语版本3秒后,它会翻转,并显示英语版本,而不是在此代码程序显示英语版本不显示在所有法语版本

lo8azlld

lo8azlld1#

如果你使用window.after(3000, flip),它会计划在>= 3000 ms内执行函数flip并立即执行下一行,它实际上不会等待3000 ms并执行flip,然后继续执行下一行(像time.sleep(3))。所以在倒计时之后你想要执行的所有东西都必须放到回调中:

def button_y_pressed():
    x = random.choice(data)
    canvas.itemconfig(text1, text="French")
    canvas.itemconfig(text2, text=x["French"])
    window.after(3000, flip_card)
    

def flip_card():
    canvas.itemconfig(text1, text="English")
    canvas.itemconfig(text2, text=x["English"])
    canvas.itemconfig(canvas_image, image=back_image)

相关问题