tobmes
Goto Top

Tkinter Button Klick und Parameterübergabe

Hi,

ich spiele gerade mit Python rum und bin dabei auch auf Tkinter gestossen. Ich habe das folgende Problem. Wenn ich auf den Button Select klicke, dann öffnet sich ein Dialog zum Öffnen einer Datei. Ich bräuchte jetzt den Pfad dieser Datei für eine andere Funktion. Leider bekomme ich das aber nicht so wirklich hin. Hier mal mein bisheriger Code

    def selPic():
        global file
        file = askopenfilename(filetypes=(('JPEG Picture', '*.jpg'),  
                                               ('PNG Picture', '*.png'),  
                                               ('All Files', '*.*')))  
        if sys.platform == "win32":  
            Label(upload, font='bold', text=str(file).split('/').pop()).grid(row=0, column=1)  

            # Label(upload, text=file).grid(row=0, column=1)
            print("filename:", str(file).split('/').pop())  
            print('Path:', file)  

        else:
            Label(upload, text=str(file).split('\\').pop()).grid(row=0, column=1)  

    def resizePic(img_name, img_path):
        print(file.get())
        #if len(e_img_name.get()) == 0:
        #    Label(upload, text="Filename", bg="red").grid(row=1) 
        Picture.resize(None, img_name, img_path)

        #else:
         #   pass



    upload = Tk()
    upload.focus_force()
    upload.title("Upload Picture")  
    # Labels
    Label(upload, text="Choose a picture to upload").grid(row=0)  
    Label(upload, text="Filename").grid(row=1)  
    Label(upload, text="Password").grid(row=2)  
    # Entries
    img_name = StringVar(value="Test")  
    file = StringVar()
    #e_path = Entry(upload, textvariable=file)
    e_img_name = Entry(upload, textvariable=img_name)
    e_pass = Entry(upload, show='*')  
    # Buttons
    Button(upload, text="Select", command=selPic).grid(row=0, column=2, sticky=W, pady=4)  
    Button(upload, text="Resize", command=lambda: resizePic(e_img_name.get(), file.get)).grid(  
        row=1, column=2, sticky=W, pady=4)

    # Place entries
    #e_path.grid(row=0, column=1)
    e_img_name.grid(row=1, column=1)
    e_pass.grid(row=2, column=1)

    mainloop()

Ich habe versucht den Pfad in der globalen Variablen file zu speichern und dann wieder für die Funktion resize zu verwenden. Wie gesagt, leider ohne Erfolg. Kann mir da evtl. jemand helfen?

Danke schon mal

Content-ID: 348253

Url: https://administrator.de/forum/tkinter-button-klick-und-parameteruebergabe-348253.html

Ausgedruckt am: 21.02.2025 um 11:02 Uhr

133883
133883 05.09.2017 um 18:16:14 Uhr
Goto Top
from tkinter import *
filename = filedialog.askopenfile()
print(filename.name)