How to set a Python Qt4 window icon? - python

I tried to use one icon from:
https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
PyQt4 set windows taskbar icon
But it does not show up on the window:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *
# Create an PyQT4 application object.
a = QApplication(sys.argv)
# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QWidget()
# Set window size.
w.resize(820, 240)
# Set window title
w.setWindowTitle("Hello World!")
# https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
undoicon = QIcon.fromTheme("camera-web")
w.setWindowIcon(undoicon)
# Show window
w.show()
sys.exit(a.exec_())
I am on Windows 10 with:
Anaconda conda --version -> conda 4.3.18
Python python --version -> Python 2.7.13 :: Anaconda custom (32-bit)

The documentation says
By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of your themeSearchPaths() and set the appropriate themeName().
This function was introduced in Qt 4.6.
Since you would need to gather a theme anyways to ship with the application, you could also just choose to collect some icons and specify the files directly.
w.setWindowIcon( QtGui.QIcon("folder.png") )

Related

how to make the applications developed by pyqt5 support Chinese input on ubuntu

On ubuntu, I developed an application using pyqt5, but it does not support Chinese input(My sogouPinyin input method cannot show Chinese context menu when I want to type text on my application). My OS supports Chinese input. How to fix it?
install some packages.
sudo apt install fcitx-frontend-qt5 fcitx-libs-qt fcitx-libs-qt5
copy lib to pyqt folder.
sudo cp /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so ${PYTHON_HOME}/lib/site-packages/PyQt5/Qt/plugins/platforminputcontexts/
add os.environ['QT_IM_MODULE'] = 'fcitx' in main file. (maybe not necessary.)
reference
sogouPinyin is based on Fcitx, so for the case of Qt applications the docs indicates that you have to use the flag QT_IM_MODULE:
from PyQt5 import QtWidgets
if __name__ == '__main__':
import sys
import os
os.environ['QT_IM_MODULE'] = 'fcitx'
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(w)
lay.addWidget(QtWidgets.QLineEdit())
lay.addWidget(QtWidgets.QTextEdit())
w.show()
sys.exit(app.exec_())
After the application is open you must place the focus in an editing widget such as QLineEdit, QTextEdit and press Ctrl + Space to enable it.
But you have to have installed fcitx-qt5 package:
On Ubuntu:
sudo apt-get install fcitx-qt5 fcitx fcitx-frontend-qt5

Does PyQt5/pyqt4 already supports QtVirtualKeyboard with Handwriting Recognition?

