Is there a way to use an 'initialdir' option of tkinter filedialog to point an external directories?
I see that it works fine for local lirectories (C:\Program Files...)
but fails for something external (ftp://1.2.3.4 ...)
If not, do you have any alternatives for getting the file path from external ftp server using some GUI filedialog?
This is what I have now:
import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd
root = tk.Tk()
def location():
filename = fd.askopenfilename(initialdir = "ftp://1.2.3.4")
print(filename)
b = Button(root, text="GetFile", command=location)
b.grid(column=0, row=15, sticky='EW')
root.mainloop()
Thanks!
No. Not unless the underlying OS supports mounting an FTP as an external drive. The tkinter filedialog class uses the python OS module which doesn't directly support FTP.
You would have to write your own filedialog with FTP support or find one that someone has already written.
Related
I am building a very simple python project that will allow the user to read a message in a box and then click OK. This will part of a larger program.
The issue is that when I run it in Spyder, the message box ends up behind all other windows. How do I make it be the focus and open in front of all other windows? I am running a Windows 11 computer.
I call the below function from another python script.
# import libraries
# sys / os to change working directory
import sys
import os
# easygui for message boxes
from easygui import *
# Create function to set working directory
def setworkdir():
uni_code = fileopenbox()
print(uni_code)
# Create Welcome Box
def openMsgBox():
msgbox("Welcome to DocPhoto. Click on Ok to get started")
setworkdir()
The script to call it:
from docFunctions import openMsgBox
openMsgBox()
I have seen answers where the programmer is trying to do this in tkinter, but I'm not using tkinter. Is there a way to do it without tkinter?
So basically i want to copy the contents of this file to my clipboard
Ive tried many things like using different modules and such but for some reason i just cant do it
import shutil
owl_hub = open('owlhub.txt','r')
if a == "Owl Hub":
lbl.config(text = owl_hub.readlines())
shutil.copy(owlhub.readlines())
Use tkinter; it comes with python and provides a cross-platform way to change the clipboard
import tkinter as tk
root = tk.Tk()
root.withdraw()
root.clipboard_clear()
with open('owlhub.txt') as f:
root.clipboard_append(f.read().strip())
root.update()
root.destroy()
I have a working application using Python 2.7 and Tkinter that uses these constructs:
from Tkinter import *
import Tkinter
import tkFileDialog
class Window(Frame):
#...
# other functional code
#...
def ChangeCWD(self): #CWD is current working directory
root = Tkinter.Tk()
root.withdraw()
directory = tkFileDialog.askdirectory( ... )
root = Tk()
root.mainloop()
It has labels, buttons, canvas, multiple frames and file dialogue boxes and it all works nicely.
I have begun updating the code to work on Python 3.5 and, so far all functions seem to work except for the file dialog. This is where I have got to so far:
from tkinter import *
import tkinter
import tkinter.filedialog
class Window(Frame):
#...
# other functional code
#...
def ChangeCWD(self): #CWD is current working directory
root = tkinter.Tk()
root.withdraw()
directory = filedialog.askdirectory( ... )
root = Tk()
root.mainloop()
However this code produces the error
"NameError: name 'filedialog' is not defined"
when the filedialog.askdirectory() statement is reached. Could anyone provide any help to understand what I should do to correct the situation please?
As an aside, please be gentle with me! I've always been rather mystified by the various ways of invoking import statements and how to use "tk." or "root." before some function calls. There are simply too many conflicting explanations out on the web that I can't get a clear picture.
You use import tkinter.filedialog, which imports tkinter.filedialog with the namespace tkinter.filedialog, then you try to use filedialog in your code.
Pick one of these two:
change your call to tkinter.filedialog.askdirectory( ... )
change your import to import filedialog from tkinter, which will import tkinter.filedialog with the namespace filedialog.
Note: from tkinter import * might seem like it should import filedialog, but that * does not import submodules unless the package has explicitly specified that they should.
Trying to convert my code to .exe file so that other users can make use of it.
The purpose of this code is to get an interface where the user can enable/disable LAN connection in a single click.
Kindly 'run as administrator' you IDLE to make this code work.
import tkinter as tk
import subprocess as sub
import os
WINDOW_SIZE = "350x100"
root = tk.Tk()
root.title("Windows Adapter Toggler")
root.geometry(WINDOW_SIZE)
#to execute bash command right from python script
tk.Button(root, text="DISABLE Internet", command=lambda: sub.call('netsh
interface set interface name="Ethernet" admin=DISABLED')).pack()
tk.Button(root, text="ENABLE Internet", command=lambda: sub.call('netsh
interface set interface name="Ethernet" admin=ENABLED')).pack()
#os.system(bashCommand)
root.mainloop()
SideNote:
1.Will work if your ethernet port name is 'ethernet',if something else then change the name in the code.Will implement this 'changing name of port'feature later in the code.
2.Already tried converting with pyinstaller and py2exe but didn't happen.
Currently I am using tkinter's askopenfilename in a quicklist editor for Ubuntu to get a file's name and location. Although it works fine, the look and feel is not native.
Is there an easy alternative dialogue window, to navigate and get a file's name and location?
You could try with wxPython FileDialog:
>>> import wx
>>> d = wx.FileDialog(None)
>>> d.ShowModal()
5101
>>>
It gives a more OS specific look
wxPython is arriving soon to py3k as the Phoenix project and there are already snapshots for windows and mac (see my comment below). If you want something more stable you can use pyQt QtGui.QFileDialog.
import sys
from PyQt4 import QtGui
class Dialog(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
filename = QtGui.QFileDialog.getOpenFileName()
print filename
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
You have a more complete example here.
Zenity
Zenity's File Selection Dialog provides an easy and natively looking solution with the --file-selection option. The dialog provides a number of options.
See also Zenity's man pages.
In its simplest form:
#!/usr/bin/env python3
import subprocess
try:
file = subprocess.check_output(["zenity", "--file-selection"]).decode("utf-8").strip()
print(file)
except subprocess.CalledProcessError:
pass
Gtk's FileChooserDialog
Another option is Gtk's FileChooserDialog, which produces, as one might expect, perfectly natively looking file chooser dialog windows.