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-Key: 348253

Url: https://administrator.de/contentid/348253

Printed on: April 26, 2024 at 14:04 o'clock

Mitglied: 133883
133883 Sep 05, 2017 at 16:16:14 (UTC)
Goto Top
from tkinter import *
filename = filedialog.askopenfile()
print(filename.name)