Launch different tkinter version from python app compiled with pyinstaller on Windows - python

I have a tkinter GUI that allows me to start any kind of program:
# main_app.py
import tkinter as tk
import subprocess
root = tk.Tk()
cmd_entry = tk.Entry(width=50)
cmd_entry.pack(side='left')
def run_script():
sp = subprocess.run(cmd_entry.get().split(), shell=True)
run_btn = tk.Button(text="Run Command", command=run_script)
run_btn.pack(side='left')
root.mainloop()
It looks like this:
I can start another tkinter script from this window, for instance:
# dummy_app.py
import tkinter as tk
root = tk.Tk()
root.mainloop()
It even works when starting dummy_app.py with a different version of python. For example, I can start main_app.py with Python 3.10.8 and run the following:
C:\Path\to\python3.9\python.exe dummy_app.py
However, if I compile main_app.py into an executable with pyinstaller (v5.6.2):
pyinstaller.exe .\main_app.py --onefile
Then I get the following error when trying to run C:\Path\to\python3.9\python.exe dummy_app.py from main_app.exe:
C:/Users/.../AppData/Local/Temp/_MEI76562/tcl/init.tcl: version conflict for package "Tcl": have 8.6.9, need exactly 8.6.12
version conflict for package "Tcl": have 8.6.9, need exactly 8.6.12
while executing
"package require -exact Tcl 8.6.12"
(file "C:/Users/.../AppData/Local/Temp/_MEI76562/tcl/init.tcl" line 19)
invoked from within
"source C:/Users/.../AppData/Local/Temp/_MEI76562/tcl/init.tcl"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $tclfile]"
This probably means that Tcl wasn't installed properly.
python dummy_app.py works fine however.
Why does the tcl version has to be the same when starting the script from the compiled executable? Is there a way around this?

The version of the Tcl support scripts used inside Tkinter must exactly match the version of the Tcl and Tk binary libraries used. Bugfixes may be applied in either location, but the version matching must be exact; they're matched parts a single software product.
The workaround is not to cross-match. If you bind the version in one place (e.g., with linking options) then you must bind it in the other (e.g., by shipping the script files).
We're working on making future versions (8.7 onwards) have this binding done at link time; almost all scenarios don't need significant rewriting of internal scripted parts of Tcl and Tk's implementations.

Related

Import module error when running os.system virtualenv OpenCV

I'm trying to build a simple Python GUI around OpenCV so I can run facial recognition commands easily. I'm using a Raspberry Pi thus Raspbian to do this
It's a case of clicking a button and an os.system command executes allowing various functions to run.
The issue is with trying to run these functions whilst in the Virtual Python Environment. I need to be inside the virtualenv to gain access to the required modules however I understand every time a os.system command is ran, a new shell is created therefore taking me out the virtual environment.
I've looked into running my functions in one os.system however I still get the import module error.
Something that i assumed would take a few mins to build is taking me days.
Any help on this would be amazing.
Thank you.
Here is my current code:
from tkinter import *
import os
from tkinter import messagebox
# creating tkinter window
root = Tk()
root.geometry('500x500')
root.title("Student Attendnace System")
def stillImage():
os.system("/home/pi/.virtualenvs/cv/bin/activate & python recognize_faces_image.py ---encodings encodings.pickle --detection-method hog --image examples/example_01.jpg")
btn3 = Button(root, text = 'Detect Faces From Image', command = stillImage)
btn3.grid(column=1, row=2)
mainloop()
The idea is to enter the virtual environment and execute another python script with added facial detection arguments.
NOTE: running this in the terminal works fine.
I would use the python from the virtualenv directly:
os.system("/home/pi/.virtualenvs/cv/bin/python recognize_faces_image.py ---encodings encodings.pickle --detection-method hog --image examples/example_01.jpg")
To elaborate a script run using a python executable from a virtualenv will look for libraries relative to the python executable, i.e. inside the virtual environment.

