I'm trying to make program in wxPython, that will draw a line in a position in which I clicked on a window but it doesn't work and I actually don't know why. How could I write this, that it will work?
import wx
global coord
coord = (30, 30)
class MyFrame(wx.Frame):
"""create a color frame, inherits from wx.Frame"""
global coord
def __init__(self, parent):
# -1 is the default ID
wx.Frame.__init__(self, parent, -1, "Click for mouse position", size=(400,300),
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.SetBackgroundColour('Goldenrod')
self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
# hook some mouse events
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnLeftDown(self, event):
global coord
"""left mouse button is pressed"""
pt = event.GetPosition() # position tuple
print pt
coord = pt
self.SetTitle('LeftMouse = ' + str(pt))
def OnRightDown(self, event):
global coord
"""right mouse button is pressed"""
pt = event.GetPosition()
coord = pt
print pt
self.SetTitle('RightMouse = ' + str(pt))
def OnPaint(self, event):
global coord
dc = wx.PaintDC(self)
dc.Clear()
dc.SetPen(wx.Pen(wx.BLACK, 4))
dc.DrawLine(0, 0, int(str(coord[0])), int(str(coord[1])))
app = wx.PySimpleApp()
frame = MyFrame(None)
frame.Show(True)
app.MainLoop()
Call
self.Refresh()
at the end of OnLeftDown/OnRightDown. Otherwise the system does not know it should redraw so it won't redraw...
(also, generally speaking, it may be better to have coord as a member variable, not a global)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.