Set tkinter filedialog to open only executable files - python

Is there a way to set the tkinter filedialog filetypes attribute to open executable files?
I've been using:
filedialog.askopenfilename(filetypes=[("Applications", "*.exe")],)
but it only opens .exe files, not all types of executable files (eg. .APP).
I could add more attributes to filetypes, but it will not work properly on Linux, where any file can be executed, as long as it has the execute permissions (see here).
How can I set thinter's filedialog to only open any kind of executable file?
I am using Python 3.6.4 on Windows 10 and Python 3.5 on Ubuntu 16.
Any help would be greatly appreciated

There's nothing you can do by using the standard dialogs. Tkinter doesn't support listing files by attributes other than their extensions. You'll have to create your own file selection dialog if you want this feature.

Related

(Windows 10 64 bits) Py to Exe Error Python 3.8 (Pyinstaller, cx_Freeze)

Hi I'am a new python user please excuse my python basic level.
I have to update 26/10/2020 the Title to Windows 10 64bits, do to the fact that I have done the downgrade to 3.7 and the error is the same. The EXE file just blinks and closes.
I simplify the code to just the screen, using only the frame, and the same result, EXE just blinks and closes.
I think that the error is related to Pyinstaller and Windows 10 64 bits problems.
I am trying to create an EXE file from Py using Pyhton 3.8 unsuccessfully, using pyinstaller and/or cx_Freeze getting the same errors.
Several errors related to the .png, .ico access and even deleting this from the code, a simple screen with a frame does not open.
Please somebody can help me to solve this.
This is the code, I need to create onefile .exe using .PNG as Background and .ICO on the screen not only as .exe file icon.
from tkinter import *
root=Tk()
#set windows size
root.resizable(width=False, height=False)
root.geometry("925x722")
#set title
root.title("SOFT1)")
#frame 1
f1=Frame(root, width=345,height=475,bg="light
grey",highlightbackground="black",highlightthickness=4)
f1.place(x=20,y=235)
#set a image as BG
Logo=PhotoImage(file="fileIMG.png")
lab6=Label(root, image=Logo)
lab6.place(x=0, y=0)
Currently python 3.8 isn't supported by pyinstaller. I believe cx_Freeze does, but if you don't need any specific feature from python 3.8 then I would downgrade to python 3.7 and try using pyinstaller again. Simpler that way.
There aren't any specific errors for me to base my answer off of, but I'll show you my usual approach to fixing pyinstaller issues. First, I would run the pyinstaller command with the option --debug=all. By using this option, you can see all that goes on behind the scenes when you try and run your program. Then try running the .exe file from the command line, and redirect standard error to a text file:
>your_program_here.exe 2> error.txt
If you look inside error.txt, you should see a lot of information. The most important thing to look for is a program trace-back. It's the same kind of trace-back you'll see when there's an error in the python program you're running. the trace-back will show you which line in your code is raising an error, which is usually the culprit of why your .exe won't work.
Looking from your code, I would try using absolute file paths to open your images. Right now you have it so Pet_toolShare.png is opened from the same folder that your python file
is run from. Pyinstaller places the .exe in the "dist" folder so it wouldn't be able to access any files from outside of it unless you place the .exe in the same folder as your images.

OS "Save As", "Select File", & "Choose Directory" Dialogs

In my script I want a dialog to pop up in which the user can
select a specific file,
choose a directory,
save a file to a specific directory.
All common OS come with these three dialogs, for example the "Save As" dialog in KDE:
Is there a way of calling a function that:
returns the path to the selected file?
returns the path of the chosen directory?
saves some created file to a chosen directory?
It feels like this should be available, however I am struggling to find any packages that can handle this sort of thing. The package os seemed promissing at first, but no luck.
I do not want anything custom-looking using tkinter as is discussed in this thread. That code does, however, do exactly what I want for requirement 2.).
Any hints?
EDIT: I am using Kubuntu 17.10 and Python 2.7.14.
you have it in tkinter as
from tkinter.filedialog import askopenfile
for py3, for py2 the import is a bit different, but the docs will let you know. You can get hold of all standard file dialogs this way.
Principally you will need to make a small gui-app with tkinter.

How can I make my GUI program(tkinter) work on computers with no python installed such that no console window appears?

Here's what I have done so far:
-I made a desktop search program/GUI using python and tkinter.
-Then I used py2exe to convert it to an exe.
-Now the software perfectly works on a machine(windows) without python installed, but the problem is that a creepy black window just appears along with the GUI when the .exe is opened.
Is there any way to make it look less creepy to an end user?
Change the file extension from .py to .pyw
(You must have Python installed for this to work.)
Duplicate:
Hide console window with Tkinter and cx_Freeze
Before converting your file to an exe, change the file extension (by renaming the file) from .py to .pyw.
'.py' files open with the console, whereas '.pyw' files open with no console window.

Create a desktop icon for a Tkinter script

I have written a python script with a Tkinter GUI. I would like to create a desktop icon that will execute this script so that the end-user (not myself) will be able to double-click the icon and have the GUI load, rather than 'run' the script from the terminal or python shell and then have to F5 from there.
Is there a way to do this? I have googled many arrangements of my question but most answers seem to be normal python scripts, not ones which are Tkinter based.
I am using a Raspberry Pi with Wheezy and Python 2.7
Thanks in advance.
I create executables(icons that I click to start the programs I write) using 'py2exe'. I use windows 7 and I am not sure if it would work for you with Raspberry Pi, but a google search may clear that up for you. I will provide the link below. Basically you end up with a folder with the executable(icon) and also some files, without which the executable won't work. It's unfortunate that you get this extra 'baggage', but it's still handy and the best solution I have come across. I don't think there is a much better way, but I am not 100% on that. Interestingly, I found that you could delete most of these baggage files and the executable would still work, it was trial and error, but it didn't take long. If I want to send the folder to someone, I zip it first.
py2exe is here
If you need a 64 bit binary you can get it here, along with, actually, pretty much every other version. get py2exe here also
Besides creating executable file other option is create simple .bat file:
Open notepad
Enter "C:\ProgramData\Anaconda3\python.exe" "C:\Users\Your ID\script.py"
First part is path to python.exe, second to your python script
save file as .bat file, ex. "open_program.bat"
Now simply double click on saved .bat file icon should open your script.
I like using cx-freeze to convert python scripts to exe.
There is very easy to follow documentation to do this. In short, you create a setup.py script that specifies what libraries and packages you want to include or exclude in your application.
You can then run the build in the console and it will create a build folder, in which will be an Application File. You can create a Desktop short cut using send to and selecting desktop.
Documentation link
You can save the script as a .pyw file so the user can click on the file and the GUi would open

python saving .pyw to desktop after running a program

from Tkinter import *
import tkMessageBox
from test2_new import *
When I run this, python is saving a .pyw file of test2_new to the deskop. Why is this happening and how do i fix this? Thank you!
A pyw file is a windows specific format that is generated for GUi scripts. It causes windows to open the script directly in a window instead of launching a console. Most likely you are calling your script from the user desktop location. Try changing directories to your script location. Also if you are double click launching your py then it could also be defaulting to the desktop. Move that pyw to your script location and run that pyw directly.

Categories