I'm working on a desktop application using pyqt5, and I want to use a Virtual Keyboard with Handwriting Recognition. I saw that Qt, QtVirtualKeyboard already support it.
Here's a link!
I got the C++ Qt example code running on QtCreator.
But using python3.5 and PyQt5 it gives this message:
module "QtQuick.VirtualKeyboard" is not installed
import QtQuick.VirtualKeyboard 2.1
How should I go on from here?
Does PyQt5 comes with VirtualKeyboard module? if no How to install it on PyQt5?
for qt desinger you can add only this line on your .py file.
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
but If you want use QML with qtvirtualkeyboard;
There is no virtualkeyboard plugin in pyqt5.8, you must be use qt's paths.
For a example, basic steps for pyqt5, qt5.8 and qtvirtualkeyboard installiation on ubuntu:
1.step install qt5.8 with qtvirtualkeyboard
wget
http://download.qt.io/official_releases/qt/5.8/5.8.0/qt-opensource-linux-x64-5.8.0.run
chmod +x qt-opensource-linux-x64-5.8.0.run
./qt-opensource-linux-x64-5.8.0.run
2.step
apt-get install python3 python3-pip
pip3 install pyqt5
3.step
set environmental variables your qt paths on your python code.
import sys, os
os.environ["QT_DIR"] = "/opt/Qt5.8.0/5.8/gcc_64"
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins/platforms"
os.environ["QT_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins"
os.environ["QML_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QML2_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
#print(os.environ)
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtQuick import *
class keyboardapp(object):
def __init__(self):
self.view = QQuickView()
self.view.setObjectName("View")
#self.view.setFlags(Qt.FramelessWindowHint)
self.view.setSource(QUrl("main.qml"))
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
#self.Screen = self.view.rootObject()
#print("Screen(Root) = " + str(self.Screen))
self.view.show()
app = QApplication(sys.argv)
test = keyboardapp()
sys.exit(app.exec_())
I been stuck with this too, and i am new to Qt
After some researching, and digging in source code, found the solution
You dont need to import it to use it since its a module it will implement itself to every qt input
Well you dont need to install it on PyQt5 but on Qt5 if it is not already come with your Qt package, if you use archlinux you can install it with pacman
pacman -S qt5-virtualkeyboard
If you cannot find it in you os repositories try to build it here is documantation
https://doc.qt.io/qt-5/qtvirtualkeyboard-index.html
Then to use it in your pyqt application, set environmental variable QT_IM_MODULE to "qtvirtualkeyboard" either from your bash or inside the top your script like
import os
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"

Executable from Python2 script w. PyInstaller: Qt4 module import error

I have a project with a GUI created in Qt Designer and compiled with pyuic. So I import QtCore and QtGui from PyQt4.
The script runs OK.
I need an executable file. The tool is PyInstaller, target platforms - Linux and Windows. I've tried and had a success once. Then I developed a project for some time and now... I cannot make an executable - it crashes with
ImportError: No module named QtCore
The problem is that I cannot compare current project with the old one. And I'm not sure how the environment in my PC has changed.
So I have to understand why PyInstaller makes an executable with no error messages - but the program crashes. Or how to help PyInstaller (I've read manual and tried a lot from it but to no avail).
Here is a simplified version of my project (actually a single file) that has the main feature: it runs OK from Python and crashes as standalone program.
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" test script to learn PyInstaller usage
"""
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_ADCmonitor()
self.ui.setupUi(self)
self.exitAction = QtGui.QAction('Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.triggered.connect(self.close)
toolbar = self.addToolBar('Exit')
toolbar.addAction(self.exitAction)
self.refresh = QtCore.QTimer()
self.status_freeze_timer = QtCore.QTimer()
self.update_data()
def update_data(self):
self.status_refresh('Ok')
self.refresh.singleShot(200, self.update_data)
def status_refresh(self, msg):
self.ui.statusbar.showMessage(msg)
class Ui_ADCmonitor(object):
def setupUi(self, ADCmonitor):
ADCmonitor.setObjectName("ADCmonitor")
ADCmonitor.resize(300, 300)
self.statusbar = QtGui.QStatusBar(ADCmonitor)
self.statusbar.setObjectName("statusbar")
ADCmonitor.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(ADCmonitor)
ADCmonitor.setWindowTitle("ADC Monitor")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
And please don't propose me to use other dist utilities. I tried some of them, I've chosen PyInstaller and I'd like to use it.
So, the problem is in a PyInstaller 2.1 bug - as Joran Beasley supposed.
Solution:
sudo pip install git+https://github.com/pyinstaller/pyinstaller.git
And bingo!
pyinstaller myscript.py makes correct exec.

PySide "hello world": py2exe/pyinstaller/cx_freeze and access violation

I am trying to build a very basic executable (Windows) using PySide. The following script runs properly in the interpreter (Python 2.7, PySide 1.1.2)
#!/usr/bin/python
import sys
sys.stdout = open("my_stdout.log", "w")
sys.stderr = open("my_stderr.log", "w")
import PySide.QtGui
from PySide.QtGui import QApplication
from PySide.QtGui import QMessageBox
# Create the application object
app = QApplication(sys.argv)
# Create a simple dialog box
msgBox = QMessageBox()
msgBox.setText("Hello World - using PySide version " + PySide.__version__)
msgBox.exec_()
I tried 3 methods (py2exe, pyinstaller and cx_freeze) and all the 3 generated executables fail to execute. The two stdout/stderr files appear, so I found the first PySide import is making everything fail. (Unhandled exception/Access violation)
I analyzed the executable file with depends (http://www.dependencywalker.com/) and everything looks correctly linked.
Any idea?
You need to add the atexit module as an include. source: http://qt-project.org/wiki/Packaging_PySide_applications_on_Windows
(is also the case for Linux btw)
Thank you for your help. Actually, this did not change anything :/ However, I found a solution to my problem: if I add from PySide import QtCore, QtGui, then the executable (with pyinstaller) does work!

python cross platform apps

I'm trying to make an app in CPython that should work on both linux and windows.
I'm using the webkit library, witch works fine on linux (Ubuntu 12.04), but I can't get it to work on Windows.
I know that I can compile my app into a Windows executable (.exe) with py2exe, but to do that it must work on my Windows machine.
The question is: Is there any way I can package my app under linux, so it will have it's dependencies (webkit) bundled with it, so it will work under Windows? Or is there any way to make a windows executable that doesn't need any dependencies from a python file, under linux?
Thank you!
EDIT:
Here is my code for the test app:
import gtk
import webkit
class Base:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file("youtubeWindow.ui")
self.main_window = self.builder.get_object("main_window")
self.scrl_window = self.builder.get_object("scrl_window")
self.webview = webkit.WebView()
self.scrl_window.add(self.webview)
self.webview.show()
self.webview.open("http://youtu.be/o-akcEzQ6Y8")
self.main_window.show()
def main(self):
gtk.main()
print __name__
if __name__ == "__main__":
base = Base()
base.main()
Ok, so i couldn't get webkit to work on windows with GTK, but i found out that Qt provides an integrated WebKit module, so I donwloaded PySide (the Qt wrapper for python) and I tested it with this script:
import sys
from PySide import QtCore
from PySide import QtGui
from PySide import QtWebKit
class MainWindow (QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(300,300,800,600)
self.setWindowTitle('QtPlayer')
web = QtWebKit.QWebView(self)
web.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
web.load(QtCore.QUrl("http://youtu.be/Dys1_TuUmI4"))
web.show()
self.show()
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Also i used GUI2EXE and *cx_Freeze* to package it into an .exe windows app. (Don't forget to include the modules atexit,PySide.QtNetwork, more details here)
A cool guide on Qt-Webkit can be found here (It usese PyQt, but it's compatible with Pyside)
Also a Pyside tutorial here
In order run your script on Windows you need to install Webkit and its bindings for Windows (libraries). The 2 links below provide the setup files and instructions.
http://opensourcepack.blogspot.com/2009/12/pywebkitgtk-windows-binary.html
http://opensourcepack.blogspot.com/2011/01/conservative-all-in-one-pygtk-installer.html
The second link provides a binary that installs all the needed libraries (a all-in-one package).

Categories