How to use PyQt method QWindow.fromWinId(id) on linux? - python

I'm trying to embed a foreign executable inside of a Python GUI (PyQt) just like this post describes: Run a foreign exe inside a Python GUI (PyQt)
but the answer requires the win32gui module, which doesn't seem to be available on linux systems. The PyQt method (http://doc.qt.io/qt-5/qwindow.html):
--> QWindow.fromWinId(id)
does what I need, but I'm not sure how to get the handle id (hwnd) of the window of my executable (for the record, I'm trying to embed saods9 within my GUI) on linux (Ubuntu). Everything I'm finding looks to be geared toward a Windows operating system, not Linux. So I'm thinking that this Qt method can't be used on a Linux operating system?
That would be my question: How do you get the handle id of a native window within Linux to be used in the Qt method QWindow.fromWinId(id)?

Related

How does Tkinter work on Microsoft Windows?

I was reading the up on Tkinter for certain operations that the windows manager handles and I got to wondering how exactly does Tkinter communicate with the OS's window manager? Per the docs it uses Xlib to draw graphics, but how does that work on Microsoft Windows? Isn't Xlib a Unix-like grahpics library, i.e. X11? How exactly does Tkinter communicate with Windows graphics? Does the Windows version of Python have access to the WinAPI and doesnt use Xlib?
Tk (the library upon which tkinter is base) uses the windows API on windows, and cocoa libraries on OSX. Those two platforms don't use xlib, though it is possible to compile X11-based versions on those platforms if you wish.
A lot of information about the windows port of tcl/tk is available on the Tcl wiki: https://wiki.tcl-lang.org/page/Microsoft+Windows+and+Tcl

Displaying a pdf document within a PySide python application

I have a python application using PySide, Qt and python. In my application I would like to open and display a pdf document, but have the viewer within the application and not using the default viewer on the operating system.
I have not as of yet been able to find a widget which is capable of doing this or some example code. I have never used these tools before and this is my first Qt & PySide application so I am sure there is a widget which is capable of this but that I am just not aware of it.
I found this post and when I take the sample code I am unable to get any pdf to show and cannot replicate the behaviour of the poster, but I am using python 2.7.3, PySide 1.1.1 so perhaps this is why. The error that I get when trying to run the sample code in the other post is:
QWebInspector: QSettings couldn't read configuration setting [resourceTrackingEnabled].
and the pdfs I would like to render are saved locally, in the same directory as the the python script and not accessible via the web.
Could somebody point me in the direction of some sample code or a Qt widget which I would be able to use to render a pdf?
As an aside, I am not sure that it matters but the pdf will be generated using LaTeX.
Edit --More information--
The program has to run on both Windows and Mac OSX environments but it is just for a personal project which wont be distributed. Using PyQt is not an option as it is not compatible with the python editor that I use and PySide is the requirement. I have read the following link but from what I understood under the Rendering section it said that the 3rd party libraries available worked with C, C++ and Qt not python or PySide but maybe I have misunderstood this?

console application gui for python

We have console application option in C#, but how do I can make a console GUI in python?
I should note I want to compile and use my program in windows.
Also I want to select Items in Python GUI by Enter button.
If you're using Windows you will ultimately need to integrate with the win32 console API to create a console GUI (aka. a Text UI or TUI). Your options are basically:
Write your TUI using curses or the packages that sit on top of it, like urwid or npyscreen. To do this you either need to install cygwin or PDcurses to make it work.
Write to the win32 API directly using something like pywin32.
Use a package that simplifies the win32 API like effbot's console package.
I found all the above unsatisfactory as I didn't want all the hassle of installing 3rd party binaries for Windows and wanted an API that I could use anywhere (after a simple pip install), so I wrote a cross-platform package (asciimatics) to fix it. This package takes all the hassle away and provides a clean, documented API with a set of widgets for TUI applications.
For example the following is a screenshot of the contacts list sample code referenced in the previous docs link, showing some of the basic text and button widgets.
Take a look at Python's curses library:
http://docs.python.org/howto/curses.html
http://www.dev-explorer.com/articles/python-with-curses
http://docs.python.org/library/curses.html
If you'd like a Python shell, then you might want to look at something like PyFlakes / PyShell or similar.

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.

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