Best Canvas for WxPython

我只是一个虾纸丫 提交于 2020-01-23 08:02:16

问题


I am a Java programmer working on a project in Python and the latest version of WxPython. In Java Swing, you can draw on JPanel elements by overriding their paint method.

I am looking for a similar class in WxPython for a GUI application.

I saw this question here:

Best canvas for drawing in wxPython?

but the fact that the projects are not being updated has me worried.

Three years later, is there anything else I should look into other than FloatCanvas or OGL?

The end use case i drawing a sound waves at varying degrees of zoom.


回答1:


Just use a wx.Panel.

Here is some documentation on the drawing context functions:

http://docs.wxwidgets.org/stable/wx_wxdc.html

http://www.wxpython.org/docs/api/wx.DC-class.html

import wx

class View(wx.Panel):
    def __init__(self, parent):
        super(View, self).__init__(parent)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.Bind(wx.EVT_PAINT, self.on_paint)
    def on_size(self, event):
        event.Skip()
        self.Refresh()
    def on_paint(self, event):
        w, h = self.GetClientSize()
        dc = wx.AutoBufferedPaintDC(self)
        dc.Clear()
        dc.DrawLine(0, 0, w, h)
        dc.SetPen(wx.Pen(wx.BLACK, 5))
        dc.DrawCircle(w / 2, h / 2, 100)

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.SetTitle('My Title')
        self.SetClientSize((500, 500))
        self.Center()
        self.view = View(self)

def main():
    app = wx.App(False)
    frame = Frame()
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()



来源:https://stackoverflow.com/questions/16597110/best-canvas-for-wxpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!