im trying to make only the file name show up using filedialog
here's the example :
import tkinter as tk
import pygame
from tkinter import filedialog
root = tk.Tk()
controls_frame = tk.Frame(root)
controls_frame.pack()
def add_name():
name = filedialog.askopenfile()
names.insert(filedialog.END,name)
names = tk.Listbox(root,bg="black",fg="red",width=60,height=6)
names.pack(pady=0,side='bottom')
add_name_btn = tk.Button(controls_frame , text='add name' , command=add_name)
add_name_btn.grid(column=3,row=0,padx=20)
root.mainloop()
it shows the full directory to the file, which i dont want.
how do i remove the directory that it shows on box?
The module os can help with this:
import os
def add_name():
name = os.path.basename(str(filedialog.askopenfile()))
name = name.replace("' mode='r' encoding='UTF-8'>", "")
names.insert(filedialog.END,name)
You can do it like this.
I found this solution on another post.
import tkinter
from tkinter import filedialog
def filename123():
global filename1, nopath1
filename1 = filedialog.askopenfilename()
nopath1 = filename1.split("/")[len(filename1.split("/"))-1]
filename123()
Related
I've been trying to get the printed output into a variable. Whenever I call the function it just simply repeats the button process. I tried to utilize global, but it doesn't seem to work.
Help pls thanks.
from tkinter import *
from tkinter import filedialog
import tkinter as tk
def openFile():
filepath = filedialog.askopenfilename(title="Open for me")
print(filepath)
window.destroy()
window = tk.Tk()
window.title("Insert Excel File")
window.geometry("200x200")
button = Button(text="Open",command=openFile,height=3,width=10)
button.pack()
window.mainloop()
print , outputs the string to stdout it does not store the value in a variable, you'll need to store it yourself.
Since you're using a callback you can't return the value. The best way to implement this is to use classes, the bad way is to use a global variable to store the result
I'm assuming you are not familiar with classes, so a possible solution would be
from tkinter import *
from tkinter import filedialog
import tkinter as tk
path = ""
def openFile():
global path
path = filedialog.askopenfilename(title="Open for me")
window.destroy()
window = tk.Tk()
window.title("Insert Excel File")
window.geometry("200x200")
button = Button(text="Open",command=openFile,height=3,width=10)
button.pack()
window.mainloop()
# do something with path e.g. print
print(path)
Using Python Tkinter I am trying to get the directory path of selected Folder. I do not want to load a file or navigate to a file but get the folder path like
How can I do this?
from Tkinter import *
from tkFileDialog import askopenfilename
def callback():
name= askopenfilename()
print name
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
Update
from Tkinter import *
from tkFileDialog import askopenfilename
from tkinter import filedialog #for Python 3
def callback():
name= askopenfilename()
directory = filedialog.askdirectory()
print directory
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
You can use askdirectory from filedialog as follows:
from tkinter import filedialog #for Python 3
directory = filedialog.askdirectory()
Ok Looks like I find the solution on my own. Putting here which might help someone else in future.
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
dirname = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
print(dirname)
This is for my assignment.
I was given a template like this... def_choosefile() :
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
return filedialog.askopenfilename()
So if i get this correct, it'll prompt a dialog window, asking to select a file. And when a file is selected, the program is supposed to tell what files did it pick. Using these:
selected_file = choose_file()
print ("Selected file: {}".format (selected_file))
after that, how do i make the program read the files normally? Normally i would use:
infile = open('text')
infile.readline()
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
def choose_file():
return filedialog.askopenfilename()
selected_file = choose_file()
with open (selected_file,'r') as readfile:
lines = readfile.read().splitlines()
for line in lines[0:2]:
print (line)
I am looking to create a reminder that prompts the user to select a file if they've completed it. After selecting the file they want, I would like the script to save that file to a specific folder. This is what I have so far...
import tkinter
import time
import win32api
from tkinter import messagebox
dialog_title = 'Complaince calendar'
dialog_text = 'Have you completed the monthly spreadsheet?'
answer = messagebox.askquestion(dialog_title, dialog_text)
if answer == 'yes':
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
else: # 'no'
win32api.MessageBox(0, 'Make sure you get that done', 'Compliance calendar')
How can I save the file_path to a specified folder?
Thanks!!
I'm making a program that you use the askopenname file dialog to select a file, which I then want to save the directory to a string so I can use another function (which I already made) to extract the file to a different location that is predetermined.
My button code that opens the file dialog is this:
`a = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))`
This should be what you want:
import tkinter
import tkinter.filedialog
import getpass
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
# Get the file
file = tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user)
# Split the filepath to get the directory
directory = os.path.split(file)[0]
print(directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()
If you know where the file actually is, you could always just ask for a directory instead of the file using:
from tkFileDialog import askdirectory
directory= askdirectory()
Then in the code:
import tkinter
import tkinter.filedialog
import getpass
from tkFileDialog import askdirectory
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
directory= askdirectory()
print (directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()