Programmatically choose correct backend for Matplotlib on Mac OS X - macos

I have a program which integrates a tkinter GUI as well as a matplotlib plot (using pyplot). I'm running into endless troubles having this program work correctly across a variety of Mac platforms. The main problem seems to be the appropriate selection of the backend.
In some cases, the program runs fine no problem. In other cases, I run into a similar issue documented in this question. Implementing the solution outlined there solves that problem, but then other errors pop up for other systems. The solution to these other errors appears to be to use the Qt4Agg backend.
There has to be some standard way of getting a program using tkinter and matplotlib at the same time to play nice with Macs. How can I programmatically make sure the correct backend is being used such that the program won't crash for a Mac user?
Sorry if this is vague but it is a very broad problem.

See this answer: How to switch backends in matplotlib / Python
In essence, if you do not know which backend is available, the following code should load up the first backend that is available on the current machine. (I have only included 4 backends, there are quite a few others).
import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
try:
print("testing", gui)
matplotlib.use(gui,warn=False, force=True)
from matplotlib import pyplot as plt
break
except:
continue
print("Using:",matplotlib.get_backend())
Using: GTKAgg
Update: I am lead to believe that there is a backend for OSX called MacOSX which could be added to that list, although I have no way of testing it myself.

Related

How to fix blurry text in WxPython controls on Windows?

We have a Python-based application written with WxPython 4 that we build for multiple operating systems, including Windows. In Windows 10, at least in my environment (inside a VM on a high resolution monitor), the text elements in the interface are blurry:
I can improve the situation somewhat by setting the Windows DPI-scaling override on the application. This is indirect and suboptimal, and it seems like there should be a way to do it from WxPython more directly. However, I have not found a way to do it.
Is there any way to set DPI scaling from WxPython, or otherwise fix blurry text on Windows in WxPython 4 directly from within the application?
According to a message from Python Issue Tracker and what I tried in the wxPython official Hello World, Part 2 example (helloworld2.py), adding following code to your program should work:
import ctypes
try:
ctypes.windll.shcore.SetProcessDpiAwareness(True)
except:
pass

Python - pythondiologue vs. tkinker vs. wxPython vs. pyQt

I made a really simple, pseudo GUI using pythondialogue (its a wrapper for whiptail for bash), I need it to be cross platform between linux and Mac OSX. The main issue is its really hard to find information on pythondialogue, the only documentation seems to be on their own site. I would just use whiptail, but I'm learning python so using this to hone my python skills.
What I like about pythondialogue (and whiptail) is that its not really a GUI, just a dialogue inside the CLI, so it can used purely through the command line such as if you SSH to the computer you want to run it on. Can tkinter do this too?
Either way, a big thing I'm wondering is what benefits tkinter would provide over regular pythondialogue. Obviously the difference is it lets you create proper GUI applications, but would it be wisest to only create a GUI application in cases where its absolutely necessary?
tkinter sounds like the easiest way to code GUIs in python. What disadvantages does it have to PyQt or wxPython. I wan't to start developing mobile apps as soon as possible and I see iOS and android apps can be written using python and Qt can be used to write both Android and iOS apps. So with this in mind, would learning PyQt mean I would also be developing the skills I'll need to create iOS and android apps? If so, this is most definitely what I'm going to do.
Learning PyQt will definitely help you learn to create IOS and Android apps. Even more, PyQt comes with QtDesinger which is a visual editor for creating apps with minimal coding. Tkinter in my perspective is for very light GUI programming. If you feel like you want to make money of these apps, I would highly advise you to check out Kivy.

LLDB: is it possible to display graphics from lldb session?

I would like to display a plot from LLDB session, is that possible?
plt.figure()
plt.title('Test')
plt.imshow(array, cmap='gray')
plt.show()
Right now, when i do that through the "command script import ~/script.py"; the session is stuck!
This works correctly in command-line lldb (or at least it does for me...)
That it doesn't work when trying to share the connection to the Window Server with Xcode (since lldb is running in the Xcode app process) is not entirely surprising. Doing plt.figure() seems to stall, though it wasn't immediately clear to me what Python thought it was doing when you called this method. It was not stalled somewhere obvious.
I don't think lldb has anything to do with this one way or the other (especially since command-line lldb works.) You're more likely to figure out how to get this working by asking the MatPlotLib folks if they have any experience sharing GUI's when the python is an embedded interpreter, especially in something complex like Xcode.
You might also see if they have any way to call out to an out-of-process renderer. That might get around the complexities of living inside Xcode.
I had a similar issue while plotting custom objects in Xcode. The lldb session crashed when calling plt.plot().
I used matplotlib with the Agg backend and was able to plot and save the generated plots at a convenient location. However you won't be able to show them directly from lldb in Xcode with the Agg backend.
Here is how my code looked like:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
plt.plot([1,2,3,4,5])
plt.title("Title")
plt.savefig("your/path")
plt.close()
Hope this helps.

include pygame in your program

I'm about to finish a program which uses Pygame, which means you would need to install Python and Pygame in order to run the game. How can I include Python and Pygame in the program itself, or is there an easy way to make an installer for the game?
py2exe seems to be a popular answer, and I would suggest looking into it as well. I would also suggest trying pyinstaller. The setup is a bit more involved, but the tutorials are great.
Like PygameNerd said, I would suggest looking into py2exe. I wrote a game for a game jam and wanted to distribute it to people in my dorm. I used py2exe to load it up and distribute it pretty effortlessly (especially using Pygame).
Check out the py2exe website as well as this link on working with various modules in your package. I had dependencies with wxPython and used the latter guide to much success.

Are there any benefits to using Python for GUI development?

I was looking into Tkinter, but I was wondering if there are any other packages that help with GUI. Even though Java is more robust than python, python is faster to write and would be very helpful if I am just trying to create a quick application. I usually use .Swing and .awt in java to make all of my applications.
Also, Is there something similar to JWS for python? I am looking to add a link on a website that will fire up a python coded application
Thanks
(P.S. I'm using Python 3.2.2)
I love using tkinter with python. It is pretty easy to do, there are lots of examples, and numerous questions with answers on here too! Also, 90% of people with python have tkinter already on their system, making distribution pretty straight forward. I also have had success using py2exe to create windows executables.
Here is a gui that I wrote in all of ~10 minutes. Once you understand the syntax it is very easy.
And another more elaborate one that I wrote with vtk and matplotlib embedded into the gui for 3D and 2D graphics! The question I asked with this.
Yes, python and tkinter are excellent for developing GUIs. While this is true of most scripting languages, I think these two make a particularly good combination.
For most GUIs, functionality is more important than form (ie: eye candy is not top priority). Even then, it's possible to make very nice looking GUIs with Tkinter. Tk has been my go-to toolkit for close to twenty years. It is most easily used with Tcl since that is its native language, but there's no denying the advantage python has over Tcl as far as built-in functionality goes.
That being said, all of the toolkits available to a python programmer are good. Tkinter's main advantages are that it is extremely easy to use and you probably already have it installed, so the barrier for entry is very low.

Resources