What I am trying to do is make an on screen keyboard.
To do this I need to stop the Program from taking focus away from other windows.
Here is the code I have that keeps the window on top.
import sys
from PyQt4 import QtGui, QtCore, Qt
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow.show()
app.exec_()
(Note: Example from Keep Window on Top)
So what I want to do is add code to stop the window taking focus.
Thanks
Change focus policy of window and all of its contents QWidget::setFocusPolicy
Related
I am learning how to use PyQt5 and I came across this issue where "my first label" does not complete display on my screen.
Display after running the code:
Code:
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) #enable highdpi scaling
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) #use highdpi icons
def window():
app = QApplication(sys.argv)
win = QMainWindow()
win = QMainWindow()
win.setGeometry(200, 200, 400, 400)
win.setWindowTitle("Tech with Aeijan")
label = QtWidgets.QLabel(win)
label.setText("my first label!")
label.move(50,50)
win.show()
sys.exit(app.exec_())
window()
QLabel adapts its contents based on the (possible) parent layout manager, but you didn't use any, so it doesn't know how to correctly display itself or adapt its size to do that.
The simplest solution is to call label.adjustSize(), which will cause the label to resize itself so that it will be able to display its contents.
That wouldn't be a very good idea, though: you are trying to use a fixed position for a widget (which is normally considered a bad thing to do, for plenty of reasons); the result will be that if the label text is too big and the user resizes the window, the text won't be completely visible as it should be, nor the label would know how to resize or eventually wrap its contents to do ensure that all its text is shown.
The better approach is to use a layout manager, but that is a solution reserved for simpler widgets (like a QWidget or a QDialog); a QMainWindow doesn't work like that, and it requires a central widget to be set to ensure that its contents are correctly displayed and managed.
In your case, you could simply use self.setCentralWidget(label), but that would prevent you to add any other widget to your window.
A "container" widget should be used instead, and that widget would be set as the central one for the main window; then you can set a layout for that widget and add the label to it:
def window():
app = QApplication(sys.argv)
win = QMainWindow()
central = QWidget()
win.setCentralWidget(central)
layout = QVBoxLayout()
central.setLayout(layout)
# alternatively, the above is the same as this:
# layout = QVBoxLayout(central)
label = QtWidgets.QLabel(win)
label.setText("my first label!")
layout.addWidget(label)
# ...
I'm making something akin to a screen recorder using the PyQT library. My problem is that the only way I can think to get the recording part of the application to run is in the "paint event" part of the widget class. Here's some code for example:
class MainWindow(QWidget):
def __init__(self):
#setup window
def initUI(self):
#init UI stuff
def paintEvent(self, event):
#capture the screen and then display it on this widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
My main problem is on the paintevent area. I could start a thread and let the at capture and save frames, but I want to actively display each frame on the window. This can work while the widget has focus, but once the mouse moves away, and the window loses focus, then it stops because the paintevent is not being activated.
Is there anyway to solve this? Thank you!
I'm working on an applcation in Python's PyQt4 and cannot find how to change the taskbar icon. I made my .ui files in Qt's Designer, where I can change the windowIcon properties. But that is not what I am looking for. I want to change the look of the application's icon in windows taskbar. For now it is Python logo in a window icon.
I found some information on SO: link but it's not helping me much.
I tried:
app = QtGui.QApplication([])
app.setWindowIcon(QtGui.QIcon('chip_icon_normal.png'))
app.exec_()
But the icon remains unchanged.
What i want to change, showing the picture:
(This is done calling the setWindowIcon on main window/ dialog, or the application, as shown above.)
This problem is caused by some peculiarities in how taskbar icons are handled on the Windows platform.
See this answer for details, along with a workaround using ctypes.
It seems to me that the problem may be caused by lack of icon with the right size.
The following setup worked for me in PyQT4:
# set app icon
app_icon = QtGui.QIcon()
app_icon.addFile('gui/icons/16x16.png', QtCore.QSize(16,16))
app_icon.addFile('gui/icons/24x24.png', QtCore.QSize(24,24))
app_icon.addFile('gui/icons/32x32.png', QtCore.QSize(32,32))
app_icon.addFile('gui/icons/48x48.png', QtCore.QSize(48,48))
app_icon.addFile('gui/icons/256x256.png', QtCore.QSize(256,256))
app.setWindowIcon(app_icon)
I have got a task bar icon in Windows 7 and correct icons in all windows without any changes to ui files.
You need to call setWindowIcon(...) on the window, not on the application.
Here's an example, which works for me:
#!/usr/bin/env python3
import os
import sys
import subprocess
import os.path
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWin(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.setWindowTitle("My Window")
self.setWindowIcon(QtGui.QIcon('test_icon.png'))
self.show()
def main(args):
app = QtGui.QApplication([])
ww= MyWin()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv[1:])
For me, the following code works for both changing task bar icon and window icon
win.setWindowIcon(QIcon('logo.png'))
I created a small window using PyQt4 and Pydev. The code is below:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
# Create GUI object
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.setGeometry(400,300,800,800) # Position window
widget.resize(450,250) # Resize window
widget.setWindowTitle('Sample') # Set Title of the window
Password = QtGui.QLineEdit() # Input Box for password
widget.show() # Display window
# Exit program
sys.exit(app.exec_())
I created the Password LineEdit box but how to show on the active window, which is represented by widget?
Just use
Password = QtGui.QLineEdit(widget)
This tells Qt that you want widget to be the parent of the QLineEdit. If you leave out the widget, then the QLineEdit has no parent, so it's not shown.
Update: To position child items in parent windows, you'll have to read up about layouts (I assume you want to do it properly, not as a toy/learning exercise). Any good PyQt book should be able to help, e.g. this one.
This is on PyQt4, Linux and Python 2.5
Can I make PyQt set my window "always on top" over other applications?
For example, in GTK i use the property: Modal.
Now, in PyQt I am using a QWidget, but, I can't find a way to do that.
Any ideas??
Pass the QMainWindow the WindowStaysOnTopHint window flag (or use setWindowFlags).
As in the name, this is a hint to the windowing manager (not a hard guarantee).
Simplest possible example:
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
setwindowaFlags is a method that can call it from form object and just take one parameter is a constant QtCore.Qt.WindowStaysOnTopHint that refer to make your form Stays On Top