wxPython一个网络拓扑工具 - 需要一些帮助 - 请看看:)

时间:2012-03-25 18:17:10

标签: python wxpython wxwidgets

提前感谢回复....我已经开始创建一个网络拓扑程序,其工作方式与cisco数据包跟踪器或GNS3相同...我使用Python进行了许多项目但是使用了wxPython在了解使用哪些功能时,首次证明库是一个巨大的手段。

用户有各种不同的按钮,例如路由器按钮,开关,主机和服务器按钮等,当他们点击此按钮时,我希望按钮上的图像出现在可编辑区域中。 (类似于绘画的东西)然后用户可以使用wxPython拖放功能在此区域内移动它。我无法理解这个领域的用途....我试着在谷歌搜索'wxPython如何创建一个油漆程序'等但仍然没有运气。

请有人建议他们如何制作这个节目?

卡尔

1 个答案:

答案 0 :(得分:1)

以下是wxpython(由Robin Dunn执行的wxpython)书中的摘录,该书介绍了如何使用油漆。希望这会有所帮助:

    import wx
    class SketchWindow(wx.Window):
    def __init__(self, parent, ID):
    wx.Window.__init__(self, parent, ID)
    self.SetBackgroundColour("White")
    self.color = "Black"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
    self.lines = []
    self.curLine = []
    self.pos = (0, 0)
    self.InitBuffer()
    self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
    self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)

    self.Bind(wx.EVT_MOTION, self.OnMotion) self.Bind(wx.EVT_SIZE,
    self.OnSize) self.Bind(wx.EVT_IDLE, self.OnIdle)
    self.Bind(wx.EVT_PAINT, self.OnPaint)

    def InitBuffer(self):
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    dc = wx.BufferedDC(None, self.buffer)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    self.DrawLines(dc)
    self.reInitBuffer = False

    def GetLinesData(self):
     return self.lines[:]
   def SetLinesData(self, lines):
     self.lines = lines[:]
     self.InitBuffer()
     self.Refresh()
   def OnLeftDown(self, event):
    self.curLine = []
    self.pos = event.GetPositionTuple()
    self.CaptureMouse()
   def OnLeftUp(self, event):
    if self.HasCapture():
     self.lines.append((self.color,
    self.thickness,
   self.curLine))
   self.curLine = []
    self.ReleaseMouse()
   def OnMotion(self, event):
     if event.Dragging() and event.LeftIsDown():
    dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
    self.drawMotion(dc, event)
    event.Skip()
   def drawMotion(self, dc, event):
   dc.SetPen(self.pen)
   newPos = event.GetPositionTuple()
   coords = self.pos + newPos
   self.curLine.append(coords)
    dc.DrawLine(*coords)
   self.pos = newPos

    def OnSize(self, event):
    self.reInitBuffer = True
      def OnIdle(self, event):
       if self.reInitBuffer:
     self.InitBuffer()
      self.Refresh(False)
    def OnPaint(self, event):
     dc = wx.BufferedPaintDC(self, self.buffer)
    def DrawLines(self, dc):
   for colour, thickness, line in self.lines:
    pen = wx.Pen(colour, thickness, wx.SOLID)
    dc.SetPen(pen)
    for coords in line:
    dc.DrawLine(*coords)
    def SetColor(self, color):
    self.color = color
     self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
    def SetThickness(self, num):
    self.thickness = num
    self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
   class SketchFrame(wx.Frame):
    def __init__(self, parent):
    wx.Frame.__init__(self, parent, -1, "Sketch Frame",
     size=(800,600))
    self.sketch = SketchWindow(self, -1)
      if __name__ == '__main__':
      app = wx.PySimpleApp()
     frame = SketchFrame(None)
      frame.Show(True)
      app.MainLoop()