0

I´m trying to wite a small SqLite Project with a Tkinter Gui. I´m planning to have several windows - all written in seperate py-files/modules. When I try to open/load an image I´m getting an error:

    my_image_A = ImageTk.PhotoImage(Image.open(neuebox.filenameA))
    AttributeError: 'Toplevel' object has no attribute 'filenameA'

I don´t understand the relation between the window 'Toplevel' and a variable like 'filenameA'.

That´s the code:

# This is:
# MODUL01NeueBox006.py

from tkinter import *
from tkinter import filedialog # WARUM AUCH IMMER - ABER DAS MUSS EXTRA IMPORTIERT WERDEN
import sqlite3
from PIL import ImageTk, Image

startseite = None

def open_neuebox():
    neuebox = Toplevel()
    neuebox.configure(background='#587a8f')
    neuebox.geometry("1920x1400")
    neuebox.title("   KTR - Neue Box anlegen")
    p1 = PhotoImage(file = "C:/Benutzer_Marcel_privat/___PROJEKTE_LOKAL_SAVE/PYTHON/KLEINTEILEREGISTER/gui/icon.png")
    neuebox.iconphoto(False, p1)
    
    #CONNECT DATABASE
    conn = sqlite3.connect("KTR.db")
    #CREATE CURSOR
    c = conn.cursor()
    
    # AB HIER BILDER HOCHLADEN

        #MAIN WINDOW WITHDRAW FEHLT HIER

    #BILD_A
    def select_imageA():
        filetypes = [("Bilder als JPEG", "*.jpg")]    #HIER MUSS DIE ECKIGE KLAMMER RUM
        filenameA = filedialog.askopenfilename(title='Open a file', initialdir='C:/Benutzer_Marcel_privat/___PROJEKTE_LOKAL_SAVE/PYTHON/KLEINTEILEREGISTER/gui/Images', filetypes=filetypes)
        my_image_A = ImageTk.PhotoImage(Image.open(neuebox.filenameA))
        Blid_A_Label = Label(Image=my_image_A)
        Blid_A_Label.place(x=80, y=550, width=420, height=236)
    def convertToBinaryData(filenameA):
        # Convert digital data to binary format
        with open(filenameA, 'rb') as file:
            blobData_A = file.read()
            return blobData_A
    #BILD_B
    def select_imageB():
        filetypes = [("Bilder als JPEG", "*.jpg")]    #HIER MUSS DIE ECKIGE KLAMMER RUM
        filenameB = filedialog.askopenfilename(title='Open a file', initialdir='C:/Benutzer_Marcel_privat/___PROJEKTE_LOKAL_SAVE/PYTHON/KLEINTEILEREGISTER/gui/Images', filetypes=filetypes)
        #MAIN WINDOW WITHDRAW FEHLT HIER
    def convertToBinaryData(filenameA):
        # Convert digital data to binary format
        with open(filenameB, 'rb') as file:
            blobData_B = file.read()
            return blobData_B
    #BILD_C
    def select_imageC():
        filetypes = [("Bilder als JPEG", "*.jpg")]    #HIER MUSS DIE ECKIGE KLAMMER RUM
        filenameC = filedialog.askopenfilename(title='Open a file', initialdir='C:/Benutzer_Marcel_privat/___PROJEKTE_LOKAL_SAVE/PYTHON/KLEINTEILEREGISTER/gui/Images', filetypes=filetypes)
        #MAIN WINDOW WITHDRAW FEHLT HIER
    def convertToBinaryData(filenameC):
        # Convert digital data to binary format
        with open(filenameC, 'rb') as file:
            blobData_C = file.read()
            return blobData_C      
    #BILD_D
    def select_imageD():
        filetypes = [("Bilder als JPEG", "*.jpg")]    #HIER MUSS DIE ECKIGE KLAMMER RUM
        filenameD = filedialog.askopenfilename(title='Open a file', initialdir='C:/Benutzer_Marcel_privat/___PROJEKTE_LOKAL_SAVE/PYTHON/KLEINTEILEREGISTER/gui/Images', filetypes=filetypes)
        #MAIN WINDOW WITHDRAW FEHLT HIER
    def convertToBinaryData(filenameD):
        # Convert digital data to binary format
        with open(filenameD, 'rb') as file:
            blobData_D = file.read()
            return blobData_D        

    #CREATE SUBMIT FUNCTION FOR DATABASE
    def submit():
        #CONNECT DATAB#SE
        conn = sqlite3.connect("KTR.db")
        c = conn.cursor()
        
        #INSERT INTO TABLE
        c.execute("INSERT INTO KTR_table VALUES(:lagerortEntry, :fachEntry, :boxnoEntry, :kategorie1eingabeVariable, :kategorie2eingabeVariable, :inhaltText, :BildA_label, :BildB_label, :BildC_label, :BildD_label)",
                {
                    "lagerortEntry": lagerortEntry.get(),
                    "fachEntry": fachEntry.get(),
                    "boxnoEntry": boxnoEntry.get(),
                    "kategorie1eingabeVariable": kategorie1eingabeVariable.get(),
                    "kategorie2eingabeVariable": kategorie2eingabeVariable.get(),
                    "inhaltText": inhaltText.get("1.0",END)      #,
#                     "BildA_label": boxnoEntry.get(),
#                     "BildB_label": boxnoEntry.get(),
#                     "BildC_label": boxnoEntry.get(),
#                     "BildD_label": boxnoEntry.get()
                })
        
        #CLEAR THE TEXT IN BOXES
        lagerortEntry.delete(0, END)
        fachEntry.delete(0, END)
        boxnoEntry.delete(0, END)
        inhaltText.delete("1.0", END)
        kategorie1eingabeVariable.set("  ***Keine***") # RESET TO DEFAULT VALUE
        kategorie2eingabeVariable.set("  ***Keine***") # RESET TO DEFAULT VALUE
        
        #COMMIT AND CLOSE DATABASE CONNECTION
        conn.commit()
        conn.close()

