繁体   English   中英

wxPython按钮小部件不会移动

[英]wxPython button widget won't move

我正在构建一个简单的网络摄像头程序来拍照。 现在,我正在完成GUI,即使明确放置了wxPython按钮也不会移动。 我尝试按照基本的wxPython 教程进行操作 ,但该方法无法正常工作。 可能是我的位图不在面板上吗?

这是我的代码:

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()

我仅为网络摄像头图像创建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 请参阅: 类层次结构

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM