pyqt how to make application truly active - python

Under OS X, using the zetcode example for menubar, if one runs this from the command line:
$ python menubar.py
the application starts, but at the bottom of the window stack. This can be fixed by adding
self.raise_()
to the code.
However, the application is still not immediately active. The terminal application menubar still shows, not the pyqt menubar.
The only way to get the pyqt menubar to show is to switch away from the pyqt application or terminal application and back again.
Is this expected behavior? Is there anyway to fix this so the pyqt application immediately becomes active, i.e. the pyqt menubar immediately shows upon execution.
Versions: OS X 10.9, Qt 4.8.6, PyQt 4.11.3, SIP 4.16.5, python 2.7.8

The identical problem affects some apps bundled by PyInstaller. The following blog post discusses the fix, however the fix is in terms of the PyInstaller boot loader, which would not be directly relevant to your code, but it might be informative.
http://dvitonis.net/blog/2015/01/07/menu-bar-not-visible-when-building-pyqt-app-bundle-pyinstaller-mac-osx-mavericks-yosemite/
The problem relates to use of TransformProcessType() which is discussed in this linked article:
http://www.sheepsystems.com/developers_blog/TPT-show-menu.html
I do not know what Qt method would relate to the Mac OS TransformProcessType() but you might experiment with calling QWidget.setWindowState() in your mainwindow's __init__().

I was experiencing this behavior, but only when I launch with a --nosplash option to skip the splash screen in my application.
That gave me a hint, and I developed this workaround using a 'dummy' splash screen:
widget = QtGui.QMainWindow() # or whatever your main window class is
dummy = QtGui.QSplashScreen()
dummy.show()
dummy.finish(widget)
widget.show()
widget.raise_()

Related

How to embed gnome-terminal in tkinter?

I am making a simple Python program which I want to run gnome terminal into it.
I've done some research and I have found this answer, but I want to embed gnome terminal not xterm. The linked question's answer uses xterm -into but gnome-terminal does not seem to have a comparable option.
gnome-terminal is a GTK app, Tkinter creates TCL app: both libraries will interact with the underlying window manager in very different ways and likely with some conflict (in handling/consuming input events, for example).
Since the embed option does not exist, to start with, I´d say this is not feasible but for setting a raw framebuffer inside the tkinter app, and somehow routing the gnome-terminal to use an emulated/contained window manager in that framebuffer - that would be at least an order of magnitude more complex than any answer you might be looking for.
Instead, if you want to give the user of your app a raw shell in a terminal app, and can't live with simply popping it up in another window, is to re-do your application using GTK instead of tkinter, and check in the gnome-terminal development docs/source code if the main terminal widget can be used stand-alone in another app.

Win10 starting Python PyQt5 application using shortcut