# CREATE QUERY FUNCTION - SPAETER AUSKOMMENTIEREN [ZEIGT ALLE DB-EINTRAEGE IM SHELL AN]
    def query():
        conn = sqlite3.connect("KTR.db")
        #CREATE CURSOR
        c = conn.cursor()
        
        #QUERY THE DATABASE
        c.execute("SELECT *, oid FROM KTR_table")
        records = c.fetchall()
        print(records)
        #COMMIT AND CLOSE DATABASE CONNECTION
        conn.commit()
        conn.close()


    #LABELS
    lagerortLabel = Label(neuebox, text="Lagerort ")
    lagerortLabel.place(x=80, y=80, width=180, height=50)
    fachLabel = Label(neuebox, text="Fach ")
    fachLabel.place(x=80, y=200, width=180, height=50)
    boxnoLabel = Label(neuebox, text="Box No ")
    boxnoLabel.place(x=80, y=320, width=180, height=50)
    kategorie01Label = Label(neuebox, text="Kategorie 1 ")
    kategorie01Label.place(x=270, y=80, width=180, height=50)
    kategorie02Label = Label(neuebox, text="Kategorie 2 ")
    kategorie02Label.place(x=270, y=140, width=180, height=50)
    inhaltLabel = Label(neuebox, text="Inhalt ")
    inhaltLabel.place(x=860, y=80, width=980, height=50)
    # WHITE LINES
    line_01Label = Label(neuebox)
    line_01Label.place(x=80, y=490, width=1760, height=3)
    line_02Label = Label(neuebox)
    line_02Label.place(x=80, y=840, width=1760, height=3)

    #BUTTONS
    abbrechenButton = Button(neuebox, text="Abbrechen", command=neuebox.destroy)
    abbrechenButton.place(x=1460, y=890, width=185, height=50)
    sichernButton = Button(neuebox, text="Sichern", command=submit)
    sichernButton.place(x=1660, y=890, width=185, height=50)
    #CREATE A QUERY BUTTON [SPÄTER AUSKOMMENTIERT]
    queryButton = Button(neuebox, text="Zeige Eintraege der Datenbank", command=query)
    queryButton.place(x=1260, y=950, width=600, height=50)
    #CREATE OPEN IMAGE BUTTONS
    open_imgA_button = Button(neuebox, text="Hinterlege ein Bild\n Filetype: JPEG\n BSV: 16:9\n 800 x 450 Pixel", command=select_imageA)
    open_imgA_button.place(x=80, y=550, width=420, height=236)
    open_imgB_button = Button(neuebox, text="Hinterlege ein Bild\n Filetype: JPEG\n BSV: 16:9\n 800 x 450 Pixel", command=select_imageB)
    open_imgB_button.place(x=525, y=550, width=420, height=236)
    open_imgC_button = Button(neuebox, text="Hinterlege ein Bild\n Filetype: JPEG\n BSV: 16:9\n 800 x 450 Pixel", command=select_imageC)
    open_imgC_button.place(x=975, y=550, width=420, height=236)
    open_imgD_button = Button(neuebox, text="Hinterlege ein Bild\n Filetype: JPEG\n BSV: 16:9\n 800 x 450 Pixel", command=select_imageD)
    open_imgD_button.place(x=1420, y=550, width=420, height=236)
    
    #ENTRIES
    lagerortEntry = Entry(neuebox, justify=CENTER)
    lagerortEntry.place(x=80, y=140, width=180, height=50)
    fachEntry = Entry(neuebox, justify=CENTER)
    fachEntry.place(x=80, y=260, width=180, height=50)
    boxnoEntry = Entry(neuebox, justify=CENTER)
    boxnoEntry.place(x=80, y=380, width=180, height=50)
    inhaltText = Text(neuebox, padx = 15)                       # pad x damit Text nicht ganz am Rand der Box steht
    inhaltText.place(x=860, y=140, width=980, height=290)

    #DROPDOWNS EINGABE
    KATEGORIEN = [
        "***Keine***",
        "Auto", "Baumaterial","Befestigung", "Camping", "Computer", "Drohne",
        "Elektrik", "Elektronik", "Fahrrad", "Farben", "Gas", "Holzbearbeitung",
        "Kamera", "Möbel", "Motorrad", "Roboter", "Schlagzeug", "Schmierstoffe",
        "Spielzeug",  "Wasser", "Werkzeug"
        ]
    
    kategorie1eingabeVariable = StringVar(neuebox)
    kategorie1eingabeVariable.set("  ***Keine***") # default value
    kategorie1eingabeOptionMenu = OptionMenu(neuebox, kategorie1eingabeVariable, *KATEGORIEN)
    kategorie1eingabeOptionMenu.place(x=460, y=80, width=385, height=50)

    kategorie2eingabeVariable = StringVar(neuebox)
    kategorie2eingabeVariable.set("  ***Keine***") # default value
    kategorie2eingabeOptionMenu = OptionMenu(neuebox, kategorie2eingabeVariable, *KATEGORIEN)
    kategorie2eingabeOptionMenu.place(x=460, y=140, width=385, height=50)

    neuebox.mainloop()

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • 1
    `filenameA` is an ordinary variable, not an attribute. Just use `Image.open(filenameA)` – Barmar Oct 26 '21 at 15:49
  • 1
    also don't nest functions, use classes instead if you want to structure your file like this – Matiiss Oct 26 '21 at 15:55
  • After fixing this issue, you may have another issue: [Why does tkinter image not show up if created in a function](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). – acw1668 Oct 27 '21 at 00:51

0 Answers0