I've been having some problems with a program that I've been writing and would appreciate some help or input. For some background, I'm using Python 2.7 and wxPython in order to do a streaming webcam client. The client gets the images from the server in its own thread, and puts them into a Queue. The GUI thread then gets those images from the Queue and converts them to a wxBitmap object. This happens every .5 seconds and works just great. I am able to save the wxBitmap object as a file so I know that everything is working properly.
The problem that I'm having is actually getting the wxBitmap object to show up on my GUI. The only thing I seem to be able to make the GUI do is display a gray rectangle where the web cam image should be.
Here is my onPaint() that is called when I want to refresh the screen:
def onPaint(self,e):
## this is the function that actually draws and redraws the window
## to be displayed. I think it is something similar to blit()
## in other graphical display frameworks
print "in onPaint"
## create the device context object (graphics painter)
dc = wx.PaintDC(self)
dc.BeginDrawing()
## draw the bitmap to the screen
dc.DrawBitmap(self.imageBit,0,0,True)
dc.EndDrawing()
## test code.
## the following works and updates, which means that
## everything is being converted properly and updated.
## not sure why the dc won't paint it to the window.
self.imageBit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP)
Simply put, I'm at a loss as to why its not working. from my research I've found that because I'm on a windows machine I needed the BeginDrawing() and EndDrawing() functions, so I added them. Still doesn't work. There are no errors or exceptions being thrown.
other questions that might help solve this issue:
I'm updating a wxFrame object. Maybe the wxPaintDC needs to operate in another type of container to work?
?
Actually, maybe my __init__ function is whats holding the problem. Am I setting this up properly?
class viewWindow(wx.Frame):
imgSizer = (480,360)
def __init__(self, *args, **kw):
## this is called when an instance of this class is created
super(viewWindow,self).__init__(*args,**kw)
## here is where the actual stuff inside the frame is set up.
self.pnl = wx.Panel(self)
## create a button that opens up a Connection Window
#test = wx.Button(self.pnl, label='Connection Settings')
## test.Bind(wx.EVT_BUTTON, self.openConnectionWindow)
## create the wxImage for the web cam pic
self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])
## create the wxBitmap so that the wxImage can be displayed
self.imageBit = wx.BitmapFromImage(self.image)
## create a timer that will update the window based of frame rate
self.timex = wx.Timer(self, wx.ID_OK)
self.timex.Start(500)
self.Bind(wx.EVT_TIMER, self.redraw, self.timex)
## need to do the following in order to display images in wxPython:
self.Bind(wx.EVT_PAINT, self.onPaint)
self.SetSize(self.imgSizer)
self.SetTitle('View Window')
self.Show()
Anyways, thanks in advance for your help.
EDIT: I solved the problem accidentally by deleting the line self.pnl = wx.Panel(self).
So apparently it was rendering properly, but the bitmap was underneath the panel. Maybe? I'm not really sure. I'm new to this whole wxPython thing.
That appears to be what the wxPython demo is doing too: dc.DrawBitmap. And it works on Windows! At least, that's what they do in the AlphaDrawing demo. In the DrawImage demo, they use dc.Blit(). You might try that.
However, I wonder if you couldn't do it like I did with my photo viewer. I don't use DCs to draw, but instead just use a wx.StaticBitmap that I update.
This code works. It displays the images every time and all that. It does tend to 'flicker', though. So there is probably a better way of doing this that I'm not aware of.
class viewWindow(wx.Frame):
imgSizer = (480,360)
def __init__(self, parent, title="View Window"):
super(viewWindow,self).__init__(parent)
## create the menu and its sub trees
menubar = wx.MenuBar()
filemenu = wx.Menu()
menubar.Append(filemenu, 'File')
self.fitem = filemenu.Append(wx.ID_ANY, 'Open Connection Window')
self.Bind(wx.EVT_MENU, self.openConnectionWindow, self.fitem)
self.SetMenuBar(menubar)
## here is where the actual stuff inside the frame is set up.
self.pnl = wx.Panel(self)
self.vbox = wx.BoxSizer(wx.VERTICAL)
## create the wxImage for the web cam pic
self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])
## create the wxBitmap so that the wxImage can be displayed
self.imageBit = wx.BitmapFromImage(self.image)
self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY, self.imageBit)
## add the staticBit to the sizer so it is rendered properly on resizes and such
## note: not actually needed to get the image to display, but reccommended for ease
## of layout
self.vbox.Add(self.staticBit)
## register the sizer with the panel so the panel knows to use it.
self.pnl.SetSizer(self.vbox)
## create a timer that will update the window based on frame rate
self.timex = wx.Timer(self, wx.ID_OK)
self.timex.Start(1000/framerate)
self.Bind(wx.EVT_TIMER, self.redraw, self.timex)
## set the size of the frame itself when it is first opened
self.SetSize(self.imgSizer)
self.Show()
def openConnectionWindow(self, e):
## this will open a new connection window
connect = connectionWindow(None)
def redraw(self,e):
## this function updates the frame with the latest web cam image that has been
## retrieved by the client thread from the server.
## get the newest image in the queue
if not imgQ.empty():
picz = imgQ.get()
## convert the image from a string to something usable (wxImage)
self.image.SetData(picz)
## from wxImage to wxBitmap
self.imageBit = wx.BitmapFromImage(self.image)
self.staticBit = wx.StaticBitmap(self.pnl,wx.ID_ANY, self.imageBit)
## refresh the frame
self.Refresh()
Several hours later, and researching a different question I had, I find this:
How to detect motion between two PIL images? (wxPython webcam integration example included)
That has example code in it that works beautifully.
Related
I am very new to pyqt5 and trying to figure out how to replicate the following idea.
Load an image.
Draw MOVEABLE rectangle(s) on top.
-- by Moveable, I mean rectangles that I can move via click and drag after initial placement. Like if I dropped it on the top right of picture, I can later move it to middle of the picture.
-- rectangles(s) multiple is very important. hopefully I can add/remove them dynamically too.
ideally show a text associated with the rectangle, like a, b, c... that is editable. (nice to have)
The following picture from LabelImg show cases the idea very well.
At this point in time, I have been able to load a picture and display it via QLabels and converting to PixMap. The self.ui, is the typical syntax for loading a UI from QT Designer.
class ImageLoader(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.ui = Ui_Form() # from QT Designer
self.ui.setupUi(self)
self.ui.frame_image_label.setMouseTracking(True)
self.ui.frame_image_label.setAcceptDrops(True)
#.... other stuff not quite relevant #####
# a special draggable Label (see below)
# The following does NOT work. (And I want to be able to make multiple labels on key-press...)
self.special_label = DragLabel('hii', self.ui.frame_image_label)
def go_to_image(self):
'''Load the image from a video into the base QLabel to show'''
self.current_frame_number, img = self.video_folder.get_frame(self.current_frame_number)
if img is None:
return
img = self.video_folder.resize_image(image = img)
pixmap = convertCvImage2QtImage(img)
if pixmap.isNull():
return
self.ui.frame_image_label.setPixmap(pixmap)
I have also created a custom DragLabel
class DragLabel(QLabel):
def __init__(self, button_text, parent):
super().__init__(button_text, parent)
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
pixmap = QPixmap(self.size())
self.render(pixmap)
drag.setPixmap(pixmap)
drag.exec_(Qt.MoveAction)
The current issues:
The special_label does show up in my UI. But it doesn't move when I drag it.
It is not constrained to my self.ui.frame_image_label area
In general, the reason Im not using the LabelImg, which is very nice, is because I'm trying to label a VIDEO, and am mainly use this as "correction", ie finding where the Area of Interest was incorrectly identified and then hand-correcting them. Hence the need to able to move my selections. Also, I am trying to embed "play/pause", slider selection of frames for ease of use.
Any help is appreciated.
Edit:
Musicamante has the correct idea in that we should swap to the Graphics View Framework and use views and scenes.
QLabels with pictures is not the correct idea.
Introduction and Issue
I've made a gridsizer to resize my frame itself.
But because of the gridsizer if I use WX_EXPAND flag (to let them have a new height and width when I use self.Layout() to refresh when the app is resized) they don't resize the % of the screen I gave them (I put blank widget to put all my widget where I want).
Example here
What I have tried
I've tried to make a wx.GridBagSizer but I can't understand why it always say that GenericTreeCtrl don't exist (its a must I need this tree) so I'm asking a way to do this with wx.GridSizer.
I want to work with something like that and be able to resize my widget: what I want to be resizable
Question
Can you please tell me whats the correct and optimal way to dynamically resize a widget using a wx.GridSizer?
class mainPanel(wx.Panel):
def __init__(self, parent, pageNum, FrameSize):
self.parent = parent
self.pageNum = pageNum
wx.Panel.__init__(self, parent=parent)
Sizer = wx.GridSizer(6,6,0,0)
self.PathList = []
self.PathSelected = []
self.pastePath = ""
self.SetSizer(Sizer)
#tree
widthA,heightA = FrameSize[0],FrameSize[1]
path = "/media/" + os.getlogin()
self.folder_tree_project = wx.GenericDirCtrl(self, wx.ID_ANY,path, (0,0), wx.Size(widthA*0.3,heightA*0.75),wx.FULL_REPAINT_ON_RESIZE|wx.DIRCTRL_MULTIPLE)
Sizer.Add(self.folder_tree_project,0,wx.LEFT,0)
self.t1 = self.folder_tree_project.GetTreeCtrl()
self.folder_tree_project.ShowHidden(False)
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelect,id=self.t1.GetId())
self.Bind(wx.EVT_SIZE, self.OnResize)
#--------------------------------------------------------
def OnResize(self,event):
FrameSize = self.GetSize()
self.Sizer.Layout()
Try this
def OnResize(self,event):
FrameSize = self.GetSize()
widthA,heightA = FrameSize[0],FrameSize[1]
self.folder_tree_project.SetSize((int(widthA*0.3),int(heightA*0.75)))
self.Sizer.Layout()
I will not be able to test it Leonardo but thanks for the reply ! (i did not answered because of the Global Game Jam that i was participating)
I've found another way thanks to someone on another forum, using the app 'WxGlade' I was able to understand how GridBagSizer work.
You can't do things like GenericDirCtrl in WxGlade but I've read the code given by it by putting button and blank text like I want and putting gridbagsizer, with the previsualization and the generated code I was able to understand how GridBagSizer work.
Im making a python program and in some functions it needs to hide the X and expand window buttons, how would i do it? Im using WxPython, how would I put this in?
The widgets in the window frame are defined as part of the window's style: CLOSE_BOX, MINIMIZE_BOX, and MAXIMIZE_BOX.
So, when you create the window, just leave those styles out.
If you're using a wx.Frame subclass, note that DEFAULT_FRAME_STYLE includes these values, so you will have to mask them out:
style = wx.DEFAULT_FRAME_STYLE & (~wx.CLOSE_BOX) & (~wx.MAXIMIZE_BOX)
super().__init__(whatever, args, you, use, style=style)
If you want to change them after creation, you use SetWindowStyle:
style = self.GetWindowStyle()
self.SetWindowStyle(style & (~wx.CLOSE_BOX) & (~wx.MAXIMIZE_BOX))
self.Refresh()
However, notice that the documentation of that function says:
Please note that some styles cannot be changed after the window creation and that Refresh() might need to be called after changing the others for the change to take place immediately.
And, from what I can tell, on Windows, if you create a window with a close box and then remove it later in this way, it doesn't actually go away. It does disable, which may be good enough. But if not, there's probably no way to do what you want without either reaching underneath wx to the native Windows API (which gets very tricky), or drawing the widgets on the frame manually (which gets even more tricky, especially if you care about looking right on different versions of Windows—not to mention porting to other platforms).
I wrote about Frame styles a while ago on my blog. To remove all the buttons, you could do this:
import wx
########################################################################
class NoSystemMenuFrame(wx.Frame):
"""
There is no system menu, which means the title bar is there, but
no buttons and no menu when clicking the top left hand corner
of the frame
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
no_sys_menu = wx.CAPTION
wx.Frame.__init__(self, None, title="No System Menu", style=no_sys_menu)
panel = wx.Panel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = NoSystemMenuFrame()
app.MainLoop()
I tried setting the style to wx.DEFAULT_FRAME_STYLE & (~wx.CLOSE_BOX) & (~wx.MAXIMIZE_BOX) and to wx.DEFAULT_FRAME_STYLE^(wx.CLOSE_BOX|wx.MAXIMIZE_BOX), but both of those seem to only remove the Close box. For some reason, the Maximize button is still there on my Xubuntu machine.
I am trying to make an application that displays a PDF file, using PyQt4 and python-poppler-qt4.
So far I have managed to display the entire document by loading pixmaps generated with Poppler, set on a QLabel and appended to a QFrame. The QFrame is displayed in a QScrollArea.
It looks pretty good, until implementing zooming, which is done by regenerating the pixmaps all over again, with an incremented resolution. This process requires the entire document to be rendered into pixmaps, which obviously takes time and results into an unwanted lag.
Logic wants that I should display images of the pages I am seeing only (it sounds like quantum physics). I have two options in mind:
create blank pages with QLabels and load the image onto them when they become visible in the scroll area;
create only one page and add or remove precedent or subsequent pages right before it should be displayed.
I am not sure I am on the right track or whether there is an alternative.
The first option seems more feasible, because the visibility of a blank page determines when the pixmap has to be uploaded (although I have no idea how to delete that pixmap when the page is hidden). Yet I am not sure that zooming will be faster this way, since a document of, say, 600 pages, will have to be regenerated, albeit with blank pages.
The second option should definitely improve zooming since 1 to 4 pages at a time would have to be regenerated when zooming. In that second case however, I am not sure how to trigger the construction of pages.
What would you suggest?
wouldn't it be simple to forget the QLabels and directly draw the image:
from PyQt4.QtGui import *
import sys
app = QApplication(sys.argv)
class Test(QWidget):
def __init__(self):
super(Test, self).__init__()
self.painter = QPainter()
# placeholder for the real stuff to draw
self.image = QImage("/tmp/test.jpg")
def paintEvent(self, evt):
rect = evt.rect()
evt.accept()
print rect
self.painter.begin(self)
zoomedImage = self.image # ... calculate this for your images
sourceRect = rect # ... caluclate this ...
# draw it directly
self.painter.drawImage(rect, self.image, sourceRect)
self.painter.end()
t = Test()
t.setGeometry(0,0,600,800)
s = QScrollArea()
s.setWidget(t)
s.setGeometry(0,0,300,400)
s.show()
app.exec_()
I've worked out an answer, using option 1 in the question:
def moveEvent(self, event):
self.checkVisibility()
event.ignore()
def resizeEvent(self, event):
self.checkVisibility()
event.ignore()
def checkVisibility(self):
print "Checking visibility"
for page in self.getPages():
if not page.visibleRegion().isEmpty():
if page.was_visible:
pass
else:
print page.page_number, "became visible"
page.was_visible = True
self.applyImageToPage(page)
else:
if page.was_visible:
print page.page_number, "became invisible"
page.was_visible = False
else:
pass
def applyImageToPage(self, page):
print "applying image to page", page.page_number
source = self.getSourcePage(self.getPageNumber(page))
scale = self.display.scale
# this is where the error occurs
image = source.renderToImage(72 * scale, 72 * scale)
pixmap = QtGui.QPixmap.fromImage(image)
page.setPixmap(pixmap)
OK, so iv almost completed my program for my project but I cant get a BUTTON_EVT to work which if i am honest should be the easiest thing todo. I have the buttons on my program which represent the hardware and I have created a def function for them to appear on the OGL canvas.
Problem has been solved... The code associated with the problem is found in the answer below
Edited from your last comment. Use this (using your own images):
def OnClickRouter(self, event):
image=wx.Image('cat.jpg', wx.BITMAP_TYPE_JPEG)
self.frame = bucky(None, image)
self.frame.Show()
If you call bucky() this way you must also fix the class signature:
class bucky(wx.Frame):
# Creating the outer window/frame
def __init__(self, parent, image=None):
wx.Frame.__init__(self, parent, -1,'Karls Network Tool', size=(900,700))
my_image = image if image else wx.Image("myself.bmp", wx.BITMAP_TYPE_BMP)
''''''''''''''''''''''''''''''''
# Button images
buttonOneRouter = my_image.ConvertToBitmap()
self.buttonOneRouter = wx.BitmapButton(panel, -1, buttonOneRouter, pos=(20,340))
self.buttonOneRouter.Bind(wx.EVT_BUTTON, self.OnClickRouter)
''''''''''''''''''''''''''''''''
Then you can see that after clicking the buttonOnerouter what actually you are doing is opening a new frame. The left figure is what I get when I run the program, the right one is after I click and enter again my name (I simplified a bit your code. Thats why you only see one button at the bottom instead of 4):
If you want to put my cat in the canvas instead of in the button there is still some work to do. I recommend to you to give a look at the wxPython demo. In the miscellaneous group of examples you have one called OGL that shows how to do that.
Edit: You can download the wxPython docs and demos package from here
I don't know if this is right or not but I suggest you to take this approach and see if it works or not.
Modify your frame class as:
def __init(self,parent,id,img=None)
def onClickRouter(self,event):
image=wx.Image('router.jpg', wx.BITMAP_TYPE_JPEG)
temp = image.ConvertToBitmap()
self.bmp = wx.StaticBitmap(parent=self, bitmap=temp)
self.frame=bucky(self.bmp)
Please let know the outcome.