1

My code displays two sides of a card.

from tkinter import *
import pandas as pd
import random as r
import time


BACKGROUND_COLOR = "#B1DDC6"
FRONT_SIDE_IMAGE = r"D:\python course\day 31 - flash card capstone project\images\card_front.png"
BACK_SIDE_IMAGE = r"D:\python course\day 31 - flash card capstone project\images\card_back.png"
CORRECT_BUTTON_IMAGE = r"D:\python course\day 31 - flash card capstone project\images\right.png"
INCORRECT_BUTTON_IMAGE = r"D:\python course\day 31 - flash card capstone project\images\wrong.png"
DATA_FILE_PATH = r"D:\python course\day 31 - flash card capstone project\data\french_words.csv"

# front background image
bg_canvas = Canvas(width=800, height=526,
                   bg=BACKGROUND_COLOR, highlightthickness=0)
front_img = PhotoImage(file=FRONT_SIDE_IMAGE)
card_img = bg_canvas.create_image(400, 263, image=front_img)
language = bg_canvas.create_text(
    400, 150, text="French", font=("Arial", 40, "italic"))
card_word = bg_canvas.create_text(
    400, 263, text="", font=("Arial", 60, "bold"))
bg_canvas.grid(row=0, column=0, columnspan=2)

Unfortunately, I am unable to load the "card_img" and it just shows that background colour. Will be grateful for guidance!

dev
  • 41
  • 2

1 Answers1

0

First of all, I think that you could have just done a quick Microsoft Bing (or Google, if you prefer, but I think Bing is better for coding and developers) search. My top result was another Stack Overflow question, which I've marked as a duplicate to yours and have linked to in the second to last paragraph.

Aside from all that, I think what you are trying to do is to set the background of your tkinter window, something like this (image credit of Educba):
Screenshot of result

If that's what you are trying to do, then you can look at this Stack Overflow question, (that's the duplicate I was talking about before).

So use the following code (this was found in Bryan Oakley's answer, I didn't create this):

background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

And if you are creating and showing the image in a function, prevent the image from being garbage collected by Python by creating a variable that holds an instance of the image, like this:

background_label.image = background_image

You should now have a background image in your GUI!

P.S. You can also take a peek at this article.

Ben the Coder
  • 539
  • 2
  • 5
  • 21