Python 3 tkinter Photoimage "pyimage1" doesn't exist - python-3.x

PhotoImage: Tkinter PhotoImage objects and their idiosyncracies
http://tkinter.unpythonic.net/wiki/PhotoImage
I tested the example with python 2.7.9, 3.2.5, 3.3.5, 3.4.3 in 32bit and 64bit.
(Win 8.1 64bit)
The code works. ( even without pillow )
( in python 3.4.3 64bit I had first an error message.
I've completely uninstalled 3.4.3 and then reinstalled.
Now, the example works also with 3.4.3 64 bit )
# basic code from >>
# http://tkinter.unpythonic.net/wiki/PhotoImage
# extra code -------------------------------------------------------------------------
from __future__ import print_function
try:
import tkinter as tk
except:
import Tkinter as tk
import sys
import platform
print ()
print ('python ', sys.version)
print ('tkinter ', tk.TkVersion)
print ()
print (platform.platform(),' ',platform.machine())
print ()
# basic code -------------------------------------------------------------------------
root = tk.Tk()
def create_button_with_scoped_image():
# "w6.gif" >>
# http://www.inf-schule.de/content/software/gui/entwicklung_tkinter/bilder/w6.gif
img = tk.PhotoImage(file="w6.gif") # reference PhotoImage in local variable
button = tk.Button(root, image=img)
# button.img = img # store a reference to the image as an attribute of the widget
button.image = img # store a reference to the image as an attribute of the widget
button.grid()
create_button_with_scoped_image()
tk.mainloop()

This is done by replacing the root.Tk() by root.Toplevel()

Your script works fine for me with exactly the same version of Python 3.4 running on windows 7. I suspect the only difference is that I have also installed Pillow by downloading the wheel package from this repository. I think this includes the image support you need.
As a side note: on windows use the ttk package widgets to get buttons that actually look correct for the platform. Just import tkinter.ttk as ttk then use ttk.Button instead of tk.Button.
Update
Given the same code is working on my machine and not yours I thought I would add how I obtained this version. I installed python using chocolatey (choco install python) then added Pillow, lxml, numpy, requests and simplejson from the gohlke site.
Checking the Tk version I see that I get Tk 8.6.1 so I suspect you are picking up a Tcl/Tk installation installed locally and not shipped with your version of Python. Try ensuring no Tk installation is in your PATH and see if that resolves the problem. My output was:
python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
tkinter 8.6
Test Tk location:
>>> from tkinter import *
>>> root = Tk()
>>> root.eval('set tk_library')
'C:\\opt\\Python34\\tcl\\tk8.6'
I actually persuaded python to install to c:\opt\Python I think by using choco install python -ia "TARGETDIR=c:\opt\Python" but I doubt that is relevant.

You must close all open windows, which hang in the memory. If you have an error and did not destroy the window the next time you start not started there several windows. Check!

Related

How to reliably convince matplotlib to use PySide2 backend when running from Anaconda (Spyder)

I am creating a PySide2 application which uses matplotlib. I am running this application from Spyder in an environment with PySide2 installed. This is causing the application to be run from the iPython console. Somewhere along the line, PyQt5 is imported, which I am attempting to purge in order to convince matplotlib that I really do want to use PySide2, NOT PyQt5. Something like following was working until very recently and I am not really sure why it has stopped, but safe to say this method is unreliable. How can I absolutely convince matplotlib that I am wanting PySide2?
I have tried setting the environment variable QT_API in the operating system (Windows 10), but in this case Spyder itself refuses to open.
import sys
import os
ps = list(filter(lambda x: 'PyQt5' in x, sys.modules))
for p in ps:
print(f"purging module {p}")
sys.modules.pop(p)
# matplotlib.__init__ uses this
os.environ["MPLBACKEND"] = "PySide2"
# matplotlib.backends.qt_compat uses this
os.environ["QT_API"] = "PySide2"
import PySide2.QtCore
assert "PyQt5.QtCore" not in sys.modules
assert "PySide2.QtCore" in sys.modules
# rcParams has the right idea
from matplotlib import rcParams
print(rcParams["backend"])
# qt_compat has the WRONG idea!
import matplotlib.backends.qt_compat as qt_compat
print(qt_compat.QT_API)
# The FigureCanvasWidget is of the wrong (PyQt5) type
from matplotlib.backends.backend_qt5agg import FigureCanvas
import inspect
print(inspect.getmro(FigureCanvas))
To answer this question, the reason that it stopped working was because I had set 'activate support' for Matplotlib graphics in the ipython tab under Spyder settings. After unchecking this, the above works.

Cannot import Tkinter on visual studio code?

