- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 wxPython 用于 GUI,它一半是 OpenCV 网络摄像头视频,另一半是 wxPython 的其他组件(按钮、文本框等)。
我已经能够使用以下代码将 OpenCV 窗口放入我的 wxPython 框架中:
import wx
import cv2
vs = cv2.VideoCapture(0)
class ImagePanel(wx.Panel):
def __init__(self, parent, fps=15):
super().__init__(parent)
ret, frame = vs.read()
frame = cv2.flip(frame, 1)
height, width = frame.shape[:2]
parent.SetSize((width, height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp = wx.Bitmap.FromBuffer(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 = vs.read()
if ret:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp.CopyFromBuffer(frame)
self.Refresh()
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='User Capture')
self.vs = vs
ImagePanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()
然后我尝试创建一个主面板来容纳 OpenCV 面板和其他组件(现在是一个按钮)
import wx
import cv2
import threading
vs = cv2.VideoCapture(0)
class ImagePanel(wx.Panel):
def __init__(self, parent, fps=15):
super().__init__(parent)
ret, frame = vs.read()
frame = cv2.flip(frame, 1)
height, width = frame.shape[:2]
parent.SetSize((width, height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp = wx.Bitmap.FromBuffer(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 = vs.read()
if ret:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.bmp.CopyFromBuffer(frame)
self.Refresh()
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
main_sizer = wx.BoxSizer(wx.VERTICAL)
button_sizer = self._button_sizer(frame)
img = ImagePanel(self)
main_sizer.Add(img)
main_sizer.Add(button_sizer)
self.SetSizer(main_sizer)
self.Show()
def _button_sizer(self, frame):
snap = wx.Button(self, label='Snap')
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(snap)
button_sizer.Add((-1, -1), proportion=1)
return button_sizer
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='User Capture')
self.vs = vs
MainPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()
结果是:
我可以说我的 wxPython 实现显然不好,所以我需要一些帮助来正确地执行此操作
最佳答案
你已经很接近了。
尽量不要将 frame
用作 parent
或任何其他看起来像保留字的词。
这没有错,只是令人困惑。
这是您编辑的代码。 (注:给猫剥皮的方法有很多种)
import wx
class ImagePanel(wx.Panel):
def __init__(self, parent, fps=15):
super().__init__(parent)
self.bmp = wx.StaticBitmap(self, -1, wx.Bitmap("frame1.png"))
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
main_sizer = wx.BoxSizer(wx.VERTICAL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
img = ImagePanel(self)
snap = wx.Button(self, -1, label='Snap')
main_sizer.Add(img, 1, wx.EXPAND, 0)
button_sizer.Add(snap)
main_sizer.Add(button_sizer)
self.SetSizer(main_sizer)
snap.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self, event):
print("Button Pressed")
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='User Capture')
MainPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
app.MainLoop()
关于python - 如何使用 OpenCV 网络摄像头视频和其他 wx 组件(在同一框架/面板内)创建 wxPython 框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62678893/
我是 Python 和 WX 的新手。我创建了一个简单的测试对话框,如下所示,用组合框提示用户。我想从我的主程序中的组合框中获取值。我如何从我的主程序中调用它? 这就是我打算调用它来显示对话框但当前不
我已经在框架内创建了 wx.Grid 小部件,如果用户正在使用网格小部件,我想更改光标类型。我已经成功地使用 wx.StockCursor 和 .SetCursor 方法 做到了这一点,但是如果用户将
我想知道如何从 wx 笔记本的一页到另一页获取变量。我在想,如果我知道变量名和页面 ID,应该有某种方法可以引用变量。例如,如果我有以下代码,我将如何从面板 y 引用变量 x,反之亦然 import
我需要使用 wx.Textvalidator 验证文本框。有什么可以帮我做这个吗? 我如何将 wx.FILTER_ALPHA 与验证器一起使用,如果用户输入错误,我该如何给他们消息? 我需要在点击保存
我的印象是面板更好。真的吗? 一个比另一个有什么优势?有什么理由使用一个而不是另一个? 最佳答案 wx.Window 是所有小部件的基类(不一定是所有 windows - 这个名字有误导性)。 wx.
我有一个 wx.ListCtrl 设置了 wx.LC_REPORT 位。它有 3 列。我希望在第一列中为每个其他条目填充一个复选框。我尝试使用 ListCtrl.InsertItem 方法,但它只需要
./configure 运行时返回错误消息: wx:无法链接wx驱动程序,wx将无法使用 当我运行.\configure | grep wx,日志如下: configure: configuring
你好,我想在静态框内绘制一些彩色线条,我使用了 onpaint 方法,但线条没有出现在静态框内,然后我尝试创建一条静态线,这有效但我无法更改静态线的颜色。有没有其他方法可以使用 onpaint 方法或
wx.PyControl 可以包含 wx.Sizer 吗? 请注意,我最终在这里尝试做的事情(具有浮点值的微调器)已经在另一个问题中得到了回答。我对在 wx.PyControl 中布局小部件特别感兴趣
派生类有一个小问题,即 wx.ListItem 和 wx.ListCtrl。我成功地将 wx.ListItem 派生为 MediaItem,代码还没有完成,但你明白了: class MediaItem
我正在用 wxPython 开发一个 GUI。我在另一个代表 CD 容器对象的正方形(也有 wxPanel 类)内绘制一个代表 CD 对象的正方形。我想在 CDWindow 的右键菜单中有“删除这张
使用 wx.lib.newevent.NewEvent() 和使用 wx.NewEventType() 和 wx.PyCommandEvent() 有什么区别?下面的代码示例使用两种不同的方式来创建事
我有一个 python 应用程序,我从 here 得到的并根据我的需要进行修改,但我想将我用于图标的图像放在脚本中,我知道可能存在性能问题但目前这不是问题,我需要的只是将所有文件放在一个文件中,这样我
我有一个 wx.Frame,其中有一个主 wx.Panel,里面有几个小部件。我想要那里的一个按钮导致出现“帮助面板”。这个帮助面板可能是一个 wx.Panel,我希望它覆盖整个主 wx.Panel(
我有一个关于如何在 (style = wx.LC_REPORT) 模式下使用 wx.ListCtrl 的教程,但我只使用一列: self.InsertColumn(0, "Item") 我怎样才能:
self.sizer.Add(self.listBox, proportion=0, flag=wx.TOP | wx.LEFT, border=75) BOTTOM 和 LEFT 的边框是 75,我
我想要一个 python 图形用户界面,它可以从文件中加载不同的图像。我见过许多使用以下代码加载图像的示例: img = wx.Image("1.jpg", wx.BITMAP_TYPE_ANY, -
背景:我正在编写一个模块来创建带有多个进度条(0-5)的自定义启动屏幕。初始屏幕可以以多种模式显示(全屏无帧、带/不带帧的屏幕百分比或带/不带帧的固定大小),并且可以容纳 0-5 个链接的进度表。进度
我刚刚在我的 mac 上安装了最新稳定版的 wxWidget,并尝试编译 hello world 教程中的代码。这是导致错误的代码: #include ' #ifndef WX_PRECOMP #in
当我编译我的项目时: ---------------------------------------------------------------------- wxFlasher configur
我是一名优秀的程序员,十分优秀!