I am making a frame with a scrollbar and some images inside. The scrollbar works fine when the frame is empty. However, when I add a picture in, the scrollbars seem to get pushed up into the top left corner of the frame. How can I implement my code so that the scrollbars stay where they are after I add pictures?
Working Code;
import wx
import wx.animate
class ScrollbarFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Scrollbar Example', pos = (100, 50), size=(1000, 1000))
self.scroll = wx.ScrolledWindow(self, -1)
self.scroll.SetScrollbars(1, 1, 1000, 1000)
#self.button = wx.Button(self.scroll, -1, "Scroll Me", pos=(50, 20))
#self.Bind(wx.EVT_BUTTON, self.OnClickTop, self.button)
#self.button2 = wx.Button(self.scroll, -1, "Scroll Back", pos=(500, 350))
#self.Bind(wx.EVT_BUTTON, self.OnClickBottom, self.button2)
self.SetBackgroundColour("gray")
imageName = "01 background.png"
gifName = "Jill.gif"
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
def update(self, imageName, gifName):
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(100, 100))
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
def OnClickTop(self, event):
self.scroll.Scroll(600, 400)
def OnClickBottom(self, event):
self.scroll.Scroll(1, 1)
app = wx.PySimpleApp()
frame = ScrollbarFrame()
frame.Show()
app.MainLoop()
if you comment out this part:
gifName = "Jill.gif"
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight()))
gifImage = wx.animate.GIFAnimationCtrl(self, 0, gifName, pos=(160, 74))
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
the window displays properly with the scrollbar. But include either (or both) of the image files, and the problem occurs.
If you want your images inside the scrolled window panel, then you have to put your static bipmap and gifImage inside it. So the parent of your images should not be self (the wx.Frame instance) but self.scroll.
Modify the 4 lines indicated:
...................
wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight())) # <- this one
gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(160, 74)) # <- this one
# clears the background
gifImage.GetPlayer().UseBackgroundColour(True)
gifImage.Play()
def update(self, imageName, gifName):
backgroundImage = wx.Image(imageName, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self.scroll, -1, backgroundImage,(10,5),(backgroundImage.GetWidth(), backgroundImage.GetHeight())) # <- this one
gifImage = wx.animate.GIFAnimationCtrl(self.scroll, 0, gifName, pos=(100, 100)) # <- this one
...................
This puts your two images one over the other. If you want to put them separately (column or row), then you should add them to a sizer inserted in your scrolled window
Related
Firstly, this is what I'm trying to make with wxPython. I would like the region labeled "Picture 1" to be able to accept a dragged image, and then when an image is dragged there, be replaced with a thumbnail of that image. I have researched "Drag and Drop" in wxPython, but I can't seem to find the tools needed for me to do this.
Any help getting me started on the right track would be greatly appreciated.
You will want the Pillow package for creating a thumbnail. The other piece you will want is most likely the wx.FileDropTarget class. I ended up doing the following:
import os
import wx
from PIL import Image
from wx.lib.pubsub import pub
PhotoMaxSize = 240
class DropTarget(wx.FileDropTarget):
def __init__(self, widget):
wx.FileDropTarget.__init__(self)
self.widget = widget
def OnDropFiles(self, x, y, filenames):
print(filenames)
image = Image.open(filenames[0])
image.thumbnail((PhotoMaxSize, PhotoMaxSize))
image.save('thumbnail.png')
pub.sendMessage('dnd', filepath='thumbnail.png')
return True
class PhotoCtrl(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
self.frame = wx.Frame(None, title='Photo Control')
self.panel = wx.Panel(self.frame)
pub.subscribe(self.update_image_on_dnd, 'dnd')
self.PhotoMaxSize = 240
self.createWidgets()
self.frame.Show()
def createWidgets(self):
instructions = 'Browse for an image'
img = wx.Image(240,240)
self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
wx.Bitmap(img))
filedroptarget = DropTarget(self)
self.imageCtrl.SetDropTarget(filedroptarget)
instructLbl = wx.StaticText(self.panel, label=instructions)
self.photoTxt = wx.TextCtrl(self.panel, size=(200,-1))
browseBtn = wx.Button(self.panel, label='Browse')
browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
0, wx.ALL|wx.EXPAND, 5)
self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
self.sizer.Add(browseBtn, 0, wx.ALL, 5)
self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)
self.panel.SetSizer(self.mainSizer)
self.mainSizer.Fit(self.frame)
self.panel.Layout()
def onBrowse(self, event):
"""
Browse for file
"""
wildcard = "JPEG files (*.jpg)|*.jpg"
dialog = wx.FileDialog(None, "Choose a file",
wildcard=wildcard,
style=wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
self.photoTxt.SetValue(dialog.GetPath())
dialog.Destroy()
self.onView()
def update_image_on_dnd(self, filepath):
self.onView(filepath=filepath)
def onView(self, filepath=None):
if not filepath:
filepath = self.photoTxt.GetValue()
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
# scale the image, preserving the aspect ratio
W = img.GetWidth()
H = img.GetHeight()
if W > H:
NewW = self.PhotoMaxSize
NewH = self.PhotoMaxSize * H / W
else:
NewH = self.PhotoMaxSize
NewW = self.PhotoMaxSize * W / H
img = img.Scale(NewW,NewH)
self.imageCtrl.SetBitmap(wx.Bitmap(img))
self.panel.Refresh()
if __name__ == '__main__':
app = PhotoCtrl()
app.MainLoop()
This worked for me on Windows 7 with wxPython 4. Note that I am dragging a file from Windows Explorer onto my wxPython application's image widget.
I made a small and simple program using wx.BoxSizer.
Here is the source code:'
import wx
# MAIN PROGRAM...
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "My Frame", size = (600, 600))
mainPanel = wx.Panel(self)
mainBox = wx.BoxSizer(wx.VERTICAL)
header1 = wx.StaticText(mainPanel, label = 'Header1:')
header2 = wx.StaticText(mainPanel, label = 'Header2:')
panel1 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)
panel2 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)
box1 = wx.BoxSizer(wx.HORIZONTAL)
box1.AddSpacer(50)
box1.Add(header1, 0, wx.ALL, 5)
box1.AddSpacer(50)
box1.Add(header2, 0, wx.ALL, 5)
box2 = wx.BoxSizer(wx.HORIZONTAL)
box2.Add(panel1, 0, wx.ALL, 5)
box2.Add(panel2, 0, wx.ALL, 5)
mainBox.Add(box1, 0, wx.ALL, 5)
mainBox.Add(box2, 0, wx.ALL, 5)
mainPanel.SetSizer(mainBox)
#self.Center()
if __name__ == '__main__':
app = wx.App()
frame = MyFrame()
frame.Show(True)
app.MainLoop()
print 'Exiting...'
The issue is that when, I add horizontal space to the left of each header, it also adds vertical space between the headers and sunken_border header1 and header2. Is there anyway to just add the horizontal space before the headers without adding the vertical space as a side effect?
Thanks.
---EDIT---
To answer your comment:
here is a picture of the program:
Simple BoxSizer program...
The 'space' in green is wanted space, but the 'space' in red is an unneeded side effect. I basically only want the green space, but I don't want the red space, I want the headers to be flush with the two panels (like right directly on top...).
Right now, I am having to do absolute positioning to get it to work, I just wanted to know if you can make it work with BoxSizer or some other layout manager...
Thanks again.
When you write
Add( ..., 0, wx.ALL, 5)
you are adding 5 pixels ALL AROUND.
So:
box1.Add(header1, 0, wx.ALL, 5)
adds 5 pizels below header 1
box2.Add(panel1, 0, wx.ALL, 5)
adds 5 pixels above panel 1
mainBox.Add(box1, 0, wx.ALL, 5)
adds 5 pixels below header 1 ( contained in box1 )
mainBox.Add(box2, 0, wx.ALL, 5)
adds 5 pixels above panel1 ( contained in box2 )
for a total of 20 extra pixels.
If you do not want white space in the vertical direction, do not write
Add( ..., 0, wx.ALL, 5)
Instead, something like this
mainPanel = wx.Panel(self)
mainBox = wx.BoxSizer(wx.VERTICAL)
header1 = wx.StaticText(mainPanel, label = 'Header1:')
header2 = wx.StaticText(mainPanel, label = 'Header2:')
panel1 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)
panel2 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)
box1 = wx.BoxSizer(wx.HORIZONTAL)
box1.AddSpacer(50)
box1.Add(header1)
box1.AddSpacer(50)
box1.Add(header2)
box2 = wx.BoxSizer(wx.HORIZONTAL)
box2.AddSpacer(5)
box2.Add(panel1)
box2.AddSpacer(10)
box2.Add(panel2)
mainBox.AddSpacer(5)
mainBox.Add(box1)
mainBox.Add(box2)
mainPanel.SetSizer(mainBox)
#self.Center()
I found the solution!
instead of this:
box1.AddSpacer(50)
do this...
box1.AddSpacer((50, 0))
It works, yay!
Thanks.
I am using a Panel within a Frame to display images (the GUI need to switch between multiple panels and hence the hierarchy). As images should be displayed in native size I used ScrolledWindow as the panel parent. The scrolls do appear and work, but it causes the Panel to collapse to minimum size and it needs to be resized using drag&drop every time.
Is there a way around this?
Below is a reduced version of the code, which shows the problem:
import os
import wx
from wx.lib.pubsub import pub
class Edit_Panel(wx.PyScrolledWindow):
def __init__(self, parent):
super(Edit_Panel, self).__init__(parent)
# Display size
width, height = wx.DisplaySize()
self.photoMaxSize = height - 500
# Loaded image
self.loaded_image = None
# Icons
self.open_icon_id = 500
# Generate panel
self.layout()
def layout(self):
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
divider = wx.StaticLine(self, -1, style = wx.LI_HORIZONTAL)
self.main_sizer.Add(divider, 0, wx.ALL | wx.EXPAND)
self.toolbar = self.init_toolbar()
self.main_sizer.Add(self.toolbar, 0, wx.ALL)
img = wx.EmptyImage(self.photoMaxSize, self.photoMaxSize)
self.image_control = wx.StaticBitmap(self, wx.ID_ANY,
wx.BitmapFromImage(img))
self.main_sizer.Add(self.image_control, 0, wx.ALL | wx.CENTER, 5)
self.image_label = wx.StaticText(self, -1, style = wx.ALIGN_CENTRE)
self.main_sizer.Add(self.image_label, 0, wx.ALL | wx.ALIGN_CENTRE, 5)
self.SetSizer(self.main_sizer)
fontsz = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT).GetPixelSize()
self.SetScrollRate(fontsz.x, fontsz.y)
self.EnableScrolling(True, True)
def init_toolbar(self):
toolbar = wx.ToolBar(self)
toolbar.SetToolBitmapSize((16, 16))
open_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (16, 16))
open_tool = toolbar.AddSimpleTool(self.open_icon_id, open_ico, "Open", "Open an Image Directory")
handler = self.on_open_reference
self.Bind(event = wx.EVT_MENU, handler = handler, source = open_tool)
toolbar.Realize()
return toolbar
def on_open_reference(self, event, wildcard = None):
if wildcard is None:
wildcard = self.get_wildcard()
defaultDir = '~/'
dbox = wx.FileDialog(self, "Choose an image to display", defaultDir = defaultDir, wildcard = wildcard, style = wx.OPEN)
if dbox.ShowModal() == wx.ID_OK:
file_name = dbox.GetPath()
# load image
self.load_image(image = file_name)
dbox.Destroy()
def get_wildcard(self):
wildcard = 'Image files (*.jpg;*.png;*.bmp)|*.png;*.bmp;*.jpg;*.jpeg'
return wildcard
def load_image(self, image):
self.loaded_image = image
# Load image
img = wx.Image(image, wx.BITMAP_TYPE_ANY)
# Label image name
image_name = os.path.basename(image)
self.image_label.SetLabel(image_name)
# scale the image, preserving the aspect ratio
scale_image = True
if scale_image:
W = img.GetWidth()
H = img.GetHeight()
if W > H:
NewW = self.photoMaxSize
NewH = self.photoMaxSize * H / W
else:
NewH = self.photoMaxSize
NewW = self.photoMaxSize * W / H
img = img.Scale(NewW, NewH)
self.image_control.SetBitmap(wx.BitmapFromImage(img))
# Render
self.main_sizer.Layout()
self.main_sizer.Fit(self)
self.Refresh()
pub.sendMessage("resize", msg = "")
class Viewer_Frame(wx.Frame):
def __init__(self, parent, id, title):
super(Viewer_Frame, self).__init__(parent = parent, id = id, title = title)
# Edit panel
self.edit_panel = Edit_Panel(self)
# Default panel
self.main_panel = self.edit_panel
# Render frame
self.render_frame()
# Subscription to re-render
pub.subscribe(self.resize_frame, ("resize"))
def render_frame(self):
# Main Sizer
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
# Add default sizer
self.main_sizer.Add(self.main_panel, 1, wx.EXPAND)
# Render
self.SetSizer(self.main_sizer)
self.Show()
self.main_sizer.Fit(self)
self.Center()
def resize_frame(self, msg):
self.main_sizer.Fit(self)
if __name__ == "__main__":
app = wx.App(False)
frame = Viewer_Frame(parent = None, id = -1, title = 'Toolkit')
app.MainLoop()
You're calling Fit(), so you're explicitly asking the panel to fit its contents, but you don't specify the min/best size of this contents anywhere (AFAICS, there is a lot of code here, so I could be missing something).
If you want to use some minimal size for the panel, just set it using SetMinSize().
I'm trying to display OpenCV with Python interface in wxPython.
I can display it but only a small region in the top left corner of the video.
What is the problem with my code?
is it the size of parent panel or something else?
note that I use code from this https://stackoverflow.com/a/14818080/2389238
and It can run perfectly but I need to display it in a panel (or other parent frame).
Thank in Advance.
import wx
import cv, cv2
class MainWindow(wx.Panel):
def __init__(self, parent,capture):
wx.Panel.__init__(self, parent)
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.inputBox = wx.TextCtrl(self)
mainSizer.Add(self.inputBox, 0, wx.ALL, 5)
# video
videoWarper = wx.StaticBox(self, label="Video",size=(640,480))
videoBoxSizer = wx.StaticBoxSizer(videoWarper, wx.VERTICAL)
videoFrame = wx.Panel(self, -1,size=(640,480))
cap = ShowCapture(videoFrame, capture)
videoBoxSizer.Add(videoFrame,0)
mainSizer.Add(videoBoxSizer,0)
parent.Centre()
self.Show()
self.SetSizerAndFit(mainSizer)
class ShowCapture(wx.Panel):
def __init__(self, parent, capture, fps=24):
wx.Panel.__init__(self, parent)
self.capture = capture
ret, frame = self.capture.read()
height, width = frame.shape[:2]
parent.SetSize((width, height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp = wx.BitmapFromBuffer(width, height, frame)
self.timer = wx.Timer(self)
self.timer.Start(1000./fps)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_TIMER, self.NextFrame)
def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
dc.DrawBitmap(self.bmp, 0, 0)
def NextFrame(self, event):
ret, frame = self.capture.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp.CopyFromBuffer(frame)
self.Refresh()
capture = cv2.VideoCapture(0)
app = wx.App(False)
frame = wx.Frame(None,-1,'HGA Count',size=(400, 400))
panel = MainWindow(frame,capture)
frame.Show()
app.MainLoop()
This is the image: http://i58.tinypic.com/2u5t7kh.png
Sorry for the link I can't put image here due to low reputation.
Thank to the answer posted by VZ
Now I understand my problem.
The small region is caused by the default size of wx.Panel that ShowCapture was inherited.
It made the dc in dc = wx.BufferedPaintDC(self) had only 20px*20px in dimension,
so the self.bmp, which is 640px * 480px, was drawn on only the small region and everything lied beyond 20px*20px is ignored.
the solution is to change the panel __init__ argument to
class ShowCapture(wx.Panel):
def __init__(self, parent, capture, fps=24):
wx.Panel.__init__(self, parent, wx.ID_ANY, (0,0), (640,480))
Note that:
I don't know whether it could be configured for auto re-size panel
videoFrame is still needed because ShowCapture need a parent frame (for layout in sizer). Don't know whether there is a better approach.
Basically, nothing sets your ShowCapture window size to the desired size, so it remains stuck at some default size. The simplest solution would probably be to do without videoFrame at all as it seems to be not needed and just create cap itself with the desired initial size, which will also become the minimal window size.
Follow up after the question update:
You can resize it automatically using the sizers and ensuring that:
The item itself grows, i.e. has positive proportion if you want it to grow in the major direction of the sizer (vertical for wxVERTICAL box sizer, horizontal for... you guessed it) or wxEXPAND flag if you want it to grow in the other direction. Or both, of course, if you want it to grow in both direction.
Very important as often forgotten: the containing sizer itself grows as well. I.e. nothing will happen if the window itself grows inside its sizer if its sizer doesn't grow inside its own containing sizer.
videoFrame is not necessary and, in fact, you ought to make your video window child of videoWarper if you're using 3.0 for the best results: since 3.0, the contents of a wxStaticBoxSizer should be created with its wxStaticBox as parent.
Here's what I came up with, it uses GenStaticBitmap, which is a subclass of wx.Panel and does the dc.DrawBitmap in its OnPaint method. I added in ROI selection with SpinCtrl and also by hovering on the right-side ROI GenStaticBitmap control. I also added in a button that when right-clicked saves both images, but when left-clicked freezes on the current images and allows the user to save either image or both. Drawing the hover and ROI selection cross-hairs was fun, hope it helps someone else!
import wx
from wx.lib import statbmp
import cv, cv2
import numpy as np
import os
import traceback
class ChooseSaveDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
panel = wx.Panel(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
btn1 = wx.Button(panel, -1, 'Save Original Image')
btn2 = wx.Button(panel, -1, 'Save ROI Image')
btn3 = wx.Button(panel, -1, 'Save Both')
sizer.Add(btn1, 0)
sizer.Add(btn2, 0)
sizer.Add(btn3, 0)
panel.SetSizer(sizer)
btn1.Bind(wx.EVT_BUTTON, self.save_orig)
btn2.Bind(wx.EVT_BUTTON, self.save_roi)
btn3.Bind(wx.EVT_BUTTON, self.save_both)
def save_orig(self, evt):
save_dialog = wx.FileDialog(self, "Save Original to JPEG", "", "", "JPG files(*.jpg)", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)#|wx.FD_CHANGE_DIR)
if save_dialog.ShowModal() == wx.ID_OK:
save_path = save_dialog.GetPath()
if save_path[:-4].lower() != '.jpg':
save_path += '.jpg'
print save_path
cv2.imwrite(save_path, self.GetParent().orig_frame)
self.EndModal(wx.OK)
def save_roi(self, evt):
save_dialog = wx.FileDialog(self, "Save ROI to JPEG", "", "", "JPG files(*.jpg)", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)#|wx.FD_CHANGE_DIR)
if save_dialog.ShowModal() == wx.ID_OK:
save_path = save_dialog.GetPath()
if save_path[:-4].lower() != '.jpg':
save_path += '.jpg'
print save_path
cv2.imwrite(save_path, self.GetParent().frameRoi)
try:
self.EndModal(wx.OK)
except wx.PyAssertionError:
pass
def save_both(self, evt):
self.save_orig(evt)
self.save_roi(evt)
class ShowCapture(wx.Frame):
def __init__(self, capture, fps=15):
wx.Frame.__init__(self, None)
panel = wx.Panel(self, -1)
#create a grid sizer with 5 pix between each cell
sizer = wx.GridBagSizer(5, 5)
self.capture = capture
ret, frame = self.capture.read()
height, width = frame.shape[:2]
self.orig_height = height
self.orig_width = width
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp = wx.BitmapFromBuffer(width, height, frame)
self.dummy_element = wx.TextCtrl(panel, -1,'')
self.dummy_element.Hide()
#create SpinCtrl widgets, these have vertical up/down buttons and a TextCtrl that increments with up/down press
self.roi_x = wx.SpinCtrl(panel, -1, "ROI X", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=width, initial=0, size=(60,-1))
self.roi_y = wx.SpinCtrl(panel, -1, "ROI Y", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=height, initial=0, size=(60,-1))
self.roi_width = wx.SpinCtrl(panel, -1, "ROI W", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=width, initial=width, size=(60,-1))
self.roi_height = wx.SpinCtrl(panel, -1, "ROI H", style=wx.TE_PROCESS_ENTER|wx.SP_ARROW_KEYS, min=0, max=height, initial=height, size=(60,-1))
save_bmp_path = os.path.join(os.path.dirname(__file__), 'icons', 'ic_action_save.png')
if os.path.isfile(save_bmp_path):
save_bmp = wx.Image(save_bmp_path).ConvertToBitmap()
save_button = wx.BitmapButton(panel,wx.ID_ANY, bitmap=save_bmp, style = wx.NO_BORDER, size=(32,32)) # )
else:
save_button = wx.Button(panel, -1, 'Save')
settings_bmp_path = os.path.join(os.path.dirname(__file__), 'icons', 'ic_action_settings.png')
if os.path.isfile(settings_bmp_path):
settings_bmp = wx.Image(settings_bmp_path).ConvertToBitmap()
settings_button = wx.BitmapButton(panel,wx.ID_ANY, bitmap=settings_bmp, style = wx.NO_BORDER, size=(32,32)) # )
else:
settings_button = wx.Button(panel, -1, 'Settings')
#create image display widgets
self.ImgControl = statbmp.GenStaticBitmap(panel, wx.ID_ANY, self.bmp)
self.ImgControl2 = statbmp.GenStaticBitmap(panel, wx.ID_ANY, self.bmp)
#add text to the sizer grid
sizer.Add(wx.StaticText(panel, -1, 'ROI'), (0, 0), (1,2), wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'X'), (1, 0), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'Y'), (2, 0), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'width,'), (1, 2), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'height'), (2, 2), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(wx.StaticText(panel, -1, 'Right-click image to reset ROI'), (2, 4), wx.DefaultSpan, wx.ALL, 5)
tool_button_sizer = wx.BoxSizer(wx.HORIZONTAL)
tool_button_sizer.Add(save_button, 0)
tool_button_sizer.Add(settings_button, 0)
#sizer.Add(, (0, 6), wx.DefaultSpan, wx.ALIGN_RIGHT)#, wx.ALL, 5)
sizer.Add(tool_button_sizer, (0, 7), wx.DefaultSpan, wx.ALIGN_RIGHT)#, wx.ALL, 5)
#add SpinCtrl widgets to the sizer grid
sizer.Add(self.roi_x, (1, 1), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(self.roi_y, (2, 1), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(self.roi_width, (1, 3), wx.DefaultSpan, wx.ALL, 5)
sizer.Add(self.roi_height, (2, 3), wx.DefaultSpan, wx.ALL, 5)
#add image widgets to the sizer grid
sizer.Add(self.ImgControl, (3, 0), (1,4), wx.EXPAND|wx.CENTER|wx.LEFT|wx.BOTTOM, 5)
sizer.Add(self.ImgControl2, (3, 4), (1,4), wx.EXPAND|wx.CENTER|wx.RIGHT|wx.BOTTOM, 5)
#set the sizer and tell the Frame about the best size
panel.SetSizer(sizer)
sizer.SetSizeHints(self)
panel.Layout()
panel.SetFocus()
#start a timer that's handler grabs a new frame and updates the image widgets
self.timer = wx.Timer(self)
self.fps = fps
self.timer.Start(1000./self.fps)
#bind timer events to the handler
self.Bind(wx.EVT_TIMER, self.NextFrame)
#bind events to ROI image widget
self.ImgControl2.Bind(wx.EVT_LEFT_DOWN, self.On_ROI_Click)
self.ImgControl2.Bind(wx.EVT_LEFT_UP, self.On_ROI_ClickRelease)
self.ImgControl2.Bind(wx.EVT_RIGHT_DOWN, self.On_ROI_RightClick)
self.ImgControl2.Bind(wx.EVT_MOTION, self.On_ROI_Hover)
self.ImgControl2.Bind(wx.EVT_ENTER_WINDOW, self.On_ROI_mouse_enter)
self.ImgControl2.Bind(wx.EVT_LEAVE_WINDOW, self.On_ROI_mouse_leave)
#bind save button
save_button.Bind(wx.EVT_BUTTON, self.on_save_click)
save_button.Bind(wx.EVT_RIGHT_DOWN, self.on_quick_save)
#bind settings button
settings_button.Bind(wx.EVT_BUTTON, self.on_settings_click)
settings_button.Bind(wx.EVT_LEFT_UP, self.on_settings_click_release)
def on_settings_click(self, evt):
pass
def on_settings_click_release(self, evt):
self.dummy_element.SetFocus()
def on_save_click(self, evt):
modal = ChooseSaveDialog(self)
self.timer.Stop()
modal.ShowModal()
modal.Destroy()
self.timer.Start(1000./self.fps)
def on_quick_save(self, evt):
cv2.imwrite('orig_frame.jpg', self.orig_frame)
cv2.imwrite('frameRoi.jpg', self.frameRoi)
def On_ROI_RightClick(self, evt):
self.roi_x.SetValue(0)
self.roi_y.SetValue(0)
self.roi_width.SetValue(self.orig_width)
self.roi_height.SetValue(self.orig_height)
def On_ROI_Hover(self, evt):
self.ROI_crosshair_pos = evt.GetPosition()
def On_ROI_mouse_enter(self, evt):
self.enable_crosshairs = True
def On_ROI_mouse_leave(self, evt):
try:
self.enable_crosshairs = False
if hasattr(self, 'roi_click_down_pos'):
self.update_spinners(evt.GetPosition())
del self.roi_click_down_pos
except AttributeError:
pass
def On_ROI_Click(self, evt):
self.roi_click_down_pos = evt.GetPosition()
def On_ROI_ClickRelease(self, evt):
roi_click_up_pos = evt.GetPosition()
#if roi_click_up_pos[0] >= 0 and roi_click_up_pos[1] >= 0:
if hasattr(self, 'roi_click_down_pos'):
self.update_spinners(roi_click_up_pos)
try:
del self.roi_click_down_pos
except AttributeError:
pass
def update_spinners(self, new_pos):
self.roi_width.SetValue(abs(new_pos[0] - self.roi_click_down_pos[0]))
self.roi_height.SetValue(abs(new_pos[1] - self.roi_click_down_pos[1]))
self.roi_x.SetValue(min(self.roi_click_down_pos[0], new_pos[0]))
self.roi_y.SetValue(min(self.roi_click_down_pos[1], new_pos[1]))
def NextFrame(self, event):
ret, self.orig_frame = self.capture.read()
if ret:
frame = cv2.cvtColor(self.orig_frame, cv2.COLOR_BGR2RGB)
self.bmp.CopyFromBuffer(frame)
self.ImgControl.SetBitmap(self.bmp)
try:
orig_height, orig_width = frame.shape[:2]
y1 = self.roi_y.GetValue()
y2 = y1 + self.roi_height.GetValue()
y2 = min(y2, orig_height)
x1 = self.roi_x.GetValue()
x2 = x1 + self.roi_width.GetValue()
x2 = min(x2, orig_width)
frameRoi = self.orig_frame[y1:y2, x1:x2]
roi_width = x2-x1
roi_height = y2-y1
#frameRoi = cv2.cvtColor(frameRoi, cv2.COLOR_BGR2GRAY )
#print len(frameRoi)
#frameRoi -= 110#frameRoi #* 1.25
#print len(frameRoi)
#frameRoi = frameRoi.clip(255.)
#frameRoi = (255./1)*((frameRoi/(255./1))**0.5)# 0.5
if hasattr(self, 'ROI_crosshair_pos') and self.enable_crosshairs:
try:
cross_x = self.ROI_crosshair_pos[0]
cross_y = self.ROI_crosshair_pos[1]
frameRoi[0:roi_height, cross_x:cross_x+1] = [42,0,255]
frameRoi[cross_y:cross_y+1, 0:roi_width] = [42,0,255]
if hasattr(self, 'roi_click_down_pos'):
roi_x1 = self.roi_click_down_pos[0]
roi_y1 = self.roi_click_down_pos[1]
if cross_y>roi_y1:
frameRoi[0:roi_y1, 0:roi_width] = frameRoi[0:roi_y1, 0:roi_width]*.50
frameRoi[cross_y:roi_height, 0:roi_width] = frameRoi[cross_y:roi_height, 0:roi_width]*.50
else:
frameRoi[roi_y1:roi_height, 0:roi_width] = frameRoi[roi_y1:roi_height, 0:roi_width]*.50
frameRoi[0:cross_y, 0:roi_width] = frameRoi[0:cross_y, 0:roi_width]*.50
if cross_x>roi_x1:
frameRoi[0:roi_height, 0:roi_x1] = frameRoi[0:roi_height, 0:roi_x1]*.50
frameRoi[0:roi_height, cross_x:roi_width] = frameRoi[0:roi_height, cross_x:roi_width]*.50
else:
frameRoi[0:roi_height, roi_x1:roi_width] = frameRoi[0:roi_height, roi_x1:roi_width]*.50
frameRoi[0:roi_height, 0:cross_x] = frameRoi[0:roi_height, 0:cross_x]*.50
except:
print 'couldn\'t draw crosshairs'
traceback.print_exc()
self.frameRoi = np.array(frameRoi , dtype = np.uint8)
#frameRoi = np.repeat(frameRoi, 3)
#frameRoi = array(newImage0,dtype=uint8)
frameRoi = cv2.cvtColor(self.frameRoi, cv2.COLOR_BGR2RGB)#GRAY2RGB)
self.bmp2 = wx.BitmapFromBuffer(roi_width, roi_height, frameRoi)
self.ImgControl2.SetBitmap(self.bmp2)
except:
traceback.print_exc()
capture = cv2.VideoCapture(0)
#capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, 320)
#capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
app = wx.App()
frame = ShowCapture( capture)
frame.Show()
app.MainLoop()
I have a panel to control editing of a matplotlib graph in a wxPython frame. The wxPython install was recently updated to 2.8.12.1 from 2.6.4.0 and it broke a few things, namely, the scroll panel no longer fills a block but instead stays at a minimum size. I'm just now picking this up from a year ago so I'm a bit rusty. Any help would be much appreciated!
Below is a stripped-down version of the code that can be run on its own and displays the problem. The ScrolledWindow should expand up to 400px. When I run it self.scroll.GetSize() returns (292, 257) but it clearly is not displaying at that size.
# testing scroll panel for PlotEditFrame
import wx
# spoof the necessary matplotlib objects
class FakePlot:
def __init__(self):
self.figure = FakeFigure()
def get_figure(self):
return self.figure
class FakeFigure:
def __init__(self):
self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
def __init__(self):
self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
def __init__(self,i):
self.label = "line #%s"%i
def get_label(self):
return self.label
class PlotEditFrame(wx.Frame):
"""
This class holds the frame for plot editing tools
"""
def __init__(self, parent, plot):
"""Constructor for PlotEditFrame"""
wx.Frame.__init__(self, parent, -1, "Edit Plot")
self.parent = parent
self.plot = plot
self.figure = plot.get_figure()
self.advanced_options = None
self.scroll = wx.ScrolledWindow(self, -1)
self.InitControls()
def InitControls(self):
"""Create labels and controls based on the figure's attributes"""
# Get current axes labels
self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
wx.StaticText(self.scroll, -1, "Color:"),
wx.StaticText(self.scroll, -1, ""))]
for axis in self.figure.axes:
for line in axis.lines:
color = wx.Colour(255,0,0,0)
lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
lineBtn = wx.Button(self.scroll, -1, size=(25,25))
lineBtn.SetBackgroundColour(color)
self.lineCtrls.append((lineTxt, lineColor, lineBtn))
# Place controls
boxSizer = wx.BoxSizer(wx.VERTICAL)
lineBox = wx.StaticBox(self, -1, "Lines")
lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
for ctrls in self.lineCtrls:
lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
(ctrls[1], 0, wx.ALIGN_LEFT),
(ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
((3,3), 0, wx.ALIGN_CENTER)])
lineSizer.AddGrowableCol(0)
# Set size
self.scroll.SetSizer(lineSizer)
width = self.scroll.GetBestSize().width
height = self.scroll.GetBestSize().height
if height > 400:
height = 400
width = width + 25 # button size
self.scroll.SetSize((width, height))
self.scroll.SetScrollbars(0, 1, 1,1)
print "set scrollbars at %s x %s"%(width, height)
lineBoxSizer.Add(self.scroll, 0, wx.EXPAND)
boxSizer.AddMany([ (lineBoxSizer, 0, wx.EXPAND) ])
self.SetSizer(boxSizer)
self.SetAutoLayout(1)
self.Fit()
height = self.GetSize().GetHeight()
self.SetSizeHints(minH=height, maxH=height,
minW=width, maxW=width*5)
if __name__ == '__main__':
app = wx.PySimpleApp(0)
parent = wx.Frame(None, wx.ID_ANY, 'test', size=(300,300))
plot = FakePlot()
panel = PlotEditFrame(parent, plot)
panel.Show()
app.MainLoop()
I can't figure out what panel needs resized. Some things I've tried, to no avail:
# These have no visible effect
boxSizer.SetMinSize((width, height))
self.scroll.SetVirtualSize((width, height))
lineBoxSizer.Fit(self.scroll)
lineBoxSizer.SetVirtualSizeHints(self.scroll)
# This makes the window the right size, but not the scroll panel
lineBoxSizer.SetMinSize((width, height))
I edited your code a bit to get it to work:
import wx
# spoof the necessary matplotlib objects
class FakePlot:
def __init__(self):
self.figure = FakeFigure()
def get_figure(self):
return self.figure
class FakeFigure:
def __init__(self):
self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
def __init__(self):
self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
def __init__(self,i):
self.label = "line #%s"%i
def get_label(self):
return self.label
class PlotEditFrame(wx.Frame):
"""
This class holds the frame for plot editing tools
"""
def __init__(self, parent, plot, size):
"""Constructor for PlotEditFrame"""
wx.Frame.__init__(self, parent, -1, "Edit Plot", size=size)
self.parent = parent
self.plot = plot
self.figure = plot.get_figure()
self.advanced_options = None
self.scroll = wx.ScrolledWindow(self, -1)
self.InitControls()
def InitControls(self):
"""Create labels and controls based on the figure's attributes"""
# Get current axes labels
self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
wx.StaticText(self.scroll, -1, "Color:"),
wx.StaticText(self.scroll, -1, ""))]
for axis in self.figure.axes:
for line in axis.lines:
color = wx.Colour(255,0,0,0)
lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
lineBtn = wx.Button(self.scroll, -1, size=(25,25))
lineBtn.SetBackgroundColour(color)
self.lineCtrls.append((lineTxt, lineColor, lineBtn))
# Place controls
boxSizer = wx.BoxSizer(wx.VERTICAL)
lineBox = wx.StaticBox(self, -1, "Lines")
lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
for ctrls in self.lineCtrls:
lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
(ctrls[1], 0, wx.ALIGN_LEFT),
(ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
((3,3), 0, wx.ALIGN_CENTER)])
lineSizer.AddGrowableCol(0)
# Set size
self.scroll.SetSizer(lineSizer)
width = self.scroll.GetBestSize().width
height = self.scroll.GetBestSize().height
if height > 400:
height = 400
width = width + 25 # button size
self.scroll.SetSize((width, height))
self.scroll.SetScrollbars(0, 1, 1,1)
print "set scrollbars at %s x %s"%(width, height)
lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)
boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)
self.SetSizer(boxSizer)
self.SetAutoLayout(1)
#self.Fit()
height = self.GetSize().GetHeight()
self.SetSizeHints(minH=height, maxH=height,
minW=width, maxW=width*5)
if __name__ == '__main__':
app = wx.App(False)
plot = FakePlot()
frame = PlotEditFrame(None, plot, size=(300,300))
frame.Show()
app.MainLoop()
The main thing was to set the proportion to "1" on the following two lines:
lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)
boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)
I changed the way you start the program as it's a little silly to put a frame inside another frame for this case. Also PySimpleApp is deprecated, so I changed that too. I have almost never found a good use for the "Fit()" method, so I took that out as it was squashing the initial GUI too much.
Hope that helps!