zoukankan      html  css  js  c++  java
  • wxPython学习笔记

    ------------恢复内容开始------------

    学习wxPython 资料

    1.wxpython wiki

    Getting started with wxPython

    https://wiki.wxpython.org/Getting%20Started

    入门例子程序:

    A First Application: "Hello, World"

    
    
    #!/usr/bin/env python
    import wx
    
    app = wx.App(False)  # Create a new app, don't redirect stdout/stderr to a window.
    frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
    frame.Show(True)     # Show the frame.
    app.MainLoop()

     

    Explanations:

    app = wx.App(False)

    Every wxPython app is an instance of wx.App. For most simple applications you can use wx.App as is. When you get to more complex applications you may need to extend the wx.App class. The "False" parameter means "don't redirect stdout and stderr to a window".

    wx.Frame(None,wx.ID_ANY,"Hello")

    wx.Frame is a top-level window. The syntax is wx.Frame(Parent, Id, Title). Most of the constructors have this shape (a parent object, followed by an Id). In this example, we use None for "no parent" and wx.ID_ANY to have wxWidgets pick an id for us.

    frame.Show(True)

    We make a frame visible by "showing" it.

    app.MainLoop()

    Finally, we start the application's MainLoop whose role is to handle the events.

     

    Building a simple text editor

    #!/usr/bin/env python
    import wx
    class MyFrame(wx.Frame):
        """ We simply derive a new class of Frame. """
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title, size=(200,100))
            self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
            self.Show(True)
    
    app = wx.App(False)
    frame = MyFrame(None, 'Small editor')
    app.MainLoop()

    In this example, we derive from wx.Frame and overwrite its __init__ method. Here we declare a new wx.TextCtrl which is a simple text edit control. Note that since the MyFrame runs self.Show() inside its __init__ method, we no longer have to call frame.Show() explicitly.

    Adding a menu bar

    Every application should have a menu bar and a status bar. Let's add them to ours

    import wx
    
    class MainWindow(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title, size=(200,100))
            self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
            self.CreateStatusBar() # A Statusbar in the bottom of the window
    
            # Setting up the menu.
            filemenu= wx.Menu()
    
            # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
            filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
            filemenu.AppendSeparator()
            filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
    
            # Creating the menubar.
            menuBar = wx.MenuBar()
            menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
            self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
            self.Show(True)
    
    app = wx.App(False)
    frame = MainWindow(None, "Sample editor")
    app.MainLoop()

    add event  handling

    import os
    import wx
    
    
    class MainWindow(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title, size=(200,100))
            self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
            self.CreateStatusBar() # A StatusBar in the bottom of the window
    
            # Setting up the menu.
            filemenu= wx.Menu()
    
            # wx.ID_ABOUT and wx.ID_EXIT are standard ids provided by wxWidgets.
            menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
            menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
    
            # Creating the menubar.
            menuBar = wx.MenuBar()
            menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
            self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
    
            # Set events.
            self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
            self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
    
            self.Show(True)
    
        def OnAbout(self,e):
            # A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
            dlg = wx.MessageDialog( self, "A small text editor", "About Sample Editor", wx.OK)
            dlg.ShowModal() # Show it
            dlg.Destroy() # finally destroy it when finished.
    
        def OnExit(self,e):
            self.Close(True)  # Close the frame.
    
    app = wx.App(False)
    frame = MainWindow(None, "Sample editor")
    app.MainLoop()

    add open text event handling

    import wx
    import os
    
    class MainWindow(wx.Frame):
    
        def __init__(self, parent, title):
            self.dirname = ''
            wx.Frame.__init__(self, parent, title=title, size=(200,-1))
            self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,pos=(40,50))
            self.CreateStatusBar() # A Statusbar in the bottom of the window
    
            #panel = wx.Panel(self)
            #self.quote = wx.StaticText(panel, label="This is a static label", pos=(100,-1))
    
            # Setting up the menu.
            filemenu= wx.Menu()
            opermenu = wx.Menu()
            operItem1 = opermenu.Append(wx.ID_ANY, "&operation", "process operation")
    
            # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
            menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open the file")
            filemenu.AppendSeparator()
            menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
            filemenu.AppendSeparator()
            menuExit =  filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program")
    
            # Creating the menubar.
            menuBar = wx.MenuBar()
            menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
            menuBar.Append(opermenu,"&Operation")  # Adding the "filemenu" to the MenuBar
            self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
    
            # set events
            self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
            self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
            self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
    
            self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
            self.buttons = []
            for i in range(0, 6):
                self.buttons.append(wx.Button(self, -1, "Button &"+str(i+1)))
                self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)
    
            # Use some sizers to see layout options
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            #self.sizer.Add(self.quote,0,wx.EXPAND)
            self.sizer.Add(self.control, 1, wx.EXPAND)
            self.sizer.Add(self.sizer2, 0, wx.EXPAND)
    
            #Layout sizers
            self.SetSizer(self.sizer)
            self.SetAutoLayout(True)
            self.sizer.Fit(self)
    
    
            self.Show(True)
    
        def OnAbout(self,event):
            dlg = wx.MessageDialog(self, "A Small Text Editor", "About Sample Editor", wx.OK)
            #wx.MessageDialog()
            dlg.ShowModal()
            dlg.Destroy()
    
        def OnExit(self,event):
            self.Close(True)
    
        def OnOpen(self,event):
            """ open the file """
            dlg = wx.FileDialog(self,"choose a file ",self.dirname, "","*.*",wx.FD_OPEN)
            dlg = wx.FileDialog(self,"choose a file ",self.dirname, "","*.*",wx.FD_OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                self.filename = dlg.GetFilename()
                self.dirname = dlg.GetDirectory()
                f = open(os.path.join(self.dirname, self.filename), 'r')
                self.control.SetValue(f.read())
                f.close()
            dlg.Destroy()
    app = wx.App(False)
    frame = MainWindow(None, "Sample editor")
    app.MainLoop()
  • 相关阅读:
    (转载)正向代理与反向代理的区别
    Java设计模式系列之装饰者模式
    工作中使用到的技术和工具分享
    Springboot+mybatis整合
    云计算入门和实践
    tesseract系列(1) -- tesseract用vs编译成库
    nodepad++ 让所有的加号收缩折叠展开的快捷键
    tesseract系列(4) -- tesseract训练问题总结
    tessereact的链接收藏
    菜鸟cmake使用
  • 原文地址:https://www.cnblogs.com/yanghonker/p/12377648.html
Copyright © 2011-2022 走看看