将数据库中的数据链接到按钮 tkinter

Linked data from database to a button tkinter

我正在 tkinter 上做一个项目 python。

我的图形界面是这样的:

我有这个 database.txt:

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

真的很简单。 0.ppm、1.ppm和2.ppm是3张图片的名字,第一张是咖喱鸡的图片,第二张是巧克力的图片,最后一张是肉酱面的图片。

我的项目:我想在单击“咖喱鸡”按钮时显示鸡肉菜肴的图像,在单击“咖喱鸡”按钮时显示巧克力蛋糕的图像巧克力蛋糕等...

这是我的代码:

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()

我的问题是无论我点击什么按钮,显示的始终是对应于“0.ppm”的图像。我不知道如何从数据库中link按钮到他的设置值。

photoOfTheDish() 中,您打开 data.txt 并只读取第一行以获取图像文件名。因此你总是得到 0.ppm.

您可以在创建按钮时使用 lambda 将图像文件名传递给 photoOfTheDish():

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)