I have never code in Python and trying to switch from Javascrpt/SVG. Being confused by variable scope in Python and process flow, I will appreciate any correction to those basic code to make it draw rectangle by mousedown and mouseup events. Please don't put links to instructions unless you didn't point me on errors in code.
if name=="main":
import wx
import math
class myframe(wx.Frame):
pt1 = 0
pt2 = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, "test", size=(500,400))
self.Bind(wx.EVT_LEFT_DOWN, self.onDown)
self.Bind(wx.EVT_LEFT_UP, self.onUp)
self.Bind(wx.EVT_PAINT, self.drawRect)
def onDown(self, event):
global pt1
pt1 = event.GetPosition() # firstPosition tuple
def onUp(self, event):
global pt2
pt2 = event.GetPosition() # secondPosition tuple
def drawRect(self, event):
dc = wx.PaintDC(self)
gc = wx.GraphicsContext.Create(dc)
nc = gc.GetNativeContext()
ctx = Context_FromSWIGObject(nc)
ctx.rectangle (pt1.x, pt1.y, pt2.x, pt2.y) # Rectangle(x0, y0, x1, y1)
ctx.set_source_rgba(0.7,1,1,0.5)
ctx.fill_preserve()
ctx.set_source_rgb(0.1,0.5,0)
ctx.stroke()
app = wx.App()
f = myframe()
f.Show()
app.MainLoop()
Yeh, you have a problem with scopes (plus - your code isn't showing properly).
Let me give you a short example how to use members and globals in python:
# Globals are defined globally, not in class
glob1 = 0
class C1:
# Class attribute
class_attrib = None # This is rarely used and tricky
def __init__(self):
# Instance attribute
self.pt1 = 0 # That's the standard way to define attribute
def other_method(self):
# Use of a global in function
global glob1
glob1 = 1
# Use of a member
self.pt1 = 1
# Use of a class attribute
C1.class_attrib = 1
In your code you are mixing all types of variables. I think you should just make pt1 and pt2 instance attributes, so your code would look like:
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "test", size=(500,400))
self.pt1 = self.pt2 = 0
...
def onDown(self, event):
self.pt1 = event.GetPosition() # firstPosition tuple
...
You could consider reading some general tutorial like this one, to learn how Python scoping works.
Related
I am trying to animate a 2d Object (drawn as a path), therefore it needs to be redrawn. What is the best way to redraw it without having a blinking object?
After redrawing it with self.Refresh() when the onIdle-Event is called, then I used a timer with a fixed time to call self.Refresh(), which works way better. But still I have the problem of a blinking object.
import wx
import math
import time
class ObjectDrawer(wx.Frame):
def __init__(self, *args, **kw):
# Initialize vars
self.dc = None
self.gc = None
self.lastTime = time.time()
super(ObjectDrawer, self).__init__(*args, **kw)
self.InitUI()
def InitUI(self):
self.timer = wx.Timer(self)
# Initialize the GUI
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_TIMER, self.evt_timer)
self.ShowFullScreen(True)
self.SetBackgroundColour('white')
def evt_timer(self, event):
self.Refresh()
def drawObjects(self):
path = self.gc.CreatePath()
#Add Something to the path e.g. a circle
path.AddCircle(100,100,50)
self.gc.StrokePath(path)
path = None
def OnPaint(self, e):
dc = wx.PaintDC(self)
self.gc = wx.GraphicsContext.Create(dc)
self.gc.SetPen(wx.Pen('#e8b100', 5, wx.LONG_DASH))
self.drawObjects()
self.timer.Start(1000/60)
app = wx.App()
window = ObjectDrawer(None)
window.Show()
app.MainLoop()
If you set self.Refresh() to self.Refresh(False) the flickering disapears. You could also use wx.AutoBufferedPaintDC instead of wx.PaintDC. Take a look at this example in the wxpython wiki for a more complex example.
I'm trying to update QLineEdit() with setText.
However, that is made through a signal thread class (A)
that is able to send every time signal to a widget class (B) [deputy to change the text].
let see an example:
classB(QWidget):
def __init__(self, parent = None):
super(classB, self).__init__(parent)
self.lineLayout = QGridLayout()
self.textbox = QLineEdit("")
self.lineLayout.addWidget(self.textbox, 0, 0)
self.setLayout(self.lineLayout)
def setInt(self,intVal):
# The shell displays this value perfectly
print(intVal)
# it does not display the change in QWidget that lives in MainWidget
self.textbox.setText("val: " + str(intVal))
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
While the class A's code:
classA(QThread):
sendVal = QtCore.pyqtSignal(int)
def __init__(self,serial):
QThread.__init__(self)
def do_stuff(self):
cont = 0
while True:
self.sendVal(cont)
cont += 1
So we connect the signal:
class MainWidget(QtWidgets.QWidget):
getClassA = classA()
getClassB = classB()
getClassA.sendVal.connect(getClassB.setInt)
I have observed the following behaviors:
The int signal is perfectly received within the [setInt] function of class B;
The real problem is that everything that happens inside the setInt function stays in setInt. I can not even change a hypothetical class variable (In def init of classB)
I'm adding a color parameter to the LineBand subclass of QWidget. I've found several examples of how to add additional parameters to a subclass in Python 3 and believe I've followed the advice. Yet, when I call the new version of the class using box = LineBand(self.widget2, color), I get the error File "C:/Users/...", line 63, in showBoxes ... box = LineBand(viewport, color) ... TypeError: __init__() takes 2 positional arguments but 3 were given. But, I'm only calling LineBand with 2 arguments, right? Below is the complete code. I've commented all the sections I've changed. I've also commented out the code that changes the background color of the text in order to see the colored lines more clearly (when they actually are drawn). The background color code works fine.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
db = ((5,8,'A',Qt.darkMagenta),(20,35,'B',Qt.darkYellow),(45,60,'C',Qt.darkCyan)) # added color to db
class TextEditor(QTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
text="This is example text that is several lines\nlong and also\nstrangely broken up and can be\nwrapped."
self.setText(text)
cursor = self.textCursor()
for n in range(0,len(db)):
row = db[n]
startChar = row[0]
endChar = row[1]
id = row[2]
color = row[3] # assign color from db to variable
cursor.setPosition(startChar)
cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, endChar-startChar)
#charfmt = cursor.charFormat()
#charfmt.setBackground(QColor(color)) # assign color to highlight (background)
#cursor.setCharFormat(charfmt)
cursor.clearSelection()
self.setTextCursor(cursor)
def getBoundingRect(self, start, end):
cursor = self.textCursor()
cursor.setPosition(end)
last_rect = end_rect = self.cursorRect(cursor)
cursor.setPosition(start)
first_rect = start_rect = self.cursorRect(cursor)
if start_rect.y() != end_rect.y():
cursor.movePosition(QTextCursor.StartOfLine)
first_rect = last_rect = self.cursorRect(cursor)
while True:
cursor.movePosition(QTextCursor.EndOfLine)
rect = self.cursorRect(cursor)
if rect.y() < end_rect.y() and rect.x() > last_rect.x():
last_rect = rect
moved = cursor.movePosition(QTextCursor.NextCharacter)
if not moved or rect.y() > end_rect.y():
break
last_rect = last_rect.united(end_rect)
return first_rect.united(last_rect)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.edit = TextEditor(self)
layout = QVBoxLayout(self)
layout.addWidget(self.edit)
self.boxes = []
def showBoxes(self):
while self.boxes:
self.boxes.pop().deleteLater()
viewport = self.edit.viewport()
for start, end, id, color in db: # get color too
rect = self.edit.getBoundingRect(start, end)
box = LineBand(viewport, color) # call LineBand with color as argument
box.setGeometry(rect)
box.show()
self.boxes.append(box)
def resizeEvent(self, event):
self.showBoxes()
super().resizeEvent(event)
class LineBand(QWidget):
def __init__(self, color): # define color within __init__
super().__init__(self)
self.color = color
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QPen(color, 1.8)) # call setPen with color
painter.drawLine(self.rect().topLeft(), self.rect().bottomRight())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
window.showBoxes()
app.exec_()
sys.exit(app.exec_())
When a method is not overwritten it will be the same as the implemented method of the parent so if you want it to work you must add those parameters, since these depend many times on the parent a simple way is to use *args and **kwargs and pass the new parameter as the first parameter. In addition you must use self.color instead of color since color only exists in the constructor.
class Window(QWidget):
[...]
def showBoxes(self):
while self.boxes:
self.boxes.pop().deleteLater()
viewport = self.edit.viewport()
for start, end, id, color in db: # get color too
rect = self.edit.getBoundingRect(start, end)
box = LineBand(color, viewport) # call LineBand with color as argument
box.setGeometry(rect)
box.show()
self.boxes.append(box)
[...]
class LineBand(QWidget):
def __init__(self, color, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.color = color
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QPen(self.color, 1.8)) # call setPen with color
painter.drawLine(self.rect().topLeft(), self.rect().bottomRight())
Output:
I'm making an application that shows a map of an area and I'm trying to draw nodes on top of it which can represent information.
I made it all work, but did so simply by making one custom widget which I showed and printing everything again and again everytime information changed. Also I couldn't 'connect' the nodes to listeners, because they were just images in the original widget.
This made me want to reform my GUI and now I'm trying to make every class a custom widget! But there's a problem, my MapNodes aren't showing up anymore.
I searched stackoverflow and found this helpful thread:
How to set absolute position of the widgets in qt
So I have to give my mapnodes a parent, and parent = the widget that is being shown (?)
Anyway, here is my throw at it, pasting the relevant code here. Hint at where stuff might go horrible wrong: all the inits
app = QtGui.QApplication(list())
mutexbranch = Lock()
mutexnode = Lock()
def exec():
return app.exec_()
#Singleton Pattern: wanneer en object aan iets moet kunnen
# waar het inherent door de structuur niet aankon
# wordt dit via dit singleton opgelost
class GuiInternalCommunication:
realmap = 0
class MapView(QtGui.QWidget, listener.Listener):
def __init__(self, mapimagepath):
QtGui.QMainWindow.__init__(self)
listener.Listener.__init__(self)
self.map = Map(self, mapimagepath)
#self.setCentralWidget(self.map)
self.initUI()
def initUI(self):
self.setWindowTitle('Population mapping')
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.map)
self.setLayout(hbox)
resolution = QtGui.QDesktopWidget().screenGeometry()
self.setGeometry(20,20,550,800)
self.show()
######################################################################
class Map(QtGui.QWidget):
def __init__(self, parent, mapimagepath):
QtGui.QWidget.__init__(self, parent)
#self.timer = QtCore.QBasicTimer()
#coordinaten hoeken NE en SW voor kaart in map graphics van SKO
self.realmap = RealMap(
mapimagepath,
(51.0442, 3.7268),
(51.0405, 3.7242),
550,
800)
GuiInternalCommunication.realmap = self.realmap
self.needsupdate = True
self.timelabel = 0
parent.setGeometry(0,0,self.realmap.width, self.realmap.height)
self.mapNodes = {}
self.mapBranches = {}
def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
rect = self.contentsRect()
#teken achtergrond
self.realmap.drawRealMap(painter)
#teken branches
mutexbranch.acquire()
try:
for branch, mapBranch in self.mapBranches.items():
mapBranch.drawMapBranch(painter)
finally:
mutexbranch.release()
######################################################################
class RealMap(QtGui.QWidget):
def __init__(self, path, coordRightTop, coordLeftBot, width, height, pixpermet = 2.6):
super(RealMap, self).__init__()
self.path = path
self.mapimage = QtGui.QImage(self.path)
self.coordLeftBot = coordLeftBot
self.coordRightTop = coordRightTop
self.width = width
self.height = height
self.realdim = self.calcRealDim()
self.pixpermet = pixpermet
def paintEvent(self, e):
painter = QtGui.QPainter()
painter.begin(self)
self.drawRealMap(self, painter)
painter.end()
def drawRealMap(self, painter):
painter.drawImage(0,0,self.mapimage)
######################################################################
class MapNode(QtGui.QWidget):
dangertocolor = {"normal":"graphics//gradients//green.png",
"elevated":"graphics//gradients//orange.png",
"danger":"graphics//gradients//red.png"}
gradimage = {"normal":QtGui.QImage(dangertocolor["normal"]),
"elevated":QtGui.QImage(dangertocolor["elevated"]),
"danger":QtGui.QImage(dangertocolor["danger"])}
btimage = QtGui.QImage("graphics//BT-icon.png")
def __init__(self, scanner, x, y, danger = 0, parent = None):
# MapNode erft over van QWidget
super(MapNode, self).__init__()
QtGui.QWidget.__init__(self, parent)
self.scanner = scanner
self.x = x
self.y = y
self.danger = 'normal'
self.calcDanger(danger)
self.grads = {}
self.grad = QtGui.QImage(MapNode.dangertocolor[self.danger])
def paintEvent(self, e):
painter = QtGui.QPainter()
painter.begin(self)
self.drawMapNode(painter)
painter.end()
def drawMapNode(self, painter):
realmap = GuiInternalCommunication.realmap
radiusm = self.scanner.range
radiusp = radiusm*realmap.pixpermet
factor = radiusp/200 # basis grootte gradiƫnten is 200 pixels.
grad = MapNode.gradimage[self.danger]
grad = grad.scaled(grad.size().width()*factor, grad.size().height()*factor)
painter.drawImage(self.x-100*factor,self.y-100*factor, grad)
painter.drawImage(self.x-10, self.y-10,MapNode.btimage)
painter.drawText(self.x-15, self.y+20, str(self.scanner.sensorid) + '-' + str(self.scanner.name))
######################################################################
class MapBranch:
branchpens = {"normal": QtGui.QPen(QtCore.Qt.green, 3, QtCore.Qt.DashLine),
"elevated": QtGui.QPen(QtGui.QColor(255, 51, 0), 3, QtCore.Qt.DashLine), #mandarine orange hex is 255-165-0
"danger": QtGui.QPen(QtCore.Qt.red, 3, QtCore.Qt.DashLine)}
def __init__(self, branch, mapnode1, mapnode2, danger = 0):
self.mapnode1 = mapnode1
self.mapnode2 = mapnode2
self.branch = branch
self.danger = danger
self.calcDanger(danger)
def drawMapBranch(self, painter):
painter.setPen(MapBranch.branchpens[self.danger])
painter.drawLine(self.mapnode1.x,
self.mapnode1.y,
self.mapnode2.x,
self.mapnode2.y)
EDIT - I forgot to add the code that adds the nodes. So after an event comes in the node needs to be created, this method fires creating the node:
def addNode(self, scanner):
mutexnode.acquire()
try:
coord = self.realmap.convertLatLon2Pix((scanner.latitude, scanner.longitude))
self.mapNodes[scanner.sensorid] = MapNode(scanner, coord[0], coord[1], parent = self)
self.mapNodes[scanner.sensorid].move(coord[0],coord[1])
#self.mapNodes[scanner.sensorid].show()
finally:
mutexnode.release()
I would recommend you to use the QGraphicsScene and QGraphicsItem classes for your map instead of normal QWidget classes, since they are made exactly for the purpose of displaying a large number of graphical items:
QGraphicsScene http://doc.qt.io/qt-5/qgraphicsscene.html
QGraphicsItem: http://doc.qt.io/qt-5/qgraphicsitem-members.html
From the documentation:
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
The class serves as a container for QGraphicsItems. It is used together with QGraphicsView for visualizing graphical items, such as lines, rectangles, text, or even custom items, on a 2D surface. QGraphicsScene is part of the Graphics View Framework.
QGraphicsScene also provides functionality that lets you efficiently determine both the location of items, and for determining what items are visible within an arbitrary area on the scene. With the QGraphicsView widget, you can either visualize the whole scene, or zoom in and view only parts of the scene.
You can also embed widgets derived from QWidget in the scene, which should allow you to display practically any kind of information. As a bonus, you will get layering, fast transformations and ready-to-use handling of mouse interactions, which should be quite useful for realizing an interactive map.
I am just trying my first prototype in pyside (python/Qt). The application itself starts up fine, creates a window with widgets according to my layout. Threads are started and execute, all fine. Except...
I want to enhance the GUI by adding some custom widget indicating the execution state of the threads. So I thought flashing LEDs would be fine for that. For this I try to implement a custom LED widget.
Remember that I currently try to learn python, so there might be some strange approaches in this. Anyway, here is the LED widgets class in its current state:
from PySide import QtCore, QtGui
class LED(QtGui.QWidget):
class Mode:
STATIC_OFF = 0
STATIC_ON = 1
FLASH_SLOW = 2
FLASH_MEDIUM = 2
FLASH_FAST = 3
class Color:
BLACK = '#000000'
GREEN = '#00FF00'
RED = '#FF0000'
BLUE = '#0000FF'
YELLOW = '#FFFF00'
WHITE = '#FFFFFF'
mode = Mode.STATIC_ON
color = Color.BLACK
radius = 10
status = False
timer = None
outdated = QtCore.Signal()
def __init__(self, mode, color, radius, parent=None):
super(LED, self).__init__(parent)
self.outdated.connect(self.update)
self.setMode(mode,False)
self.setColor(color,False)
self.setRadius(radius,False)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.adjustAppearance)
self.adjustAppearance()
def getCenter(self):
return QtCore.QPoint(self.radius, self.radius)
def getBox(self):
return QtCore.QRect(self.radius, self.radius)
def setColor(self, color, update=True):
assert color in (self.Color.GREEN,self.Color.RED,self.Color.BLUE,self.Color.YELLOW,self.Color.WHITE), "invalid color"
self.color = color
if update:
self.adjustAppearance()
def setMode(self, mode, update=True):
assert mode in (self.Mode.STATIC_OFF,self.Mode.STATIC_ON,self.Mode.FLASH_SLOW,self.Mode.FLASH_MEDIUM,self.Mode.FLASH_FAST),"invalid mode"
self.mode = mode
if update:
self.adjustAppearance()
def setRadius(self, radius, update=True):
assert isinstance(radius, int), "invalid radius type (integer)"
assert 10<=radius<=100, "invalid radius value (0-100)"
self.radius = radius
if update:
self.adjustAppearance()
def switchOn(self):
self.status = True
self.adjustAppearance()
def switchOff(self):
self.status = False
self.adjustAppearance()
def adjustAppearance(self):
if self.mode is self.Mode.STATIC_OFF:
self.status = False
self.timer.stop()
elif self.mode is self.Mode.STATIC_ON:
self.status = True
self.timer.stop()
elif self.mode is self.Mode.FLASH_SLOW:
self.status = not self.status
self.timer.start(200)
elif self.mode is self.Mode.FLASH_SLOW:
self.status = not self.status
self.timer.start(500)
elif self.mode is self.Mode.FLASH_SLOW:
self.status = not self.status
self.timer.start(1000)
self.outdated.emit()
def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
self.drawWidget(event, painter)
painter.end()
def drawWidget(self, event, painter):
if self.status:
shade = QtGui.QColor(self.color).darker
else:
shade = QtGui.QColor(self.color).lighter
#painter.setPen(QtGui.QColor('black'), 1, QtCore.Qt.SolidLine)
painter.setPen(QtGui.QColor('black'))
painter.setBrush(QtCore.Qt.RadialGradientPattern)
painter.drawEllipse(self.getCenter(), self.radius, self.radius)
My problem is that the widget simply does not show when I add it to the windows layout. Other widgets (non-custome, plain Qt widgets) do show, so I gues it is not a question of creating the widget, not a question of how I use the widget. Nevertheless here is the (shortened) instanciation if the widget:
class View(QtGui.QMainWindow):
ui = None
def __init__(self, config, parent=None):
log.debug("window setup")
self.config = config
super(View, self).__init__(parent)
try:
self.ui = self.Ui(self)
self.setObjectName("appView")
self.setWindowTitle("AvaTalk")
self.show()
except RuntimeError as e:
log.error(e.message)
class Ui(QtCore.QObject):
# [...]
iconDetector = None
buttonDetector = None
# [...]
def __init__(self, window, parent=None):
log.debug("ui setup")
super(View.Ui, self).__init__(parent)
self.window = window
# central widget
log.debug("widget setup")
self.centralWidget = QtGui.QWidget()
self.widgetLayout = QtGui.QVBoxLayout(self.centralWidget)
# create toolbars
#self.createMenubar()
#self.createCanvas()
self.createToolbar()
#self.createStatusbar()
# visualize widget
self.window.setCentralWidget(self.centralWidget)
# actions
log.debug("actions setup")
self.actionQuit = QtGui.QAction(self.window)
self.actionQuit.setObjectName("actionQuit")
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
log.debug("connections setup")
QtCore.QObject.connect(self.actionQuit, QtCore.SIGNAL("activated()"), self.window.close)
QtCore.QMetaObject.connectSlotsByName(self.window)
def createToolbar(self):
log.debug("toolbar setup")
self.toolbar = QtGui.QHBoxLayout()
self.toolbar.setObjectName("toolbar")
self.toolbar.addStretch(1)
# camera
# detector
self.iconDetector = LED(LED.Mode.STATIC_OFF,LED.Color.GREEN,10,self.window)
self.buttonDetector = IconButton("Detector", "detector",self.window)
self.toolbar.addWidget(self.iconDetector)
self.toolbar.addWidget(self.buttonDetector)
self.toolbar.addStretch(1)
# analyzer
# extractor
# layout
self.widgetLayout.addLayout(self.toolbar)
It might well be that the actual painting using QPainter is still nonsense. I did not yet come to test that: actually when testing I find that isVisible() returns False on the widget after the setup has completed. So I assume I miss a central point. Unfortunately I am unable to find out what I miss...
Maybe someone can spot my issue? Thanks !
One thing to be careful when implementing custom widgets derived from QWidget is: sizeHint or minimumSizeHint for QWidget returns invalid QSize by default. This means, if it is added to a layout, depending on the other widgets, it will shrink to 0. This effectively makes it 'not-visible'. Although, isVisible would still return True. Widget is 'visible', but it just doesn't have anything to show (0 size). So, if you're getting False, there is definitely another issue at hand.
So it is necessary to define these two methods with sensible sizes:
class LED(QtGui.QWidget):
# ...
def sizeHint(self):
size = 2 * self.radius + 2
return QtCore.QSize(size, size)
def minimumSizeHint(self):
size = 2 * self.radius + 2
return QtCore.QSize(size, size)
Note: There are other issues:
Like defining mode, color, etc as class attributes and then overriding them with instance attributes. They won't break anything but they are pointless.
painter.setBrush(QtCore.Qt.RadialGradientPattern) is wrong. You can't create a brush with QtCore.Qt.RadialGradientPattern. It is there, so that brush.style() can return something. If you want a gradient pattern you should create a brush with QGradient constructor.
if self.mode is self.Mode.STATIC_OFF: comparing with is is wrong. is compares identity, you want == here. (also, FLASH_SLOW and FLASH_MEDIUM are both 2)
assert is for debugging and unit tests. You shouldn't use it in real code. Raise an exception if you want. Or have sensible defaults, where invalid values would be replaced with that.