⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floatcanvas.py

📁 用wxPython编写GUI程序的样例代码
💻 PY
📖 第 1 页 / 共 5 页
字号:
#!/usr/bin/env python

try:
    import numpy as N
    import numpy.random as RandomArray
    haveNumpy = True
    #print "Using numpy, version:", N.__version__
except ImportError:
            # numpy isn't there
            haveNumpy = False
            errorText = (
            "The FloatCanvas requires the numpy module, version 1.* \n\n"
            "You can get info about it at:\n"
            "http://numpy.scipy.org/\n\n"
            )
      
#---------------------------------------------------------------------------


def BuildDrawFrame(): # this gets called when needed, rather than on import
    try:
        from floatcanvas import NavCanvas, FloatCanvas, Resources
    except ImportError: # if it's not there locally, try the wxPython lib.
        from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
    import wx.lib.colourdb
    import time, random

    class DrawFrame(wx.Frame):

        """
        A frame used for the FloatCanvas Demo

        """


        def __init__(self,parent, id,title,position,size):
            wx.Frame.__init__(self,parent, id,title,position, size)

            ## Set up the MenuBar
            MenuBar = wx.MenuBar()

            file_menu = wx.Menu()
            item = file_menu.Append(-1, "&Close","Close this frame")
            self.Bind(wx.EVT_MENU, self.OnQuit, item)

            item = file_menu.Append(-1, "&SavePNG","Save the current image as a PNG")
            self.Bind(wx.EVT_MENU, self.OnSavePNG, item)
            MenuBar.Append(file_menu, "&File")

            draw_menu = wx.Menu()

            item = draw_menu.Append(-1, "&Clear","Clear the Canvas")
            self.Bind(wx.EVT_MENU, self.Clear, item)

            item = draw_menu.Append(-1, "&Draw Test","Run a test of drawing random components")
            self.Bind(wx.EVT_MENU, self.DrawTest, item)

            item = draw_menu.Append(-1, "&Line Test","Run a test of drawing random lines")
            self.Bind(wx.EVT_MENU, self.LineTest, item)

            item = draw_menu.Append(-1, "Draw &Map","Run a test of drawing a map")
            self.Bind(wx.EVT_MENU, self.DrawMap, item)

            item = draw_menu.Append(-1, "&Text Test","Run a test of text drawing")
            self.Bind(wx.EVT_MENU, self.TestText, item)

            item = draw_menu.Append(-1, "&ScaledText Test","Run a test of text drawing")
            self.Bind(wx.EVT_MENU, self.TestScaledText, item)

            item = draw_menu.Append(-1, "&ScaledTextBox Test","Run a test of the Scaled Text Box")
            self.Bind(wx.EVT_MENU, self.TestScaledTextBox, item)

            item = draw_menu.Append(-1, "&Bitmap Test","Run a test of the Bitmap Object")
            self.Bind(wx.EVT_MENU, self.TestBitmap, item)

            item = draw_menu.Append(-1, "&Hit Test","Run a test of the hit test code")
            self.Bind(wx.EVT_MENU, self.TestHitTest, item)

            item = draw_menu.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
            self.Bind(wx.EVT_MENU, self.TestHitTestForeground, item)

            item = draw_menu.Append(-1, "&Animation","Run a test of Animation")
            self.Bind(wx.EVT_MENU, self.TestAnimation, item)

            #item = draw_menu.Append(-1, "&Speed","Run a test of Drawing Speed")
            #self.Bind(wx.EVT_MENU, self.SpeedTest, item)

            item = draw_menu.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
            self.Bind(wx.EVT_MENU, self.PropertiesChangeTest, item)

            item = draw_menu.Append(-1, "&Arrows","Run a test of Arrows")
            self.Bind(wx.EVT_MENU, self.ArrowTest, item)

            item = draw_menu.Append(-1, "&ArrowLine Test","Run a test of drawing Arrow Lines")
            self.Bind(wx.EVT_MENU, self.ArrowLineTest, item)

            item = draw_menu.Append(-1, "&Hide","Run a test of hiding and showing objects")
            self.Bind(wx.EVT_MENU, self.HideTest, item)

            MenuBar.Append(draw_menu, "&Tests")

            view_menu = wx.Menu()
            item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
            self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
            MenuBar.Append(view_menu, "&View")

            help_menu = wx.Menu()
            item = help_menu.Append(-1, "&About",
                                    "More information About this program")
            self.Bind(wx.EVT_MENU, self.OnAbout, item)
            MenuBar.Append(help_menu, "&Help")

            self.SetMenuBar(MenuBar)

            self.CreateStatusBar()

            
            # Add the Canvas
            NC = NavCanvas.NavCanvas(self,
                                     Debug = 0,
                                     BackgroundColor = "DARK SLATE BLUE")

            self.Canvas = NC.Canvas # reference the contained FloatCanvas

            self.MsgWindow = wx.TextCtrl(self, wx.ID_ANY,
                                         "Look Here for output from events\n",
                                         style = (wx.TE_MULTILINE |
                                                  wx.TE_READONLY |
                                                  wx.SUNKEN_BORDER)
                                         )
            
            ##Create a sizer to manage the Canvas and message window
            MainSizer = wx.BoxSizer(wx.VERTICAL)
            MainSizer.Add(NC, 4, wx.EXPAND)
            MainSizer.Add(self.MsgWindow, 1, wx.EXPAND | wx.ALL, 5)

            self.SetSizer(MainSizer)
            self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

            self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove) 
            self.Canvas.Bind(FloatCanvas.EVT_MOUSEWHEEL, self.OnWheel) 

            self.EventsAreBound = False

            ## getting all the colors for random objects
            wx.lib.colourdb.updateColourDB()
            self.colors = wx.lib.colourdb.getColourList()


            return None

        def Log(self, text):
            self.MsgWindow.AppendText(text)
            if not text[-1] == "\n":
                self.MsgWindow.AppendText("\n")
            

        def BindAllMouseEvents(self):
            if not self.EventsAreBound:
                ## Here is how you catch FloatCanvas mouse events
                self.Canvas.Bind(FloatCanvas.EVT_LEFT_DOWN, self.OnLeftDown) 
                self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp)
                self.Canvas.Bind(FloatCanvas.EVT_LEFT_DCLICK, self.OnLeftDouble) 

                self.Canvas.Bind(FloatCanvas.EVT_MIDDLE_DOWN, self.OnMiddleDown) 
                self.Canvas.Bind(FloatCanvas.EVT_MIDDLE_UP, self.OnMiddleUp) 
                self.Canvas.Bind(FloatCanvas.EVT_MIDDLE_DCLICK, self.OnMiddleDouble) 

                self.Canvas.Bind(FloatCanvas.EVT_RIGHT_DOWN, self.OnRightDown) 
                self.Canvas.Bind(FloatCanvas.EVT_RIGHT_UP, self.OnRightUp) 
                self.Canvas.Bind(FloatCanvas.EVT_RIGHT_DCLICK, self.OnRightDouble) 

            self.EventsAreBound = True


        def UnBindAllMouseEvents(self):
            ## Here is how you unbind FloatCanvas mouse events
            self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DOWN)
            self.Canvas.Unbind(FloatCanvas.EVT_LEFT_UP)
            self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DCLICK)

            self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DOWN)
            self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_UP)
            self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DCLICK)

            self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DOWN)
            self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_UP)
            self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DCLICK)

            self.EventsAreBound = False


        def PrintCoords(self,event):
            self.Log("coords are: %s"%(event.Coords,))
            self.Log("pixel coords are: %s\n"%(event.GetPosition(),))

        def OnSavePNG(self, event=None):
            import os
            dlg = wx.FileDialog(
                self, message="Save file as ...", defaultDir=os.getcwd(), 
                defaultFile="", wildcard="*.png", style=wx.SAVE
                )
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                if not(path[-4:].lower() == ".png"):
                    path = path+".png"
                self.Canvas.SaveAsImage(path)

                    
        def OnLeftDown(self, event):
            self.Log("LeftDown")
            self.PrintCoords(event)

        def OnLeftUp(self, event):
            self.Log("LeftUp")
            self.PrintCoords(event)

        def OnLeftDouble(self, event):
            self.Log("LeftDouble")
            self.PrintCoords(event)

        def OnMiddleDown(self, event):
            self.Log("MiddleDown")
            self.PrintCoords(event)

        def OnMiddleUp(self, event):
            self.Log("MiddleUp")
            self.PrintCoords(event)

        def OnMiddleDouble(self, event):
            self.Log("MiddleDouble")
            self.PrintCoords(event)

        def OnRightDown(self, event):
            self.Log("RightDown")
            self.PrintCoords(event)

        def OnRightUp(self, event):
            self.Log("RightUp")
            self.PrintCoords(event)

        def OnRightDouble(self, event):
            self.Log("RightDouble")
            self.PrintCoords(event)

        def OnWheel(self, event):
            self.Log("Mouse Wheel")
            self.PrintCoords(event)
            Rot = event.GetWheelRotation()
            Rot = Rot / abs(Rot) * 0.1
            if event.ControlDown(): # move left-right
                self.Canvas.MoveImage( (Rot, 0), "Panel" )
            else: # move up-down
                self.Canvas.MoveImage( (0, Rot), "Panel" )
                
        def OnMove(self, event):
            """
            Updates the status bar with the world coordinates
            """
            self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
            event.Skip()

        def OnAbout(self, event):
            dlg = wx.MessageDialog(self,
                                   "This is a small program to demonstrate\n"
                                   "the use of the FloatCanvas\n",
                                   "About Me",
                                   wx.OK | wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()

        def ZoomToFit(self,event):
            self.Canvas.ZoomToBB()

        def Clear(self,event = None):
            self.UnBindAllMouseEvents()
            self.Canvas.InitAll()
            self.Canvas.Draw()

        def OnQuit(self,event):
            self.Close(True)

        def OnCloseWindow(self, event):
            self.Destroy()

        def DrawTest(self,event=None):
            """
            This demo draws a few of everything

            """
            

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -