Search code examples
pythonlistimagetypestxt

Read list from .txt-file and referring to entries


I am new to programming and am trying to write a card game using python.

I have some problems:

  1. I am reading in a decklist from a .txt-file like this:

2,Card001,1

2,Card002,2

2,Card003,3

I am trying to display images in tkinter based on this decklist.

My main problem is that reading in gives me a list with the card names as a string. And I don't know how to convert this string so that I am able to refer to it and display the associated images.

Here is my "simplified" code:

import csv
import tkinter as tk
from PIL import ImageTk,Image
import random

### CREATING GUI:

root = tk.Tk()
root.geometry("1260x700")
root.resizable(width=True, height=False)
root.title("RandomCards")

### READING IN TXT-FILE:

col_names = ("Quantity", "Card Name", "Value")

with open("deck_list.txt", "r", newline="") as decklist:
    reader = csv.reader(decklist)
    data = list(reader)

### CREATING DECK:

deck = []

for i, row in enumerate(data, start=4):
    if row[0]=='1':
        deck.append(row[1]) 
    if row[0]=='2':
        deck.append(row[1])
        deck.append(row[1]) 
    if row[0]=='3':
        deck.append(row[1])
        deck.append(row[1])
        deck.append(row[1])
           
### STORING IMAGES:
    
img_Card001 = Image.open("C:/Users/DFeldmann/Card001.webp")
Card001 = ImageTk.PhotoImage(img_Card001)

img_Card002 = Image.open("C:/Users/DFeldmann/Card002.webp")
Card002 = ImageTk.PhotoImage(img_Card002)

img_Card003 = Image.open("C:/Users/DFeldmann/Card003.webp")
Card003 = ImageTk.PhotoImage(img_Card003)

#### >>> This is how >deck< should look like: 
####     deck = [Card001, Card001, Card002, Card002, Card003, Card003]

hand_size = 4
def draw_hand():
    for i in range(hand_size):
        draw = deck[random.randint(0,len(deck)-1)]
        deck.remove(draw)
        hand.append(draw)
    
hand = []
draw_hand()
print(hand)

### CREATING HAND SLOTS:

button = tk.Button(root, image=hand[0])
button.grid(row=1, column=1)

button = tk.Button(root, image=hand[1])
button.grid(row=1, column=2)

button = tk.Button(root, image=hand[2])
button.grid(row=1, column=3)

button = tk.Button(root, image=hand[3])
button.grid(row=1, column=4)


root.mainloop()
  1. I guess there are a lot of things to improve like storing the images in a more efficient way or creating the deck list based on the given quantity of cards in the .txt-file.

All in all this code is working if I create the deck manually, since this is not given as string. (But as object?)

deck = [Card001, Card001, Card002, Card002, Card003, Card003]

I have tried to search other topics but since I am very new to programming and do not understand a lot of the terminology it is hard for me to search for this specific issue.


Solution

  • You can create the card objects dynamically when reading the .txt:

    1. Read each line, split by ',' and get the second item (the name);
    2. Create an Image using a format string and the card name;
    3. Create an ImageTk.PhotoImage using the Image and append it to a list deck.
    deck = []
    
    with open("deck_list.txt", "r") as f:
        for line in f:
            card_name = line.split(",")[1]
            img_card = Image.open(f"C:/Users/DFeldmann/{card_name}.webp")
            deck.append(ImageTk.PhotoImage(img_card))