Simply stated I have a simple python application which generates random passwords. This application was originally written using Tkinter and currently works. I am trying to improve the GUI interface by employing PyQt5. My efforts, so far have resulted in an application that runs from within my IDE (Spyder) and can also be run by invoking python from the commandline with the fullpath of the python script.
It should be noted this works for the Tkinter as well as the PyQt implementation.
My next step was to define a shortcut on the desktop to execute this script and have a window appear allowing creation of a password. The shortcut for the Tkinter script performs as expected and results in a window appearing. The script for the PyQt5 based script does not work. The only differences between the scripts are the target files being invoked by the script. Also both script files are in the same directory. This is a side by side image of the shortcut properties.
Here is a very simplistic example of the Puqt5 code. This code exhibits the same characteristics as the original in that it runs in the IDE as well as directly from Python in the CMD window but will not execute from a shortcut icon.
"""
Created on Wed Sep 9 10:37:46 2020
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
def main():
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Has anyone else had this type of problem or can anyone suggest an approach for determining what is the cause and solution
everything works on Windows 7
It work when invoked from a shortcut on Win &, hmm that's very strange.
I tested it on Windows 10 also works.
Thanks for the helpful suggestions as well as your efforts to test this problem on your own systems. I finally found an answer that satisfies my needs. I used pyinstaller to create an executable and then launch the executable from a desktop shortcut. This works, so I am considering my question closed.

PyDev doesn't recognise PyQt5

I am following a tutorial on pyqt, and got this code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cb = QCheckBox('Show title', self)
cb.move(20, 20)
cb.toggle()
cb.stateChanged.connect(self.changeTitle)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Checkbox')
self.show()
def changeTitle(self, state):
if state == Qt.Checked:
self.setWindowTitle('Checkbox')
else: self.setWindowTitle('Unchecked!')
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I'm using PyDev on Eclipse. Suffice it to say that the code runs fine, but what is awkward is that PyDev underlines anything Qt/Q with a red line which when hovered over says Undefined variable: <..>. If it is undefined then how is it that my code runs without errors? Clearly this ought to be a problem with PyDev. I've removed the python interpreter (it was pointing to python2.7 instead of 3.4) and readded it as the correct version; but that didn't work. Interestingly enough, it recognises PyQt4 and insists on using widgets from that instead of PyQt5.
Just so you guys are aware, the code sample above is from another laptop which had PyQt5 as well. Both projects were from PyDev, and both had Ubuntu 15.04. It's possible that my importing of the project on my current machine messed up PyDev parsing the required libraries. Does anyone have a solution as to why PyDev doesn't recognise PyQt5?
I had the same problem. These steps worked for me.
Set the environment variable: export QT_API=pyqt5 (or whatever as appropriate)
restart eclipse so that picks up the new environment setting, and then add PyQt5 to the list of forced builtins for the interpreter (Window->preferences->pydev->interpreters->python interpreters) or look here http://www.pydev.org/manual_101_interpreter.html for more details.
The following SO question tipped me off to the presence of the variable: Setting up IPython Qtconsole with PyQt5. Before I set it, I as able to get some completion to work just by adding 'PyQt5' to the builtins, but it would not, for example, provide the full list of completions to something likefrom PyQt5.QtGui import, even though ipython stand-alone would. Further, the python console in pydev had the same problem and calling module_completion("from PyQt5.QtGui import Q") from Ipython.core.completerlib produced the same incomplete list. In the end, I guessed that since pydev was loading PyQt4 for the gui event loop (also configurable in the interpreter settings), there was a namespace conflict when it tried to introspect the Qt5 modules, causing it to bail out before it could build the full list of completions. Setting the environment variable causes pydev to load pyqt5 instead of the default pyqt4. I haven't checked, but it seems likely that set this way pydev will have problems completing pyqt4 references.
For all those lonesome internet wanderers trying to figure out how to integrate eclipse, pydev, and pyqt5 on Linux, I bring you my method from start to finish.
Eclipse, PyQt5, and PyDev on Linux
Install python v3.6
Install eclipse from eclipse.org
In eclipse, click Help->Install New Software
Click Add...
Add in software source "http://www.PyDev.org/updates" to the available software sources
Call it PyDev
Click on PyDev checkbox
Install it by clicking Next
Download PyQt5
Download SIP
Install SIP first
Install PyQt5
Reconfigure eclipse to use PyQt5
Click on Window→Preferences→PyDev→Interpreters→Python Interpreters
Click on Advanced Auto-Config
Rename interpreter to “python3.6”
Click on Libraries tab
Click on New Folder
Add in “/usr/lib/x86_64-linux-gnu/qt5/plugins”
Add in “/usr/lib/x86_64-linux-gnu/qt5/libexec”
Add in “/usr/lib/x86_64-linux-gnu/qt5/bin”
Click Apply
Click Apply and Close
Restart eclipse
Profit!
This will allow you to get the tab code completion in eclipse when developing pyqt5 applications.

How to make PyQt App give up window focus?

I have a python command line script that starts a PyQt Application showing the input of the video device in a little window. Then it starts a curses screen so that the user can control the camera with the keyboard.
After starting the script, the window focus is automatically on the PyQt window. Is there any way to tell PyQt to give up the window focus or curses / the terminal to reclaim it?
I use Linux (Mint Cinnamon) and the solution does not have to be OS independent.
You can try with setFocusPolicy here is the documentation. http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setFocusPolicy

Hide console for Tkinter app on OSX

I'm trying to hide the Terminal when I launch a GUI Tkinter based app, but when I double click the app.py file on OSX, the Terminal window appears. I've tried changing the extension to .pyw and tried launching it with /usr/bin/pythonw, but no matter what, the Terminal window still appears.
I've even tried adding the try/except below, but when I run it I get the error: 'invalid command name "console"' in the Terminal window that appears.
from Tkinter import *
class MainWindow(Tk):
def __init__(self):
Tk.__init__(self)
try:
self.tk.call('console', 'hide')
except TclError, err:
print err
win = MainWindow()
win.mainloop()
I haven't been able to find any way to hide the Terminal window from appearing. Anybody got any ideas?
By double-clicking a .py file on OS X, you are likely launching a Python gui instance via the Python Launcher.app supplied with OS X Pythons. You can verify that by selecting the .py file in the Finder and doing a Get Info on it. Python Launcher is a very simple-minded app that starts Python via a Terminal.app command. To directly launch your own Python GUI app, the preferred approach is to create a simple app using py2app. There's a brief tutorial here.
EDIT:
There are other ways, of course, but most likely any of them would be adding more levels of indirection. To make a normal launchable, "double-clickable" application, you need some sort of app structure. That's what py2app lets you create directly.
A very simple-minded alternative is to take advantage of the AppleScript Editor's ability to create a launcher app. In the AppleScript editor:
/Applications/Utilities/AppleScript
Editor.app in OS X 10.6
/Applications/AppleScript/Script
Editor.app in 10.5
make a new script similar to this:
do shell script "/path/to/python /path/to/script.py &> /dev/null &"
and then Save As.. with File Format -> Application. Then you'll have a double-clickable app that will launch another app. You can create something similar with Apple's Automater.app. But, under the covers, they are doing something similar to what py2app does for you, just with more layers on top.
Adding to the answer by Ned Deily, im my case when I tried to launch the Python application using an AppleScript application, it did not work initially. I discovered that it has something to to with some kind of encoding error (I am using UTF-8 and in the past I had felt need to configure it to UTF-8).
So, after further investigation, I discovered that I can accomplish this by creating an AppleScript application with the following code (adjusting the paths of python3 and of the Python application as needed):
do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/local/bin/python3 '/Users/USER/FOLDER/SCRIPT.py' &> /dev/null &"
It launches the Python application without any Terminal windows. The AppleScript application can then be personalised with a custom icon as usual, and can be placed in the Dock. When clicked, it will launch the Python intepreter, that still shows up in Dock, but with no visible windows.
I think this may be useful to other users.
'console hide' doesn't hide the Terminal in OS X. It hides Tk's built-in console, which is really a relic from the MacOS Classic days (and which is still commonly used on Windows).

Categories