I am using the following code to enable the user to chose a folder:
from tkinter import Tk
from tkinter.filedialog import askdirectory
path = askdirectory(title='Select Folder') # shows dialog box and return the path
print(path)
However, the 'Browse' window does not display the Android phone connected via USB, although I can see it via windows explorer.
How can this be enabled?
Related
I made my python application the default application for the .txt extension using the Inno Setup. But I have two problems running the application by double-clicking a .txt file. The first problem is that the application uses a .ico image, and when I open the application by double-clicking a file, it searches for the image in the .txt file directory, not in the application directory. This is how I import the image:
`
import tkinter as tk
import os
import sys
app_folder = os.path.abspath(".")
def resource_path(relative):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return os.path.join(relative)
class App:
def __init__(self, parent):
parent.iconbitmap(resource_path(f'{app_folder}\\images\\logo.ico'))
parent.geometry("1320x900")
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
`
This is the error:
_tkinter.TclError: bitmap "C:\Users\Filipe\Desktop\images\logo.ico" not defined.
How can I make the application search for the logo in the application installation folder?
The second question is how to import the double-clicked .txt file into the application?
I think that I should call a function when the application is started by double-clicking a .txt file, but I have no idea about how to trigger this function. Any idea is welcome.
ps: I'm using pyinstaller to bundle the application in a .exe file.
Running the following code opens a file dialog in the directory of __main__ instead of "Downloads". I've seen examples of tk using shell shortcuts, so my understanding is this should work. What am I doing wrong?
from tkinter.filedialog import askopenfilename
filepath = askopenfilename(initialdir='shell:Downloads',
title='Select File',
filetypes=(('csv files', '*.csv'),('all files', '*.*')))
I've also tried the following which mirrors an example I found from 2 years ago which supposedly worked, and tried different shell shortcuts, which all act as though an initial dir is not specified:
import tkinter as tk
import tkinter.filedialog as filedialog
root = tk.Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir='shell:Downloads',
title='Select File',
filetypes=(('csv files', '*.csv'),('all files', '*.*')))
Python 3.8
TK 8.6.10
My source for using shell shortcuts in accepted answer at:
Specify "this pc" as initial dir in tkinter open file popup
I'm trying to open Adv5KTCP.exe (path shown in code) but this exe actually opens three more avi.files as shown in image below. This causes the error. I've tried os, subprocess & pywinauto to call but to no avail. I've also tried adding the files path to environmental variable but I think that makes no sense. Opening the exe from command prompt does not work too. However, the exe can be opened manually by double clicking the exe file (like the usual). I just need it to be automated.
AVI Files:
The Error:
Double clicking the exe would open this window:
Here is my code:
import os
import sys, logging
import subprocess
import ctypes
from pywinauto import Application
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
exe = "Adv5KTCP.exe" #path set in environment variable: "C:\Program Files (x86)\Advantech\ADAM-5000TCP-6000 Utility\Program"
os.startfile(exe)
# subprocess.Popen([exe])
# application = Application(backend="uia").start(exe)
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
Does anybody have any idea on how I could go about this? Thank you in advance for your response.
New Findings:
I tried to cut the three AVI files to desktop & set desktop path to system variable & the exe gives the same error. However when i cut the exe file to desktop with the avi files as shown below, it works! Even when the other required files are not in desktop but path's set to system variable. Which means the exe is somehow registering the paths of the avi upon click, location/position or some sort which I'm not sure of.
I want to launch a shortcut named blender.ink located at "D://games//blender.ink". I have tryed using:-
os.startfile ("D://games//blender.ink")
But it failed, it only launches exe files.
The Python os.startfile function should work fine, but you need to specify a .lnk extension to be a valid Windows shortcut file:
import os
os.startfile (r"D:\games\blender.lnk")
If you need to wait for the application to complete before continuing, then a different approach would be needed as follows:
import win32com.shell.shell as shell
import win32event
se_ret = shell.ShellExecuteEx(fMask=0x140, lpFile=r"D:\games\blender.lnk", nShow=1)
win32event.WaitForSingleObject(se_ret['hProcess'], -1)
To use Tkinter to open a dialog box I have the following Python 2.7 code:
from Tkinkter import Tk
from tkFileDialog import asksavesfinename
root = Tk().withdraw()
f = asksaveaskfilename()
This works just fine if I run the program under Idle.
However, if I run it, as root, from the LXTerminal, it fails with the exception “Client is not authorized to connect to Server…..”
Any help will be appreciated, Thanks.