Fonts management on Linux for Python and Tkinter - python

I have written an application in Python2.7/Tkinter.
I have 2 Linux machines:
1 Xubuntu with python 2.7.4
1 CentOS (5.2) with python 2.7.1 (Unfortunately, and before someone ask, I can't upgrade this machine)
I connect on both machine from my WindowsXP laptop through SSH and I export the display. Xming is installed on the windows machine as X server.
The script is exactly the same on both machine (shared drive mounted on both machine).
In the script I have :
# show which fonts the system know
print tkFont.families()
# configure the default font
default_font = tkFont.nametofont("TkDefaultFont")
default_font.configure(family="Liberation Sans", size="10")
master.option_add("*Font", default_font)
The fonts "Liberation" are installed on both Linux machine and in Xming (and configured).
In the application I used the grid manager, so the font have a big impact on the resulting interface.
When I launch the application from Xubuntu:
Application interface look very nice. tkFont.families show me some
fonts which aren't installed in Xming (but on Xubuntu)
When I launch the application from CentOs:
Application interface is ugly almost unreadable and completely
deformed. tkFont.families display the "liberation" font in the list
ONLY when installed and configured in Xming.
I scratch my head since this morning on this, I even copied the liberation*.ttf files from Xubuntu to all other machines, without success
How can I suppress this difference between the 2 machines, that my interface look the same on both ?
Any help will be greatly appreciated

At a guess, your tk installation server-side relies on the X Core fonts subsystem to render fonts (this is why it shows xming fonts).
This subsystem has been replaced by fontconfig a long time ago (but tk was very late in switching). Centos 5.2 is probably old enough its tk version still relies on the X Core fonts subsystem. However, Fedora and Red Hat stopped exposing system truetype fonts as X Core fonts quite a long time ago (it was breaking legacy applications).
So your solution is :
– either upgrade to a tk that can use fontconfig (or reconfigure it to use fontconfig) : this way it will see the modern fonts installed in Centos, including Liberation
http://wiki.tcl.tk/9015
– or, do all the legacy magic to expose liberation font in xfs (mkfontdir, and friends, as documented in all pre-2000 Linux font howtos). ANd pray that does not break something else, the X core font system is not particularly robust.
https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s1-x-fonts.html
Mid term you'll had to do it the fontconfig way anyway since x core fonts are on the way out (it was already the case when RHEL 5 was released circa 2007).

Related

Python and Active Directory (modules pyad, flask_ldap, active_directory, python_ldap, etc.)

Trying to investigate using Python instead of a compiled language to support users with "web applications", etc. So at this point, I'm getting the various aspects of "what we do" and trying basic things out to find out if they work.
I'm sure this will probably result in a "not specific enough", or something similar, BUT there are so many problems/issues with all the packages I've tried to install for Python and Active Directory, it makes me wonder if these are being heavily used, and if not - what should be?
We operate in an mixed Windows/Linux environment, Windows servers are 2012/2012R2/2016, Linux boxes are generally RHEL 7 level (couple of older RHEL5 boxes that will be updated). PCs are all 64bit, Windows 10
Need the following capabilities
Host the flask content on either Windows or Linux server
Get the current AD account login from the client (are they logged into AD, and what account?)
From that current account - get the groups that the account is a member of
something still supported, and not "umpteen" years old...
I've "looked" into using/installing (on Windows ONLY at this time...):
active_directory (fails because of print syntax errors in the installation of pypiwin32 apparently)
pyad (fails due to above issue in pypiwin32)
python_ldap (fails because VStudio 14.0 is required?! I don't know of a VS version 14 - and this is "Windows unique")
flask_ldap (requires python_ldap which fails)
couple of others with same problems, etc.
Why do several of these REQUIRE a Win32 component (pypiwin32), especially if you operate in a "64bit only" environment? I'm also assuming the pypiwin32 "print" issue is a change between python 2 and 3 (i.e. "print 'something'" vs. "print('something')"???
Being as nothing I can find for python and active directory will install, I'm not sure where to turn. As I said, I'm investigating at this point.
win32 appears to be the only officially supported version, but there are alternative 64bit builds available.
e.g. https://www.lfd.uci.edu/~gohlke/pythonlibs/

Using PyParallel in Windows XP

I have successfully implemented the PyParallel module in both Linux and Mac OSX as part of a large application to interface with a sensor I am developing.... I am now attempting to use this application on an instance of Windows XP. I have found several references (including right from the PySerial/PyParallel group) that:
The windows version needs a compiled extension and the giveio.sys driver for Windows NT/2k/XP. It uses ctypes to access functions in a prebuilt DLL.
However, I don't know what "a compiled extension" requirement is. And, I can't seem to get givio.sys to work. I obtained giveio.sys here and followed the recommendations, but LoadDrv.exe fails to "start" the service (it does "install", however).
I cannot find specific examples online of getting PyParallel working on Windows XP. Since PyParallel is "thoroughly" integrated into the application and is working on both Linux and Mac OSX, I'd prefer not to use a different module -- especially since PyParallel is great to not require root/administrator privileges to utilize.
I was having trouble with giveio.sys and LoadDrv.exe as well.
There is a handy installer that does it all automatically:
http://sourceforge.net/projects/pyserial/files/pyparallel/giveio/

Deploying Python Qt application cross-platform: Win, OSX, Linux

We are designing open source UI controller application for Plone CMS. The application itself would be just simple start/stop dialog window for Plone daemon process, mainly aimed for Windows users.
However we are hoping to use the same app for OSX and Linux.
We can create the code for Python Qt application, but we feel unsure about the deployment process. In the optimal case we'd hope to create static builds (.exe for Windows, .dmg for OSX) all megabyte so Qt included for our tiny Python script, just to maximize the ease for the end user.
Are there any examples, scripts, etc. to build Qt friendly installers cross-platform? What other best practices we should consider?
PyInstaller is a nice one to create executables and it comes with full PyQt support. It says it can create executables for Windows, OsX and Linux but I've never used it for anything other than Windows. For Windows, it was quite painless and easy.
This doesn't address the packaging/deployment component of your question, but might be a useful side note.... If you haven't already, you may want to consider PySide for your Python bindings, instead of PyQt. The licensing terms are more permissive (LGPL) than PyQt, and may be more appropriate unless you're commercially licensed.
FWIW, I have successfully used py2app with PySide/Qt4 on the Mac, but don't know if it goes cross platform.

wx.TaskBarIcon on Ubuntu 11.04

There is no tray in Unity under Ubuntu 11.04.
How can I make icon appear somewhere in Unity? wx.TaskBarIcon is not appear anywhere.
Thanks
With Ubuntu Unity desktop environment (i.e. Ubuntu 11.04 or 11.10), you need to "whitelist" your application.
There are different ways to do this using the 'gsettings' application from the command line.
I had to do this to get programs like Skype showing in the system tray again, not just for my own custom apps that use wxPython.
Best answer to give is from the official Documentation:
Under X Window System, the window manager must support either the
System Tray Protocol by freedesktop.org (WMs used by modern desktop
environments such as GNOME >= 2, KDE >= 3 and XFCE >= 4 all do) or the
older methods used in GNOME 1.2 and KDE 1 and 2. If it doesn't, the
icon will appear as a toplevel window on user's desktop.
Because not all window managers have system tray, there's no guarantee
that wxTaskBarIcon will work correctly under X Window System and so
the applications should use it only as an optional component of their
user interface. The user should be required to explicitly enable the
taskbar icon on Unix, it shouldn't be on by default.
http://docs.wxwidgets.org/stable/wx_wxtaskbaricon.html
So this is related to your windowmanager, not programming wxWidgets. wxTaskbarIcon is only reliable under wxMSW, dunno about mac.
Best is to avoid it if you want real XPlatform-Applications.
If you're making a new application, consider using the newer libappindicator library instead – it’s Ubuntu's successor for the systray which should be compatible with GNOME 2.x at least. (I've no idea if either GNOME 3 or KDE support displaying libappindicator notifications anywhere.)

Create a directly-executable cross-platform GUI app using Python

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.
Complete list is at http://wiki.python.org/moin/GuiProgramming
Single executable (all platforms)
PyInstaller - the most active(Could also be used with PyQt)
fbs - if you chose Qt above
Single executable (Windows)
py2exe - used to be the most popular
Single executable (Linux)
Freeze - works the same way like py2exe but targets Linux platform
Single executable (Mac)
py2app - again, works like py2exe but targets Mac OS
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.
http://www.pyinstaller.org/
Pyinstaller is based on Gordon McMillan's Python Installer. Which is no longer available.
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.
For the GUI itself:
PyQT is pretty much the reference.
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.
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()
As explained in the PySimpleGUI Documentation, to build the .EXE file you run:
pyinstaller -wF MyGUIProgram.py
!!! KIVY !!!
I was amazed seeing that no one mentioned Kivy!!!
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.
I have been following a tutorial series if it helps...
Just to give an idea of how kivy looks, see this (The project I am working on):
And I have been working on it for barely a week now !
The benefits for Kivy you ask? Check this
The reason why I chose this is, its look and that it can be used in mobile as well.
# 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
Create a setup.py
with this code:
from distutils.core import setup
import py2exe
setup(console=['l4h.py'])
save it in a folder
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)
Run cmd from that folder (on the folder, right click + shift and choose start cmd here)
write in cmd:>python setup.py py2exe
in the dist folder there are all the files you need
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
**
pyinstaller file.py
**
Update
Read this post to make an exe on windows with pyinstaller the proper way and with one file and images in it https://pythonprogramming.altervista.org/auto-py-to-exe-only-one-file-with-images-for-our-python-apps/
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 the python interface to Tk/Tcl
From the python project webpage:
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
You can use appJar for basic GUI development.
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()
See documentation at appJar site.
Installation is made with pip install appjar from command line.
There's three things you could do:
The first thing is to find a GUI Designer that can launch its code as standalone applications like .exe files. I use a version of MatDeck (for people using GUI Designers I recommend MD Python Designer) as I believe(I use another version so I'm not too sure.) it allows me to convert the code to a standalone applications and by having it as such, there is no need to install the software on every PC that's going to run the program.
The second option is partially bypassing the problem, launch the GUI as a web page. This would give you the most compatibility as most if not all OS can utilize it. Once again, you would need a GUI Designer that can convert its components into a web compatible format, I've done it once and I used the same version of MatDeck(Visionary Deck), I would not recommend MD Python Designer this time as I don't know if it can turn its GUIs into websites using web assembly whereas Visionary Deck I've tried and tested. As with all things there are most likely other software this is just one I use frequently because I work a lot with Mathematics and Physics.
The third option is also kind of bypassing the problem but do it in Tkinter and just ensure you have a Python IDE or just plain old Python and run the code, this will launch the GUI. This is a good solution and maybe the simplest but I wouldn't class it as the shortest or the best. If you only plan to switch between a few operating systems and computers this will probably be your best bet.

Categories