【问题标题】:Drawing a line in wxPython using mouse position使用鼠标位置在 wxPython 中绘制一条线
【发布时间】:2018-01-23 22:38:01
【问题描述】:

我正在尝试在 wxPython 中编写程序,它会在我单击窗口的位置画一条线,但它不起作用,我实际上不知道为什么。我怎么能写这个,它会起作用?

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()

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    打电话

    self.Refresh()
    

    在 OnLeftDown/OnRightDown 的末尾。否则系统不知道它应该重绘所以它不会重绘...

    (另外,一般来说,将 coord 作为成员变量而不是全局变量可能会更好)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      相关资源
      最近更新 更多