How to run python script on atom?

I'm new to using atom and was wondering how to run a python script on it. I have code written at the moment which works perfectly fine in normal python shell, using tkinter, however when I run it through the command line, it says:
import tkinter as tk
ImportError: No module named tkinter
How do I fix this? In my environment variables I have already added python.exe, the file directory to the actual script I'm running and to the python download itself. How do I fix this?
The best way is to load the Jupyter plugin. It's called Hydrogen. Then under the packages menu, you can select Hydrogen/Run all and it will run your python code. there is a keyboard shortcut for doing this which speeds up the process. You can easily check your code as you write it by using the hydrogen option to run a line and go to next line.
As for your Tkinter problem have you loaded Tkinter? You can do this using pip install Tkinter. After that try running your code again.
In python 2.7 and below, the modules's name is Tkinter, while in 3.0 and above, it is tkinter.
Atom (whatever this is) seems to use a 2.7 or below version of python interpreter.

Tkinter for Windows7-64Bit

Can you please let me know how to download tkinter for windows 7 64bit. I am getting an error while i do it:
from Tkinter import *)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
from Tkinter import *
ImportError: No module named 'Tkinter'
If you are using python 3 then the code would use a lower case tkinter.
#python 2
from Tkinter import *
#python 3
from tkinter import *
if that doesn't fix it then you can install it in several ways. check out this page: http://www.tkdocs.com/tutorial/install.html
If you can't get that to work another option is using pip. pip allows you to install modules easily. If you have the newest version if python 2 then it is installed and all you have to do is set a path. Click the start button and right click on computer. Select properties and then advanced settings. Under one of the tabs is a button called environment variables. Click on it and in the new windows scroll down until you see the word "path" on the right. Click on the text to the left of it and put in a ; to put in a new path. I don't know exactly how your file structure is but find python and set a path to the folder called scripts within python. An example would be something like this: "C:\python27\scripts".
There are two possible reasons that you are not able to get Tkinter working:
First Reason
First, this is dependent on if you have installed tk onto your computer. To find out if you have, and what version, run a Python shell via:
% C:\python32\python
From the Python command prompt, enter these two commands:
>>> import tkinter
>>> tkinter._test()
This should pop up a small window; the first line at the top of the window should say "This is Tcl/Tk version 8.5"; Or whatever version has been installed on your computer. If you haven't installed, you'll want to get tkinter on there. I've found the following that may help with that:
http://www.tkdocs.com/tutorial/install.html
Second Reason
If installing tkinter onto your machine isn't the issue, then this is most likely due to what version of python you are using, as this will change some small things.
Python 3:
from tkinter import *
Python 2.7:
from Tkinter import *
If you have python you are trying to run from 2.7 or before but are using it in Python 3, you may find the 2to3 tool helpful for adapting:
https://docs.python.org/2/glossary.html#term-to3
open IDLE Python GUI and type in import tkinter and after that type import_tkinter and if you wanna run a test just type in tkinter._test()
works for python 3.4.3

Script works in IDLE, but .py-file doesn't work

