How can i open this python file with double click? - python-3.5

There are some files that open with double click . But not this one . I want to open it with double click . it has the following code written inside it:
from tkinter import*
tk=Tk()
tk.title("App Manager")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,width=460,height=500,bg='black',bd=0,highlightthickness=0)
canvas.pack()
tk.update()
def pong():
tk.destroy()
import PONG
def bounce():
tk.destroy()
import BOUNCE
def calculator():
import CALCULATOR
def quit1():
tk.destroy()
b1=Button(tk,text="Play PONG",font= ('Bold',15),bg='brown',fg='gold',command=pong)
b1.pack(side=LEFT)
b2=Button(tk,text="Play BOUNCE",font=('Bold',15),bg='brown',fg='gold',command=bounce)
b2.pack(side=LEFT)
b3=Button(tk,text="CALCULATOR",font=('Bold',15),bg='brown',fg='gold')
b3.pack(side=LEFT)
b4=Button(tk,text="Quit",font=('Bold',15),bg='brown',fg='gold',command=quit1)
b4.pack(side=RIGHT)

Rename the file and give it a .py extension.
You may have to disable "Hide extensions for known file types" under folder properties.

If you want it to run on the double click on any other computer, there is a package called Pyinstaller that will compile a python script with its dependencies so that it can be opened with a double-click and run on any system.
However, it will only run on the same operating system that it was compiled on. So if you want it to run on a windows system it must be compiled on a windows system.
http://www.pyinstaller.org/ check it out.

I found the answer myself. The easiest way to open a file.py extension through double click is to add init() function in a class.
Just create a class with init() function.
That will make every file.py open with double click.

Related

How to manipulate tkinter GUI from an imported file

I try to create a small GUI with tkinter. To make my code more readable I want to split the code in 2 files. One for the GUI information and one for the process informations. Or is it a bad idea?
So I create a gui.py where I import my process informations from program.py.
gui.py:
import tkinter as tk
from program import *
root = tk.Tk()
btn_Start = tk.Button(text="Start", command=start_loop)
btn_Stop = tk.Button(text="Stop", command=stop_loop, state=tk.DISABLED)
btn_Start.grid(row=1, column=0)
btn_Stop.grid(row=1, column=1)
root.mainloop()
program.py:
def start_loop():
print('disable Start button and enable Stop button')
# what is the code to disable the start button and enable the stop button?
def stop_loop():
print('disable Stop button and enable Start button')
# what is the code to disable the stop button and enable the start button?
How do I tell the button the disable/enable information in my program.py file? I do not understand how I get the information from the gui to the program and back to the gui?
Thanks for your help
For such a small program, it is overkill.
Looking at the tkinter programs I've written, all of them are between 200−300 lines. This is including headers, comments and blank lines. The number of actual code lines is 100−200.
In my opinion that is small enough to comfortably handle in an editor in one file.
Looking over most of my source code repositories, the longest Python files tend to top out at around 230 lines of actual code.
Keeping all the code in one file has significant advantages for a program when you need to install it. Just copy the file and you're done. No need for modules and a setup.py.

How can i open a file using a python script i coded?

I would like to open a file such as an image file using a python script, and by doing so, pass the file name and location into the script when i choose to open the image in my script.
clicks "open image in another program" in windows 10
import tkinter as tk
filename = fileopen.name()
filelocation = fileopen.path()
window = tk.Tk()
imagepic = tk.PhotoImage(file=filelocation)
picture = tk.Label(window, image=imagepic)
window.mainloop()
I'm pretty sure theres a module that allows for this but i just cannot find it.
I'm not entirely sure I understood your question correctly, but for dealing with the operating system you would use the module os:
import os
os.startfile(filelocation) # Open file in win10 default program
What I meant originally was how to go about registering a context menu option in windows explorer allowing the user to right click a file and select "Open with " - passing the file into the script as an argument.
In order to achieve this, you can follow this guide, supplying this command:
python -m <absolute/path/to/yourscript> %1
The %1 will be the file as an argument.

How to open program with key input

I want to open the calculator program when I type "Open calculator " . I researched a lot , but didn't get the answer I wanted.Can anyone please answer my question.
There are 2 ways to do this. One way would be access the cmd using Python which can be done by using os module. When you try to open the calculator from your command prompt, you probably type calc. Instead of manually doing this, you can have your Python code do it for you, this is how:
import os
os.system('calc')
The second way is very similar to the first one, except that this method opens another command prompt window so the window in which you're running the python code is not disturbed.
import subprocess
subprocess.Popen("calc",stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,shell=True)

I need to add code to tkinter exe program?

I want to be able to show the GUI user the code used to do background calculations. However copying and pasting and using "\n" in a text box takes forever. Now I know you can save the code as a pdf. Is there a simple way to attach the pdf to the program and the code still be readable even when it is moved to a different computer.
Main issues:
- How to import and attach a pdf to a button...
- How to include pdf into program so it is readable on any pc...
Thanks in advance.
Packaging:
You can attach your code as pdf file with your program like this using pyinstaller
On Windows:
pyinstaller --add-data="relative/full_path_to_pdf;." my_script.py
On Linux:
pyinstaller --add-data="relative/full_path_to_pdf:." my_script.py
This will pack your pdf file and copy it to the same folder as the .exe package(in case of single file, it will extract it to the temp path along with main .exe which can be accessed with sys._MEIPASS) or You can change the extraction path instead of using '.' Read more here.
In Code:
You can add this type of button in your UI, to open the pdf file with the default viewer of Windows/Linux(same behavior as when you double click the file)
source_code_btn = Button(root, text="Source", command=lambda: subprocess.Popen('{} {}'.format(
"start" if os.name=="nt" else "xdg-open \"$#\" 2>/dev/null",
relative/full_path_to_pdf_file), shell=True))

Vim Python: better way to call python code?

function! My_dir(fname)
python3 << EOF
import vim
blah = vim.eval('a:fname')
if str(blah) == 'None':
cb = vim.current.buffer
cb[0] = ' .vimrc'
cb.append(' .vim/plugins/')
cb.append(' newbie.vim')
EOF
endfunction
Is there a cleaner way to call python code? Any way to stick common imports in a file, vs doing import vim everytime - within the python block?
Create a X.py file and import every thing that you use commonly. For example as I usually need os and sys libraries in my programs, my CommonModules.py is this:
import os
import sys
Then put this file in a directory and create an Environmental Variable and name it PYTHONSTARTUP. and assign the path of that file as its value.
Now every time you open python, the CommonModules.py called automatically.
How to set environment variables :
Windows Vista and Windows 7 users
From the Desktop, right-click the Computer icon and select Properties. ...
Click the Advanced System Settings link in the left column.
In the System Properties window, click on the Advanced tab, then click the Environment Variables
button near the bottom of that tab.
And for linux Users:
https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables

Resources