For Windows, I think the problem is you didn’t install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html
I suggest install ipython, which provides powerful shell and necessary packages as well.
回答 1
您可以使用
import matplotlib
matplotlib.use('agg')import matplotlib.pyplot as plt
Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:
On Ubuntu, early 2018, there is no python3.6-tk on ubuntu’s (xenial/16.04) normal distributions, so even if you have earlier versions of python-tk this won’t work.
My solution was to use set everything up with python 3.5:
$ python
Python3.7.4(default,Sep22019,20:44:09)[GCC 5.4.020160609] on linux
Type"help","copyright","credits" or "license"for more information.>>> import tkinter
Traceback(most recent call last):File"<stdin>", line 1,in<module>ModuleNotFoundError:No module named 'tkinter'>>> exit()
注意。python3-tk已安装。但是不是python3.7-tk。
$ sudo apt install python3.7-tk
Reading package lists...DoneBuilding dependency tree
Reading state information...DoneSuggested packages:
tix python3.7-tk-dbg
The following NEW packages will be installed:
python3.7-tk
0 upgraded,1 newly installed,0 to remove and 34 not upgraded.Need to get 143 kB of archives.After this operation,534 kB of additional disk space will be used.Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]Fetched143 kB in0s(364 kB/s)Selecting previously unselected package python3.7-tk:amd64.(Reading database ...256375 files and directories currently installed.)Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2)...Setting up python3.7-tk:amd64 (3.7.4-1+xenial2)...
安装后,一切都很好。
$ python3
Python3.7.4(default,Sep22019,20:44:09)[GCC 5.4.020160609] on linux
Type"help","copyright","credits" or "license"for more information.>>> import tkinter
>>> exit()
For the poor guys like me using python 3.7. You need the python3.7-tk package.
sudo apt install python3.7-tk
$ python
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()
Note. python3-tk is installed. But not python3.7-tk.
$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
tix python3.7-tk-dbg
The following NEW packages will be installed:
python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...
After installing it, all good.
$ python3
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()
Sometimes (for example in osgeo4w distribution) tkinter is removed.
Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
使用支持的tcl / tk重新编译python,例如:
# download the source code of python and decompress it first.
cd <your-python-src-dir>./configure --prefix=/home/xxx/local/python \
--with-tcltk-includes=/home/xxx/local/tcl-tk/include \
--with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make
make install
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
Recompile python with tcl/tk supported, for example:
# download the source code of python and decompress it first.
cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
--with-tcltk-includes=/home/xxx/local/tcl-tk/include \
--with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make
make install
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=20)
out_png ='path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)
Traceback(most recent call last):File"example.py", line 7,in<module>
plt.scatter(x, y, s=20)File"/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241,in scatter
ax = gca()File"/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928,in gca
return gcf().gca(**kwargs)File"/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578,in gcf
return figure()File"/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527,in figure
**kwargs)File"/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84,in new_figure_manager
return new_figure_manager_given_figure(num, figure)File"/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92,in new_figure_manager_given_figure
window =Tk.Tk()File"/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810,in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
I am running a simple python script in the server:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(60)
y = np.random.randn(60)
plt.scatter(x, y, s=20)
out_png = 'path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)
I try to use the command python example.py in this server which has matplotlib 1.5.1 installed it fails with the error:
Traceback (most recent call last):
File "example.py", line 7, in <module>
plt.scatter(x, y, s=20)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241, in scatter
ax = gca()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
You can solve it by adding these two lines in the VERY beginning of your .py script.
import matplotlib
matplotlib.use('Agg')
PS: The error will still exists if these two lines are not added in the very beginning of the source code.
回答 2
为了加总答案,我在所需脚本的开头使用了它。因此它可以在不同的环境下平稳运行。
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','')=='':print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')import matplotlib.pyplot as plt
To add up on the answer, I used this at the beginning of the needed script. So it runs smoothly on different environments.
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')
import matplotlib.pyplot as plt
Because I didn’t want it to be alsways using the 'Agg' backend, only when it would go through Travis CI for example.
I had this same issue trying to run a simple tkinter app remotely on a Raspberry Pi. In my case I did want to display the tkinter GUI on the pi display, but I want to be able to execute it over SSH from my host machine. I was also not using matplotlib, so that wasn’t the cause of my issue. I was able to resolve the issue by setting the DISPLAY environment variable as the error suggests with the command:
I also met this problem while using Xshell to connect Linux server.
After seaching for methods, I find Xming + Xshell to solve image imshow problem with matplotlib.
If solutions aboved can’t solve your problem, just try to download Xming under the condition you’re using Xshell. Then set the attribute in Xshell, SSH->tunnel->X11transfer->choose X DISPLAY localhost:0.0
In order to see images, plots and anything displayed on windows on your remote machine you need to connect to it like this:
ssh -X user@hostname
That way you enable the access to the X server. The X server is a program in the X Window System that runs on local machines (i.e., the computers used directly by users) and handles all access to the graphics cards, display screens and input devices (typically a keyboard and mouse) on those computers.
importPySimpleGUIassg# Part 1 - The import# Define the window's contentslayout= [ [sg.Text("What's your name?")], # Part 2 - The Layout
[sg.Input()],
[sg.Button('Ok')] ]
# Create the windowwindow=sg.Window('Window Title', layout) # Part 3 - Window Defintion# Display and interact with the Windowevent, values=window.read() # Part 4 - Event loop or Window.read call# Do something with the information gatheredprint('Hello', values[0], "! Thanks for trying PySimpleGUI")
# Finish up by removing from the screenwindow.close() # Part 5 - Close the Window
importPySimpleGUIassg# Define the window's contentslayout= [[sg.Text("What's your name?")],
[sg.Input(key='-INPUT-')],
[sg.Text(size=(40,1), key='-OUTPUT-')],
[sg.Button('Ok'), sg.Button('Quit')]]
# Create the windowwindow=sg.Window('Window Title', layout)
# Display and interact with the Window using an Event LoopwhileTrue:
event, values=window.read()
# See if user wants to quit or window was closedifevent==sg.WINDOW_CLOSEDorevent=='Quit':
break# Output a message to the windowwindow['-OUTPUT-'].update('Hello '+values['-INPUT-'] +"! Thanks for trying PySimpleGUI")
# Finish up by removing from the screenwindow.close()
Python works on multiple platforms and can be used for desktop and web applications, thus I conclude that there is some way to compile it into an executable for Mac, Windows and Linux.
The problem being I have no idea where to start or how to write a GUI with it, can anybody shed some light on this and point me in the right direction please?
First you will need some GUI library with Python bindings and then (if you want) some program that will convert your python scripts into standalone executables.
Cross-platform GUI libraries with Python bindings (Windows, Linux, Mac)
Of course, there are many, but the most popular that I’ve seen in wild are:
Tkinter – based on Tk GUI toolkit (de-facto standard GUI library for python, free for commercial projects)
WxPython – based on WxWidgets (popular, free for commercial projects)
Qt using the PyQt bindings or Qt for Python. The former is not free for commercial projects. The latter is less mature, but can be used for free.
Another system (not mentioned in the accepted answer yet) is PyInstaller, which worked for a PyQt project of mine when py2exe would not. I found it easier to use.
An alternative tool to py2exe is bbfreeze which generates executables for windows and linux. It’s newer than py2exe and handles eggs quite well. I’ve found it magically works better without configuration for a wide variety of applications.
There’s also PyGTK, which is basically a Python wrapper for the Gnome Toolkit. I’ve found it easier to wrap my mind around than Tkinter, coming from pretty much no knowledge of GUI programming previously. It works pretty well and has some good tutorials. Unfortunately there isn’t an installer for Python 2.6 for Windows yet, and may not be for a while.
Since python is installed on nearly every non-Windows OS by default now, the only thing you really need to make sure of is that all of the non-standard libraries you use are installed.
Having said that, it is possible to build executables that include the python interpreter, and any libraries you use. This is likely to create a large executable, however.
MacOS X even includes support in the Xcode IDE for creating full standalone GUI apps. These can be run by any user running OS X.
Another way to develop a rapid user interface is to write a web app,
have it run locally and display the app in the browser.
Plus, if you go for the Tkinter option suggested by lubos hasko
you may want to try portablepy to have your app run on Windows environment
without Python.
I’m not sure that this is the best way to do it, but when I’m deploying Ruby GUI apps (not Python, but has the same “problem” as far as .exe’s are concerned) on Windows, I just write a short launcher in C# that calls on my main script. It compiles to an executable, and I then have an application executable.
回答 7
# I'd use tkinter for python 3import tkinter
tk = tkinter.Tk()
tk.geometry("400x300+500+300")
l =Label(tk,text="")
l.pack()
e =Entry(tk)
e.pack()def click():
e['text']='You clicked the button'
b =Button(tk,text="Click me",command=click)
b.pack()
tk.mainloop()# After this I would you py2exe# search for the use of this module on stakoverflow# otherwise I could edit this to let you know how to do it
# I'd use tkinter for python 3
import tkinter
tk = tkinter.Tk()
tk.geometry("400x300+500+300")
l = Label(tk,text="")
l.pack()
e = Entry(tk)
e.pack()
def click():
e['text'] = 'You clicked the button'
b = Button(tk,text="Click me",command=click)
b.pack()
tk.mainloop()
# After this I would you py2exe
# search for the use of this module on stakoverflow
# otherwise I could edit this to let you know how to do it
py2exe
Then you should use py2exe, for example, to bring in one folder all the files needed to run the app, even if the user has not python on his pc (I am talking of windows… for the apple os there is no need of an executable file, I think, as it come with python in it without any need of installing it.
Create this file
1) Create a setup.py
with this code:
from distutils.core import setup
import py2exe
setup(console=['l4h.py'])
save it in a folder
2) Put your program in the same folder of setup.py
put in this folder the program you want to make it distribuitable:
es: l4h.py
ps: change the name of the file (from l4h to anything you want, that is an example)
3) Run cmd from that folder (on the folder, right click + shift and choose start cmd here)
4) write in cmd:>python setup.py py2exe
5) in the dist folder there are all the files you need
6) you can zip it and distribute it
Pyinstaller
Install it from cmd
**
pip install pyinstaller
**
Run it from the cmd from the folder where the file is
PySimpleGUI wraps tkinter and works on Python 3 and 2.7. It also runs on Qt, WxPython and in a web browser, using the same source code for all platforms.
You can make custom GUIs that utilize all of the same widgets that you find in tkinter (sliders, checkboxes, radio buttons, …). The code tends to be very compact and readable.
#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
layout = [[ sg.Text('My Window') ],
[ sg.Button('OK')]]
window = sg.Window('My window').Layout(layout)
button, value = window.Read()
You don’t need to compile python for Mac/Windows/Linux. It is an interpreted language, so you simply need to have the Python interpreter installed on the system of your choice (it is available for all three platforms).
As for a GUI library that works cross platform, Python’s Tk/Tcl widget library works very well, and I believe is sufficiently cross platform.
Tkinter is not the only GuiProgramming
toolkit for Python. It is however the
most commonly used one, and almost the
only one that is portable between
Unix, Mac and Windows
回答 10
您可以appJar用于基本的GUI开发。
from appJar import gui
num=1def myfcn(btnName):global num
num +=1
win.setLabel("mylabel", num)
win = gui('Test')
win.addButtons(["Set"],[myfcn])
win.addLabel("mylabel","Press the Button")
win.go()
from appJar import gui
num=1
def myfcn(btnName):
global num
num +=1
win.setLabel("mylabel", num)
win = gui('Test')
win.addButtons(["Set"], [myfcn])
win.addLabel("mylabel", "Press the Button")
win.go()
I have once done a project using Tkinter, although they do advocate that it has improved a lot, it still gives me a feel of windows 98, so I switched to Kivy.
classtqdm():
""" Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested. """def__init__(self, iterable=None, desc=None, total=None, leave=True,
file=None, ncols=None, mininterval=0.1,
maxinterval=10.0, miniters=None, ascii=None, disable=False,
unit='it', unit_scale=False, dynamic_ncols=False,
smoothing=0.3, bar_format=None, initial=0, position=None,
postfix=None, unit_divisor=1000):
classtqdm():
defupdate(self, n=1):
""" Manually update the progress bar, useful for streams such as reading files. E.g.: >>> t = tqdm(total=filesize) # Initialise >>> for current_buffer in stream: ... ... ... t.update(len(current_buffer)) >>> t.close() The last line is highly recommended, but possibly not necessary if ``t.update()`` will be called in such a way that ``filesize`` will be exactly reached and printed. Parameters ---------- n : int or float, optional Increment to add to the internal counter of iterations [default: 1]. If using float, consider specifying ``{n:.3f}`` or similar in ``bar_format``, or specifying ``unit_scale``. Returns ------- out : bool or None True if a ``display()`` was triggered. """defclose(self):
"""Cleanup and (if leave=False) close the progressbar."""defclear(self, nomove=False):
"""Clear current bar display."""defrefresh(self):
""" Force refresh the display of this bar. Parameters ---------- nolock : bool, optional If ``True``, does not lock. If [default: ``False``]: calls ``acquire()`` on internal lock. lock_args : tuple, optional Passed to internal lock's ``acquire()``. If specified, will only ``display()`` if ``acquire()`` returns ``True``. """defunpause(self):
"""Restart tqdm timer from last print time."""defreset(self, total=None):
""" Resets to 0 iterations for repeated use. Consider combining with ``leave=True``. Parameters ---------- total : int or float, optional. Total to use for the new bar. """defset_description(self, desc=None, refresh=True):
""" Set/modify description of the progress bar. Parameters ---------- desc : str, optional refresh : bool, optional Forces refresh [default: True]. """defset_postfix(self, ordered_dict=None, refresh=True, **tqdm_kwargs):
""" Set/modify postfix (additional stats) with automatic formatting based on datatype. Parameters ---------- ordered_dict : dict or OrderedDict, optional refresh : bool, optional Forces refresh [default: True]. kwargs : dict, optional """@classmethoddefwrite(cls, s, file=sys.stdout, end="\n"):
"""Print a message via tqdm (without overlap with bars)."""@propertydefformat_dict(self):
"""Public API for read-only member access."""defdisplay(self, msg=None, pos=None):
""" Use ``self.sp`` to display ``msg`` in the specified ``pos``. Consider overloading this function when inheriting to use e.g.: ``self.some_frontend(**self.format_dict)`` instead of ``self.sp``. Parameters ---------- msg : str, optional. What to display (default: ``repr(self)``). pos : int, optional. Position to ``moveto`` (default: ``abs(self.pos)``). """@classmethod@contextmanagerdefwrapattr(cls, stream, method, total=None, bytes=True, **tqdm_kwargs):
""" stream : file-like object. method : str, "read" or "write". The result of ``read()`` and the first argument of ``write()`` should have a ``len()``. >>> with tqdm.wrapattr(file_obj, "read", total=file_obj.size) as fobj: ... while True: ... chunk = fobj.read(chunk_size) ... if not chunk: ... break """@classmethoddefpandas(cls, *targs, **tqdm_kwargs):
"""Registers the current `tqdm` class with `pandas`."""deftrange(*args, **tqdm_kwargs):
""" A shortcut for `tqdm(xrange(*args), **tqdm_kwargs)`. On Python3+, `range` is used instead of `xrange`. """
fromtqdmimporttqdm, trangefromrandomimportrandom, randintfromtimeimportsleepwithtrange(10) ast:
foriint:
# Description will be displayed on the leftt.set_description('GEN %i'%i)
# Postfix will be displayed on the right,# formatted automatically based on argument's datatypet.set_postfix(loss=random(), gen=randint(1,999), str='h',
lst=[1, 2])
sleep(0.1)
withtqdm(total=10, bar_format="{postfix[0]} {postfix[1][value]:>8.2g}",
postfix=["Batch", dict(value=0)]) ast:
foriinrange(10):
sleep(0.1)
t.postfix[1]["value"] =i/2t.update()
importpandasaspdimportnumpyasnpfromtqdmimporttqdmdf=pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
# Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`# (can use `tqdm.gui.tqdm`, `tqdm.notebook.tqdm`, optional kwargs, etc.)tqdm.pandas(desc="my bar!")
# Now you can use `progress_apply` instead of `apply`# and `progress_map` instead of `map`df.progress_apply(lambdax: x**2)
# can also groupby:# df.groupby(0).progress_apply(lambda x: x**2)
fromtqdm.daskimportTqdmCallbackwithTqdmCallback(desc="compute"):
...
arr.compute()
# or use callback globallycb=TqdmCallback(desc="global")
cb.register()
arr.compute()
# different celliterable=range(100)
pbar.reset(total=len(iterable)) # initialise with new `total`foriiniterable:
pbar.update()
pbar.refresh() # force print final status but don't `close()`
fromtqdmimporttqdmimportos.pathdeffind_files_recursively(path, show_progress=True):
files= []
# total=1 assumes `path` is a filet=tqdm(total=1, unit="file", disable=notshow_progress)
ifnotos.path.exists(path):
raiseIOError("Cannot find:"+path)
defappend_found_file(f):
files.append(f)
t.update()
deflist_found_dir(path):
"""returns os.listdir(path) assuming os.path.isdir(path)"""listing=os.listdir(path)
# subtract 1 since a "file" we found was actually this directoryt.total+=len(listing) -1# fancy way to give info without forcing a refresht.set_postfix(dir=path[-10:], refresh=False)
t.update(0) # may trigger a refreshreturnlistingdefrecursively_search(path):
ifos.path.isdir(path):
forfinlist_found_dir(path):
recursively_search(os.path.join(path, f))
else:
append_found_file(path)
recursively_search(path)
t.set_postfix(dir=path)
t.close()
returnfiles
fromtqdm.autoimporttqdm, trangefromtimeimportsleepbar=trange(10)
foriinbar:
# Print using tqdm class method .write()sleep(0.1)
ifnot (i%3):
tqdm.write("Done task %i"%i)
# Can also use bar.write()