The tutorial I created is a test and use of the Selenium WebDriver python package for to automate web browser interaction from Python.
This tutorial can be found on my GitHub account.
Python tutorials with source code, examples, guides, and tips and tricks for Windows and Linux development.
#!/usr/bin/python
import sys
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
class Button(QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
def mouseMoveEvent(self, e):
if e.buttons() != Qt.RightButton:
return
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.exec_(Qt.MoveAction)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == Qt.LeftButton:
print('press')
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
self.button = Button('Button', self)
self.button.move(100, 65)
self.setWindowTitle('Drag and drop with mouse - right click to move Button!')
self.setGeometry(300, 300, 550, 450)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
position = e.pos()
self.button.move(position)
e.setDropAction(Qt.MoveAction)
e.accept()
def main():
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
if __name__ == '__main__':
main()
import sys, os
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
class ImageLabel(QLabel):
def __init__(self):
super().__init__()
self.setAlignment(Qt.AlignCenter)
self.setText('\n\n Drop Image Here \n\n')
self.setStyleSheet('''
QLabel{
border: 3px dashed #bbb
}
''')
def setPixmap(self, image):
super().setPixmap(image)
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Drag and drop one image to this window!')
self.setGeometry(300, 300, 550, 450)
self.setAcceptDrops(True)
mainLayout = QVBoxLayout()
self.photoViewer = ImageLabel()
mainLayout.addWidget(self.photoViewer)
self.setLayout(mainLayout)
def dragEnterEvent(self, event):
if event.mimeData().hasImage:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasImage:
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasImage:
event.setDropAction(Qt.CopyAction)
file_path = event.mimeData().urls()[0].toLocalFile()
self.set_image(file_path)
event.accept()
else:
event.ignore()
def set_image(self, file_path):
self.photoViewer.setPixmap(QPixmap(file_path))
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())
C:\Python310>python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
...
SyntaxError: '{' was never closed
...
>>> foo(a, b for b in range(5), c)
...
foo(a, b for b in range(5), c)
^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized
...
>>> {a, b for (a, b) in zip("a", "b")}
...
{a, b for (a, b) in zip("a", "b")}
^^^^
SyntaxError: did you forget parentheses around the comprehension target?
...
SyntaxError: expected ':'
...
SyntaxError: invalid syntax. Perhaps you forgot a comma?
...
SyntaxError: ':' expected after dictionary key
...
SyntaxError: expected 'except' or 'finally' block
...
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
...
IndentationError: expected an indented block after 'if' statement in line ...
...
>>> import collections
>>> collections.namedtoplo
...
AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: 'namedtuple'?
...
>>> a = 0
>>> aa
...
NameError: name 'aa' is not defined. Did you mean: 'a'?
$ pip install pyqt5
class TileServer(object):
def __init__(self):
self.imagedict = {}
self.mydict = {}
self.layers = 'ROADMAP'
self.path = './'
self.urlTemplate = 'http://ecn.t{4}.tiles.virtualearth.net/tiles/{3}{5}?g=0'
self.layerdict = {'SATELLITE': 'a', 'HYBRID': 'h', 'ROADMAP': 'r'}
def tiletoquadkey(self, xi, yi, z, layers):
quadKey = ''
for i in range(z, 0, -1):
digit = 0
mask = 1 << (i - 1)
if(xi & mask) != 0:
digit += 1
if(yi & mask) != 0:
digit += 2
quadKey += str(digit)
return quadKey
def loadimage(self, fullname, tilekey):
im = Image.open(fullname)
self.imagedict[tilekey] = im
return self.imagedict[tilekey]
def tile_as_image(self, xi, yi, zoom):
tilekey = (xi, yi, zoom)
result = None
try:
result = self.imagedict[tilekey]
print(result)
except:
print(self.layers)
filename = '{}_{}_{}_{}.jpg'.format(zoom, xi, yi, self.layerdict[self.layers])
print("filename is " + filename)
fullname = self.path + filename
try:
result = self.loadimage(fullname, tilekey)
except:
server = random.choice(range(1,4))
quadkey = self.tiletoquadkey(*tilekey)
print (quadkey)
url = self.urlTemplate.format(xi, yi, zoom, self.layerdict[self.layers], server, quadkey)
print ("Downloading tile %s to local cache." % filename)
urllib.request.urlretrieve(url, fullname)
#urllib.urlretrieve(url, fullname)
result = self.loadimage(fullname, tilekey)
return result
import sys
from PyQt6.QtWidgets import QApplication, QWidget
def main():
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 200)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()
[root@desk mythcat]# dnf search PyQt6
Last metadata expiration check: 2:03:10 ago on Tue 06 Jul 2021 08:52:41 PM EEST.
No matches found.
[mythcat@desk ~]$ /usr/bin/python3 -m pip install --upgrade pip
...
WARNING: The scripts pip, pip3 and pip3.9 are installed in '/home/mythcat/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning,
use --no-warn-script-location.
Successfully installed pip-21.1.3
[mythcat@desk ~]$ pip install PyQt6 --user
...
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel
def main():
app = QApplication(sys.argv)
win = QLabel()
win.resize(640, 498)
win.setText("Qt is awesome!!!")
win.show()
app.exec()
if __name__ == "__main__":
main()