Python Forum

Full Version: how to draw simple line in wxPython?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hy
I am new in wxPython and I want a simple example to draw a simple line, and if it is possible by using functional style.

Thanks for your help
What do you have so far?

Have you tried this? https://wiki.wxpython.org/VerySimpleDrawing
Quote:
import wx

class DrawPanel(wx.Frame):

    """Draw a line to a panel."""

    def __init__(self):
        wx.Frame.__init__(self, title="Draw on Panel")
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event=None):
        dc = wx.PaintDC(self)
        dc.Clear()
        dc.SetPen(wx.Pen(wx.BLACK, 4))
        dc.DrawLine(0, 0, 50, 50)

app = wx.App(False)
frame = DrawPanel()
frame.Show()
app.MainLoop()
Before I was working with Tkinter and most of my examples was in functional style so I am not so good with classes and I don't want to build a class for only testing a widget
Luckily, that link includes another example that doesn't use a class :)
I wasn't able to reprodure the second example with wx.ClientDC Confused
I just tried it (had to install wx first, since I didn't have it), and it worked for me as written. So if it isn't working, please share your code, and the error message.
You really should get yourself comfortable with classes.
GUI programming involves so many layers that It's very difficult to do without.
So many methods rely of inheriting from other methods and classes that it is very difficult to
do all but the simplest of code without using classes.

One way to improve your understanding of this is to load the python demo program, which is very complete
and backed by source code, available with a single click of a notebook tab allowing you to flip between demo
and source code while trying to understand a concept.

To get this demo:
  • Download the source here: https://github.com/wxWidgets/Phoenix
  • Open a command window and:
  • Unzip in a directory of your choice
  • cd to Phoenix-master/Demo/
  • run the denmo
    python demo.py
You will like it!