I have a tkinter script, which runs just fine in IDLE. However, when I double click the .py-file from Windows Explorer, the console window flashes half a second and then it exits.
I was able to screenprint the console window. It says:
...etc.etc...
NameError: global name 'simpledialog' is not defined
simpledialog is a module in tkinter which I use in my script. As I do from tkinter import *, there is no need to explicitly write tkinter.simpledialog.
It works in IDLE, why not as .py?
IDLE uses Tkinter as its graphical environment. It is possible that your code is relying on a side effect of an import by IDLE itself. This is especially true if you use IDLE without a subprocess.
The simpledialog module does not import when using from tkinter import *.
Try adding this to your code:
import tkinter.simpledialog as simpledialog
Have you updated your PATH environment variable so that your Python executable is found? You can find more information on how to do here - Using Python on Windows
But you basically need to make sure that the folder containing python.exe (e.g. C:\Python32) is displayed when you type the following command from a prompt:
echo %PATH%
I had exactly the same problem with one of my scripts utilizing Tkinter.
Adding call to mainloop() fixed the issue.
See this tutorial for an example: [http://sebsauvage.net/python/gui/#import1
In my case, in the init function I have
def __init__(self,Width=400, Height=400):
# Create GUI window ------------------------------
win = Tk()
...
in the end of init I added:
win.mainloop()
Now it works by just running the file.
Hope this helps
Similar trouble for me just now, in my first week with python. But I dimly remembered a similar problem with a simple early test script and thought the trouble then was # comments.
So I tried that with my Tkinter infused .py script. It ran fine in IDLE as you say, then only flashed when clicked in windows. But there were a couple # commented lines at the top of file.
I took them all out and it now runs no sweat directly in windows. Have a look .. for #.
Sorry, can't seem to delete this post. Now the files work #comments included. Don't know what's up with that. ..
I found that changing the executable py file to a file.pyw fixed the problem. This tells python to execute it using the pythonw.exe which runs the script without the terminal/console in the background.
Not sure why this works, perhaps some screwed up environment variables from a previous python installation.
Changing the file's extension to pyw instead of py might solve the problem

Apple Python launcher is acting on command key bindings

I've written a Python utility that uses tkinter. I'm running it on a Macintosh. When it is executed, it runs within an apple-supplied Python launcher program (/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app).
My code installs its own menus and I bind to the usual Macintosh command-key equivalents for my edit menu (Command-x, command-c, command-x, command-a, command-z) and for quitting (command-q). My problem is that the Python launcher program is responding to the command key bindings. This is inconvenient for things like pasting because it gets done twice. It's a real problem with quitting because the launcher program kills my program before I can save changed files.
Is there some way I can stop the Python launcher program from acting on command key equivalents? I attempted this: "rootWindow.unbind ('<Command-Key-q>')", but to no avail. The launcher program quits before my code can clean up.
I'm using CPython 3.2 on OS X 10.6.6.
Instead of overriding Tkinter's default key bindings, consider re-mapping Tcl's "exit" command to a custom function. (This is called every time you hit command-q or use the "quit" menu item.)
def save_and_exit():
save_changed_files()
sys.exit()
self.createcommand('exit', save_and_exit)
Besides that, I would recommend removing your copy/paste custom keybinds and letting the library do the work for you. If you're still hell-bent on overriding the defaults, Effbot has a nice tutorial on Tkinter events and bindings.
Is there a specific reason why you are using Python.app for launching? This .app is most likely the reason for misbehaving shortcuts.
If I have understood correctly, this launcher is just a wrapper for default python (/usr/bin/python) with special imports.
If you run from terminal (-v is the key here):
/Library/Frameworks/Python.framework/Versions/5.1.1/Resources/Python.app/Contents/MacOS/Python -v
You will see what it imports at the beginning. Adding these lines to your main file should make the command line launching the same as with the .app.
Note also that python.app is in version 5.1.1.
br,
Juha
First off, /Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app is not Apple-supplied. Most likely you installed Python 3.2 using one of the python.org installers here or from some third-party distributor or possibly you built a framework version from source. In any case, Python.app is a dummy application bundle included in each framework version. Its purpose is to ensure that when you invoke python, even from a command line, it is seen by OS X as a full-fledged GUI application. This is particularly important when using tkinter. The default menus and keybindings you see are supplied by Tcl/Tk, not tkinter. As you've discovered, the right way to go about changing these are to remap the default menus. Be aware that there are currently at least three major variants of Tk available on Mac OS X: Aqua Carbon Tk, Aqua Cocoa Tk, and X11 Tk. There are important details, especially with regards to Mac OS X 10.6, about Python and Tcl/Tk on Mac OS X at the python.org website.

Categories