sudo apt install python3-pyqt5 python3-pyqt5.qtwebengine
from PyQt5 .QtCore import Qt , QTimer
QTimer .singleShot (0 , lambda : myfun )
from PyQt5 .QtCore import Qt , QTimer
def tick ():
print 'tick'
timer = QTimer ()
timer .timeout .connect (tick )
QTimer .singleShot (0 , lambda : timer .start (1000 )) # must be ran in event loop
from PyQt5 .QtCore import Qt
from PyQt5 .QtWidgets import *
from PyQt5 import QtGui
app = QApplication ([])
window = QWidget ()
window .setWindowFlags (Qt .WindowStaysOnTopHint )
window .show ()
app .exec_ ()
getattr (window , "raise" )()
from PyQt5 .QtCore import Qt
from PyQt5 .QtWidgets import *
from PyQt5 import QtGui
def on_click ():
print ('PyQt5 button click' )
app = QApplication ([])
window = QWidget ()
window .setWindowFlags (Qt .WindowStaysOnTopHint )
layout = QVBoxLayout ()
button_up = QPushButton ('Transpose Up' )
button_up .setIcon (QtGui .QIcon ('up.png' ))
button_up .clicked .connect (on_click )
button_down = QPushButton ('Transpose Down' )
button_down .setIcon (QtGui .QIcon ('down.png' ))
layout .addWidget (button_up )
layout .addWidget (button_down )
window .setLayout (layout )
window .show ()
app .exec_ ()
from PyQt5 .QtCore import Qt
from PyQt5 .QtWidgets import *
def on_item_click (item ):
print ('checked:' , item .checkState () == Qt .Checked )
print ('data' , item .data (Qt .UserRole ))
app = QApplication ([])
window = QWidget ()
window .setWindowFlags (Qt .WindowStaysOnTopHint )
layout = QVBoxLayout ()
listwidget = QListWidget ()
listwidget .itemClicked .connect (on_item_click )
def create_item (text , data ):
item = QListWidgetItem (text )
item .setFlags (item .flags () | Qt .ItemIsUserCheckable )
item .setCheckState (Qt .Unchecked )
item .setData (Qt .UserRole , data )
return item
listwidget .addItem (create_item ('Item 1' , 'item1' ))
layout .addWidget (listwidget )
window .setLayout (layout )
window .show ()
app .exec_ ()
from PyQt4 .QtCore import QTimer
def tick ():
print 'tick'
timer = QTimer ()
timer .timeout .connect (tick )
timer .start (1000 )
from PyQt5 .QtCore import Qt , QUrl
from PyQt5 .QtWidgets import *
from PyQt5 import QtGui
from PyQt5 .QtWebEngineWidgets import QWebEngineView as QWebView
app = QApplication ([])
win = QWebView ()
url = 'https://www.google.pl/'
win .load (QUrl (url ))
win .show ()
app .exec_ ()
from PyQt5 .QtCore import Qt
from PyQt5 .QtWidgets import *
from PyQt5 import QtGui
app = QApplication ([])
win = QWidget ()
grid = QGridLayout ()
def get_widget (i ):
return QPushButton ("Button %d" % i )
widgets = [get_widget (i ) for i in range (16 )]
pos = 0
for i in range (1 , 5 ):
for j in range (1 , 5 ):
grid .addWidget (widgets [pos ], i , j )
pos += 1
win .setLayout (grid )
win .show ()
app .exec_ ()
QWebPage javascript callback
from PyQt4 import QtCore , QtGui , QtWebKit
class MyDialog (QtGui .QMainWindow ):
def __init__ (self ):
super (MyDialog , self ).__init__ ()
self .setWindowTitle ("QWebView JavaScript Events" )
web_view = QtWebKit .QWebView (self )
web_view .setHtml ("""
<html>
<body>
<input type="text" name="text1"/><br/>
<input type="text" name="text2"/><br/>
<input type="submit" value="OK"/>
</body>
</html>""" )
self .setCentralWidget (web_view )
main_frame = web_view .page ().mainFrame ()
main_frame .addToJavaScriptWindowObject ("MainWindow" , self )
doc_element = main_frame .documentElement ()
doc_element .evaluateJavaScript ("""
function isTextElement(el) {
return el.tagName == "INPUT" && el.type == "text";
}
this.addEventListener("DOMFocusIn", function(e) {
if (isTextElement(e.target)) {
MainWindow.handleFocusIn(e.target.name);
}
}, false);
this.addEventListener("DOMFocusOut", function(e) {
if (isTextElement(e.target)) {
MainWindow.handleFocusOut(e.target.name);
}
}, false)
""" )
self .resize (300 , 150 )
@QtCore .pyqtSlot (QtCore .QVariant )
def handleFocusIn (self , element_name ):
QtGui .QMessageBox .information (
self , "HTML Event" , "Focus In: " + element_name )
@QtCore .pyqtSlot (QtCore .QVariant )
def handleFocusOut (self , element_name ):
QtGui .QMessageBox .information (
self , "HTML Event" , "Focus Out: " + element_name )
app = QtGui .QApplication ([])
dialog = MyDialog ()
dialog .show ()
app .exec_ ()
https://gist.github.com/pberkes/5266744