gpt4 book ai didi

python - 在 wxpython 中使用 cairo 绘图

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:23 25 4
gpt4 key购买 nike

我对 python 相当陌生,正在尝试使用 cairo 和 wxpython 编写一个简单的程序。我习惯于将 cairo 与 gtk 和 C 一起使用,但我发现自己很困惑。

我使用以下代码为自己构建了一个简单的用户界面:

import wx

class Frame(wx.Frame):

def __init__(self, *args, **kwargs):
super(Frame, self).__init__(*args, **kwargs)
self.InitUI()

def InitUI(self):
#----------------------------------------------------
# Build menu bar and submenus
menubar = wx.MenuBar()
# file menu containing quit menu item
fileMenu = wx.Menu()
quit_item = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
fileMenu.AppendItem(quit_item)
self.Bind(wx.EVT_MENU, self.OnQuit, quit_item)
menubar.Append(fileMenu, '&File')

# help menu containing about menu item
helpMenu = wx.Menu()
about_item = wx.MenuItem(helpMenu, wx.ID_ABOUT, '&About\tCtrl+A')
helpMenu.AppendItem(about_item)
self.Bind(wx.EVT_MENU, self.OnAboutBox, about_item)
menubar.Append(helpMenu, '&Help')

self.SetMenuBar(menubar)

#----------------------------------------------------
# Build window layout

panel = wx.Panel(self)
#panel.SetBackgroundColour('yellow')
vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)

midPan = wx.Panel(panel)
#midPan.SetBackgroundColour('blue')
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)
midPan.SetSizer(hbox)

smallPan = wx.Panel(panel)
#smallPan.SetBackgroundColour('red')
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(smallPan, 0, wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT|wx.BOTTOM, 12)
smallPan.SetSizer(hbox2)

#----------------------------------------------------
# Place buttons in correct box corresponding with panel

close_button = wx.Button(smallPan, wx.ID_CLOSE)
self.Bind(wx.EVT_BUTTON, self.OnQuit, close_button)

hbox2.Add(close_button)
#----------------------------------------------------
# Set window properties

self.SetSize((1600, 1200))
self.SetTitle('PROGRAM NAME')
self.Centre()

def OnQuit(self, e):
self.Close()

def main():
ex = wx.App()
f = Frame(None)
f.Show(True)
ex.MainLoop()

if __name__ == '__main__':
main()

我希望能够在名为 midPan 的面板中绘图。如何添加 OnDraw 函数并链接信号处理程序?

非常感谢您的帮助。

最佳答案

如果您习惯了过程式编程,那么您会感到困惑是自然的。 Python 是一种 OOP 语言,并且采用 OOP 进行编码语言完全不同。我已经清理并更新了提供的例子。用作绘图区域的面板绘制三个彩色矩形。您还没有提供OnAboutBox() 方法的实现,因此,我有注释掉该行。

#!/usr/bin/python

import wx
import wx.lib.wxcairo
import cairo

class DrawingArea(wx.Panel):

def __init__ (self , *args , **kw):
super(DrawingArea , self).__init__ (*args , **kw)

self.SetDoubleBuffered(True)
self.Bind(wx.EVT_PAINT, self.OnPaint)

def OnPaint(self, e):

dc = wx.PaintDC(self)
cr = wx.lib.wxcairo.ContextFromDC(dc)
self.DoDrawing(cr)

def DoDrawing(self, cr):

cr.set_source_rgb (0.2 , 0.23 , 0.9)
cr.rectangle(10 , 15, 90, 60)
cr.fill()

cr.set_source_rgb(0.9 , 0.1 , 0.1)
cr.rectangle(130 , 15, 90, 60)
cr.fill()

cr.set_source_rgb(0.4 , 0.9 , 0.4)
cr.rectangle(250 , 15, 90, 60)
cr.fill()


class Frame(wx.Frame):

def __init__(self, *args, **kwargs):
super(Frame, self).__init__(*args, **kwargs)

self.InitUI()

def InitUI(self):
#----------------------------------------------------
# Build menu bar and submenus

menubar = wx.MenuBar()
# file menu containing quit menu item
fileMenu = wx.Menu()
quit_item = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
fileMenu.AppendItem(quit_item)
self.Bind(wx.EVT_MENU, self.OnQuit, quit_item)
menubar.Append(fileMenu, '&File')

# help menu containing about menu item
helpMenu = wx.Menu()
about_item = wx.MenuItem(helpMenu, wx.ID_ABOUT, '&About\tCtrl+A')
helpMenu.AppendItem(about_item)
#~ self.Bind(wx.EVT_MENU, self.OnAboutBox, about_item)
menubar.Append(helpMenu, '&Help')

self.SetMenuBar(menubar)

#----------------------------------------------------
# Build window layout

panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)

midPan = DrawingArea(panel)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)


smallPan = wx.Panel(panel)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(smallPan, 1, wx.EXPAND|wx.ALL, 12)
smallPan.SetSizer(hbox2)

#----------------------------------------------------
# Place buttons in correct box corresponding with panel

close_button = wx.Button(smallPan, wx.ID_CLOSE)
self.Bind(wx.EVT_BUTTON, self.OnQuit, close_button)

hbox2.Add(close_button)

#----------------------------------------------------
# Set window properties

#~ self.SetSize((1600, 1200))
self.SetSize((400, 250))
#~ self.Maximize()
self.SetTitle('PROGRAM NAME')
self.Centre()

def OnQuit(self, e):
self.Close()

def main():
ex = wx.App()
f = Frame(None)
f.Show(True)
ex.MainLoop()

if __name__ == '__main__':
main()

To do the drawing, we create a custom class that will serve as adrawing area. It inherits from a wx.Panel widget.

class DrawingArea(wx.Panel):

def __init__ (self , *args , **kw):
super(DrawingArea , self).__init__ (*args , **kw)

self.SetDoubleBuffered(True)
self.Bind(wx.EVT_PAINT, self.OnPaint)
...

这是我们用于绘图的自定义类。在构造函数中我们将绘制事件绑定(bind)到 OnPaint() 方法。

def OnPaint(self, e):

dc = wx.PaintDC(self)
cr = wx.lib.wxcairo.ContextFromDC(dc)
self.DoDrawing(cr)

OnPaint()方法中我们创建了一个开罗绘图上下文并将实际绘图代码委托(delegate)给 DoDrawing()方法。

midPan = DrawingArea(panel)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)

绘图区域已创建并添加到垂直框中。

#~ self.SetSize((1600, 1200))
self.SetSize((400, 250))
#~ self.Maximize()

最后一点:如果您想最大化显示窗口,请调用Maximize()方法。计算机屏幕有不同的尺寸。

Example screenshot on Linux

关于python - 在 wxpython 中使用 cairo 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23661347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com