wxPython按钮小部件不会移动

时间:2014-07-21 01:49:20

标签: python user-interface wxpython wxwidgets

我正在构建一个简单的网络摄像头程序来拍照。现在,我正在完成GUI,即使它被明确放置,我的wxPython按钮也不会移动。我已经尝试过遵循基本的wxPython tutorial并且它不起作用。难道我的位图不在面板上吗?

这是我的代码:

import wx
import cv2

class webcamWindow(wx.Panel):

    def __init__(self, parent, camera, fps=10):
        wx.Panel.__init__(self, parent)
        panel = wx.Panel(self, -1)

        wx.Button(panel, -1, "Capture", (210,660))

        self.camera = camera
        ret_value, frame = self.camera.read()
        height, width = frame.shape[:2]
        parent.SetSize((width, (height+75)))

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.flip(frame, 1)
        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, e):
        dc = wx.BufferedPaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0)
    def NextFrame(self, e):
        ret_value, frame = self.camera.read()
        if ret_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = cv2.flip(frame, 1)
            self.bmp.CopyFromBuffer(frame)
            self.Refresh()

camera = cv2.VideoCapture(0)

app = wx.App()
frame = wx.Frame(None, -1, "Webcam")
cap = webcamWindow(frame, camera)
frame.Show()
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

我仅为网络摄像头图片创建Panel,然后我在Button

的框架中使用它
import wx
import cv2

#---------------------------------------------------------------------- 

class webcamPanel(wx.Panel):

    def __init__(self, parent, camera, fps=10):
        wx.Panel.__init__(self, parent)

        self.camera = camera

        ret_value, frame = self.camera.read()
        height, width = frame.shape[:2]

        # resize panel with camera image
        self.SetSize( (width, height) )

        # resize main window
        self.GetParent().GetParent().SetSize( (width, height+75) )

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.flip(frame, 1)
        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, e):
        dc = wx.BufferedPaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0)

    def NextFrame(self, e):
        ret_value, frame = self.camera.read()
        if ret_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = cv2.flip(frame, 1)
            self.bmp.CopyFromBuffer(frame)
            self.Refresh()

#---------------------------------------------------------------------- 

class webcamWindow(wx.Frame):

    def __init__(self, camera, fps=10):
        wx.Frame.__init__(self, None)

        self.panel = wx.Panel(self, -1)

        # add panel with webcam image
        self.webcampanel = webcamPanel(self.panel, camera)

        # get size of webcam panel
        webcampanelsize = self.webcampanel.GetSize()

        # put button below webcam panel
        self.but = wx.Button(self.panel, label="Capture", pos=(0, webcampanelsize.height), size=(webcampanelsize.width,75))

#----------------------------------------------------------------------

camera = cv2.VideoCapture(0)

app = wx.App()
cap = webcamWindow(camera)
cap.Show()
app.MainLoop()

如果您在Frame中使用某个布局管理器,则无需计算Button位置。

代替wx.Panel,您可以使用wx.Windowwx.Control。请参阅:Class hierarchy