Hello!
I have started experimenting with GUI programming using TkInter. Up to now, I have written this code:

import Tkinter, tkFileDialog, Tkconstants 
from Tkinter import * 
root = Tk() 
root.title('Watermark Image Processing 1.0b')
#Options for buttons
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}

#Define asking directory button
dirname = ''
def openDirectory():
	dirname = tkFileDialog.askdirectory(parent=root, initialdir='/home/', title='Select your pictures folder')
	
Button(root, text = 'Select your pictures folder', fg = 'black', command= openDirectory).pack(**button_opt)

#Define asking watermark file button
fileName = ''
def openFile():
	fileName = tkFileDialog.askopenfile(parent=root,initialdir='/home/',title='Select your watermark file', filetypes=[('image files', '.png')])
Button(root, text = 'Select your watermark file', fg = 'black', command= openFile).pack(**button_opt)

root.mainloop()

It simply opens a folder and a file. How can I make the program to show the paths to the selected folder and file? I guess the program stores the selected folder and file paths at dirName and fileName, am I wrong? I would like to use these paths for further actions.

Cheers!

Dani

Recommended Answers

All 12 Replies

Here is example code printing selected file:

import os
from Tkinter import Tk
import tkFileDialog

toplevel = Tk()
toplevel.withdraw()

filename = tkFileDialog.askopenfilename()

if os.path.isfile(filename):
    for line in open(filename,'r'):
        print line,
else: print 'No file chosen'
raw_input('Ready, push Enter')

Hello!
Sorry, it was not what I was looking for. I was looking for a way to show the path to the selected directory/file beside the buttons, on the same window. I do not need to print the file at the console.
Cheers!

Dani

It was example about using the filename.

Sorry for not having replied till now, but I've been away from home. This example has been very useful for me, but I still have the problem. I still don't know how to show the path to the selected file/directory beside the selection buttons. Can you help me?
Cheers!

Dani

what do you mean when you ask how to make the program show the paths to the selected folder and file.

Do you want to show the file's contents, show an explorer window of the file's location or some sort of tree view of the file?

It is much easier than that. If you run my code, you'll see that it generates a panel with 2 buttons to select a directory and a file. What I would like, is to show the user the path to the selected directory/file beside the buttons.
Cheers!

Dani

You could display the path your program gets from the user in a Tkinter Label positioned below the buttons?

How can I do it? I am a very beginner...
Cheers!

Dani

Here is your code reformatted to put the text in the labels of the buttons, change it to your needs:

import Tkinter, tkFileDialog, Tkconstants 
from Tkinter import * 
dirtext='Select your pictures folder'
filetext= 'Select your watermark file'
def openFile():
    filename = tkFileDialog.askopenfilename(parent=root,initialdir='/home/',title=filetext , filetypes=[('image files', '.png')]) ## filename not filehandle
    filebut["text"]= str(filename) if filename else filetext

def openDirectory():
    dirname = tkFileDialog.askdirectory(parent=root, initialdir='/home/', title=dirtext) 
    dirbut["text"] = str(dirname) if dirname else dirtext

root = Tk() 
root.title('Watermark Image Processing 1.0b')
#Options for buttons
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}

#Define asking directory button
dirbut= Button(root, text = dirtext, fg = 'black', command= openDirectory)
dirbut.pack(**button_opt) ## must pack separately to get the value to dirbut

#Define asking watermark file button
filebut = Button(root, text = filetext, fg = 'black', command= openFile)
filebut.pack(**button_opt)

root.mainloop()

i need this code in tcl can anybody help me :(

path = os.filename.dirname(filename)

commented: THis was a help to me. Thanks. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.