Ok, I am having a weird issue going on. From what I understand Tkinter is supposed to be built in with python 2 and 3.
I can import and use Tkinter just fine in my terminal under python3 as well as with IDLE3. However, when I try to import Tkinter in Visual Studio Code I get an "ImportError: No module named 'tkinter'.
The same issue was happening in Pycharm also but I had my interpreter set to the same as my terminal and not a project-specific interpreter.
I have tried the following:
import tkinter
from tkinter import *
try:
import tkinter
except ImportError:
import Tkinter as tkinter #Even though I'm using python3
None of this is working, Any idea why?
Also I am using Linux Mint.
I've been stuck with the same issue for weeks now where my VSC says there are no tkinter module but in shell and terminal it runs fine.
Solution: I found out that in the left buttom corner there is an interpreter that runs as Python 2 just click and select Python 3
In windows you could have selected to not install the needed tkinter components when python was installed (its optional in the installer).
Try running the installer again, and make sure these components are selected.
If your interpreter is PHP2 it is spelled "Tkinter" in the code. (uppercase)
If your interpreter is PHP3 then you should spell it like "tkinter"
When you were installing python there is a option. Install tkinter and ide just check it and then reinstall python after that close and open vs code. Here you go!

py2app tkinter 'Detected missing constraints' error

I am trying to package an existing python script that uses tk to choose a file and ask for a value into an app that includes all the dependencies.
I am using MacOS 10.12.5, homebrew python 3.6.1 and py2app 0.14.
The following works fine as a script, but when converted to an app (or -A alias app) I get an error Detected missing constraints for <private>... on the console. Simplified version that still produces the same error:
#!/usr/bin/env python3
# encoding: utf-8
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
root.withdraw()
the_answer = simpledialog.askstring("Input", "What is the answer?",)
Am I missing something or is this a bug? If it is a bug is it a tk or py2app bug?

py2app with anaconda Python 3 "missing constraints" error

I am trying to compile a bare-bones wxPython project in Python 3 using py2app.
py2app 0.13 <pip>
wxPython 4.0.0a3.dev3059+4a5c5d9 <pip>
Python 3.5.3 :: Anaconda custom (x86_64)
OS X 10.12.4
Here is my setup.py file:
from setuptools import setup
import sys
sys.setrecursionlimit(2000) # need this to prevent max recursion depth reached
APP = ['wx_py2app.py']
DATA_FILES = []
OPTIONS = {'plist': {'PyRuntimeLocations':
['/Users/***/anaconda/lib/libpython3.5m.dylib',
'/Users/***/Python/simple_examples/dist/wx_py2app.app/Contents/Frameworks/libpython3.5m.dylib']}
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Here is my wxPython code:
import wx
class simpleapp_wx(wx.Frame):
def __init__(self, parent, wx_id, title):
wx.Frame.__init__(self, parent, wx_id, title, size=(100, 100))
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = simpleapp_wx(None, -1, 'my application')
app.MainLoop()
I had to make a few patches. First, I had to hard code /Users/***/anaconda/lib/libpython3.5m.dylib into py2app/build_app.py, or else it would look for libpython3.5.dylib instead and crash here:
ValueError: '/Users/***/anaconda/lib/libpython3.5.dylib'
Second, I patched PyQt5/uic/port_v2/load_plugin.py to avoid a syntax error, and PyQt5/uic/port_v2/ascii_upper.py which was using deprecated string.maketrans.
My bare-bones project works fine in alias mode, but in non-alias mode the project compiles, but then when I open it it immediately terminates:
Detected missing constraints for <private>. It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.
As a side note, my project does work without this problem when I compile it in alias mode. Otherwise, I haven't been able to figure out anything about this error. How can I debug or fix this?

How to determine what version of python3 tkinter is installed on my linux machine?

Hi have been scavenging the web for answers on how to do this but there was no direct answer. Does anyone know how I can find the version number of tkinter?
In Python 3, it's tkinter with a small t, and you need to import it. Thus:
>>> import tkinter
>>> tkinter.TkVersion
8.6
If you didn't import it, you'd get the error you mentioned.
tkinter.TclVersion and tkinter.TkVersion described in other answers provide the major and minor Tcl/Tk versions but not the patch number.
On the other hand, the info patchlevel Tcl command returns the version in the major.minor.patch format.
import tkinter
tcl = tkinter.Tcl()
print(tcl.call("info", "patchlevel"))
# Output:
# 8.6.10
The following should be preferred to call the command from a Tkinter app, which already has an associated Tcl interpreter:
import tkinter
root = tkinter.Tk()
...
print(root.tk.call("info", "patchlevel"))
# Output:
# 8.6.10
Type this command on the Terminal and run it.
python -m tkinter
A small window will appear with the heading tk and two buttons: Click Me! and QUIT. There will be a text that goes like This is Tcl/Tk version ___. The version number will be displayed in the place of the underscores.
You cal so invoke TclVersion:
>>> import tkinter
>>> tkinter.TclVersion
8.6
Tested for Python 3.6.5 and Python 3.5.2 (Ubuntu 16.04.4):
Run Tkinter.TclVersion or Tkinter.TkVersion and if both are not working, try Tkinter.__version__
In the terminal try as follows:
python -m tkinter or python3 -m tkinter

Resources