1 year ago
#355113
Gábor Szalma
How to upload file in python Tkinter to current directory?
I am making a GUI in python with Tkinter. I can browse a file, print the path for it, but how is it possible to save the browsed file into a specific folder (for example the current folder /home/pi/Desktop/gui). Also I want to add the path for the saved file into the database.
def open():
global my_image
window.filename= filedialog.askopenfilename(initialdir="/home/pi/Desktop/gui", title="Select a file", filetypes=(("jpg files", "*jpg"),("all files", "*.*")))
my_label = Label(window, text=window.filename)
my_label.place(x=260,y=140)
print(os.path.basename(window.filename))
complete_name = os.path.join(save_path, os.path.basename(window.filename))
print(complete_name)
def savedata():
conn=sqlite3.connect('database.db')
c=conn.cursor()
c.execute('INSERT INTO users(name,password,audiofile) VALUES (?,?)',(name_entry.get(),name_entry2.get()))
conn.commit()
print("saved")
Here is the full code what I have already:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import ImageTk, Image
import sqlite3
import os
import os.path
window = Tk()
window.title("GUI")
label = Label(window, text="GUI", font=('arial',20,'bold'),bg="black",fg="white")
label.pack(side=TOP,fill=X)
label = Label(window,text="",font=('arial',15,'bold'),bg="black",fg="white")
label.pack(side=BOTTOM,fill=X)
save_path = "/home/pi/Desktop/proba/"
def open():
global my_image
window.filename = filedialog.askopenfilename(initialdir="/home/pi/Desktop/gui", title="Select a file", filetypes=(("jpg files", "*jpg"),("all files", "*.*")))
my_label = Label(window, text=window.filename)
my_label.place(x=260,y=140)
print(os.path.basename(window.filename))
complete_name = os.path.join(save_path, os.path.basename(window.filename))
print(complete_name)
def create():
conn=sqlite3.connect('database.db')
c=conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS users(id integer primary key autoincrement, name TEXT, password TEXT, audiofile TEXT, sqltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)")
conn.commit()
conn.close()
create()
label = Label(window,text="Name",font=('arial',13,'bold'))
label.place(x=30,y=60)
name_entry=StringVar()
name_entry=ttk.Entry(window, textvariable=name_entry)
name_entry.place(x=170,y=60)
name_entry.focus()
label = Label(window,text="Password",font=('arial',13,'bold'))
label.place(x=30,y=100)
name_entry2=StringVar()
name_entry2=ttk.Entry(window, textvariable=name_entry2)
name_entry2.place(x=170,y=100)
label = Label(window,text="File upload:",font=('arial',13,'bold'))
label.place(x=30,y=140)
def savedata():
conn=sqlite3.connect('database.db')
c=conn.cursor()
c.execute('INSERT INTO users(name,password,audiofile) VALUES (?,?)',(name_entry.get(),name_entry2.get()))
conn.commit()
print("saved")
btn = ttk.Button(window,text='Save Data', command=savedata)
btn.place(x=170,y=220, width=125, height=30)
my_btn = ttk.Button(window, text="Open file", command=open)
my_btn.place(x=170,y=140, width=80, height=25)
window.geometry('640x350')
window.resizable(False,False)
window.mainloop()
python
database
tkinter
file-upload
filedialog
0 Answers
Your Answer