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

lkaoscv7  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(353)

我正在做一个关于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张图片的名称,第一张是咖喱鸡图片,第二张是巧克力图片,最后一张是肉酱面食图片。
我的项目:当我点击chickencurry按钮时,我想显示鸡肉盘的图像,当我点击巧克力蛋糕时,显示巧克力蛋糕的图像,等等。。。
这是我的密码:

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”的图像。我不知道如何将按钮链接到数据库中他的值集。

dw1jzc5e

dw1jzc5e1#

在…内 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)

相关问题