0

I am working on a project on tkinter python.

This is how my graphic interface looks like:

enter image description here

And I have this database.txt:

ChickenCurry,Rice,Curry,Chicken,0.ppm
ChocolateCake,Chocolate,Flour,Sugar,Eggs,1.ppm
BolognesePasta,Pasta,Beef,TomatoeSauce,Cheese,2.ppm

Really simple. 0.ppm, 1.ppm and 2.ppm are the name of 3 images, the first one is the image of chicken curry the second one of chocolate and the last one of bolognese pasta.

My project: I would like to display the image of the chicken dish when I am clicking on the button ChickenCurry, the image of the chocolate cake when I am clicking on the chocolate cake, etc...

Here is my code:

import sys
from tkinter import *
import tkinter as tk
from PIL import Image

class Application(tk.Frame):
    x = 2

def __init__(self, param = None, i = None, master=None):
    super().__init__(master)
    self.master = master
    self.pack()
    self.create_widgets()
    
    
def create_widgets(self):
    

    if (self.x == 2):
        param = "coucou"
    self.hi_there = tk.Label(self)
    self.hi_there["text"] = param
    #self.hi_there["command"] = self.say_hi
    self.hi_there.pack(side="top")

    self.quit = tk.Button(self, text="QUIT", fg="red",
                          command=self.master.destroy)
    self.quit.pack(side="bottom")
    
    # Opening file in read format
    File = open('data.txt',"r")
    if(File == None):
        print("File Not Found..")
    else:
        while(True):
            # extracting data from records 
            record = File.readline()
            if (record == ''): break
            data = record.split(',')
            print('Name of the dish:', data[0])
            self.hi_there = tk.Button(self)
            self.hi_there["text"] = data[0]
            self.hi_there["command"] = self.photoOfTheDish
            self.hi_there.pack(side="top")
            # printing each record's data in organised form
            for i in range(1, len(data)-1):
                print('Ingredients:',data[i])
                self.hi_there = tk.Label(self)
                self.hi_there["text"] = data[i]
                self.hi_there.pack(side="top")
    File.close()
    
def photoOfTheDish(self):
    novi = Toplevel()
    self.canvas = Canvas(novi, width = 1500, height = 1000)
    self.canvas.pack(expand = YES, fill = BOTH)
    File = open('data.txt',"r")
    with open('data.txt') as f:
        record = File.readline()
        data = record.split(',')
        gif1 = PhotoImage(file = data[-1].rstrip('\n'))
                                        #image not visual
        self.canvas.create_image(50, 10, image = gif1, anchor = NW)
        #assigned the gif1 to the canvas object
        self.canvas.gif1 = gif1

    
    

root = tk.Tk()
root.geometry("5000x2000")
app = Application(master=root)
app.mainloop()

My issue is whatever the button I am clicking on, it's always the image corresponding to "0.ppm" which is displaying. I don't know how to link the button to his set of value from the database.

1 Answer 1

2

Inside photoOfTheDish(), you open data.txt and read only the first line to get the image filename. Therefore you always get the 0.ppm.

You can use lambda to pass the image filename to photoOfTheDish() when creating the buttons:

def create_widgets(self):
    ...
    else:
        while True:
            ...
            # extracting data from records
            record = File.readline().rstrip() # strip out the trailing newline
            ...
            # pass image filename to callback
            self.hi_there["command"] = lambda image=data[-1]: self.photoOfTheDish(image)
            ...
    ...

def photoOfTheDish(self, image):
    novi = Toplevel()
    self.canvas = Canvas(novi, width = 1500, height = 1000)
    self.canvas.pack(expand = YES, fill = BOTH)
    self.canvas.gif1 = PhotoImage(file=image)
    self.canvas.create_image(50, 10, image=self.canvas.gif1, anchor=NW)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.