笔记 Data Processing Using Python 5(GUI和图形化界面)

  • 继承:私有属性和方法
  1. 默认情况下,python类的成员属性和方法都是public。
  2. 提供“访问控制符号”来限定成员函数的访问。
    1. 双下划线--—_var属性会被_classname_var替换,将防止父类与子类中的同名冲突。
    2. 单下划线———在属性名前使用一个单下划线字符,防止模块的属性用“from mymodule import *”来加载。
  • GUI的基本框架
  1. 一个wxPython的例子
    import wx
     wx
    class MyApp(wx.App):
     MyApp(wx.App):
        def OnInit(self):
    def OnInit(self):
            frame=wx.Frame(None,title="Hello, World!")
    frame=wx.Frame(None,title="Hello, World!")
            frame.show()
    frame.show()
            return True
    return True
    if _name_=='__main__'
     _name_=='__main__'
    app=MyApp()
    =MyApp()
    app.MainLoop()
    .MainLoop()

     

  • 组件
  1. 组件容器(Containers)--用于容纳其他组件:例子:wx.Panel等
  2. 动态组件(Dynamic Widgets)--可用于被用户编辑:例子:wx.Button,wx.TextCtrl,wx.ListBox等
  3. 静态组件(Static Widgets)--显示信息用,不能被用户编辑,例:wx.StaticBitmap,wx.StaticText,wxStaticLine
  4. 其他组件 例:wx.ToolBar,wx.MenuBar,wx.StatusBar
    import wx
     wx
    class Frame1(wx.Frame):
     Frame1(wx.Frame):
        def __init_(self,superior):
    def __init_(self,superior):
            wx.Frame.__init__(self,parent=superior,title="Example",pos=(100,200),size(350,200))
    wx.Frame.__init__(self,parent=superior,title="Example",pos=(100,200),size(350,200))
            panel=wx.Panel(self)
    panel=wx.Panel(self)
            text1=wx.TextCtrl(panel,value="Hello World!",size=(350,200))
    text1=wx.TextCtrl(panel,value="Hello World!",size=(350,200))
    if _name_=='__main__':
     _name_=='__main__':
        app=wx.App()
    app=wx.App()
        frame=Frame1(None)
    frame=Frame1(None)
        frame.Show(True)
    frame.Show(True)
        app.MainLoop()
    app.MainLoop()

     

  • GUI工作的基本机制--事件处理
import wx
 wx
class Framel(wx.Frame):
 Framel(wx.Frame):
    def __init__(self,superior):
def __init__(self,superior):
        .... ...

        self.panel.Bind(wx.EVT_LEFT_UP,self.OnClick)
self.panel.Bind(wx.EVT_LEFT_UP,self.OnClick)
        

    def OnClick(self,event):
def OnClick(self,event):
        posm=even.GetPosition()
posm=even.GetPosition()
        wx.StaticText(parent=self.panel,label="Hello World!",pos=(posm.x,posm.y))
wx.StaticText(parent=self.panel,label="Hello World!",pos=(posm.x,posm.y))
    ....#create app and frame,show and execute event loop
#create app and frame,show and execute event loop
  • GUI常用组件
  1. 按钮Button
    1. wx.Button:文本按钮
    2. wx.BitmapButton:位图按钮
    3. wx.ToggleButton:开关按钮
  2. 菜单Menu
    1. wx.MenuBar
    2. wx.Menu
    3. wx.MeuItem
    4. wx.EVT_MENU
      #绑定实践处理
      self.Bind(wx.EVT_MENU,selfOnClickBigger.biggerItem)
      
          self.Bind(wx.EVT_MENU,selfOnClickQuit,id=wx.ID_EXIT)
      
      # 事件处理器
      def OnClickBigger(self,e):
          pass
      
      def OnClickQuit(self,e):
          self.close()
      

       

  • 静态文本(StaticText)和文本框(TextCtrl)
  • 列表(ListCtrl)
  1. wx.LC_ICON(图标)
  2. wx.LC_SMALL_ICON(小图标)
  3. wx.LC_LIST(列表)
  4. WX.LC_REPORT(报告)
  • 单选框RadioBox和复选框CheckBox(与下面sizer的例子相同)布局管理
  1. sizer---一种布局算法并且允许嵌套,一般在容器之后,子容器之前进行创建
    1. wx.BoxSizer
    2. wx.FlexGridSizer
    3. wx.GridSizer 
    4. wx.GridBagSizer
    5. wx.StaticBoxSizer
      import wx
      
      class Frame1(wx.Frame):
          def __init__(self,superior):
      
              wx.Frame.__init__(self, parent = superior, title = "Hello World in wxPython")
              panel = wx.Panel(self)
              sizer = wx.BoxSizer(wx.VERTICAL)
              self.text1= wx.TextCtrl(panel, value = "Hello, World!", size = (200,180), style = wx.TE_MULTILINE)
      
              sizer.Add(self.text1, 0, wx.ALIGN_TOP | wx.EXPAND)
      
      button = wx.Button(panel, label = "Click Me")
              sizer.Add(button)
      
              panel.SetSizerAndFit(sizer)
      
              panel.Layout()
      
              self.Bind(wx.EVT_BUTTON,self.OnClick,button)
      
      
      def OnClick(self, text):
              self.text1.AppendText("\nHello, World!")
      

       

  • 其他GUI库
    • pyQT--比较丰富且兼容C++,注意用C++避免内存泄漏
      import sys
      from PyQtS import QtWidgets:
      
      class TestWidget(QtWidgets.QWidget):
              def __init__(self):
                  super().__init__()
      
                  self.setWindowTitle("HelloWorld")
      
                  self.outputArea=QtWidgets.QTextBrowser()
      
                  self.helloButton=QtWidgets.QPushButton("Click Me")
      
                  self.layout=QtWidgets.QVBoxLayout()
      
                  self.layout.addWidget(self.outArea)
      
                  self.layout.addWidget(self.helloButton)
      
                  self.setLayout(self.layout)
      
                  self.Button.clicked.connect(self.sayHello)
         def sayHello(self):
      
                  self.outputArea.append("Hello World!")
      
      if _name_=="__main__":
          app=QTWidgets.QApplication(sys.argv)
      
          testWidget=TestWidget()
      
          testWidget.show()
      
          sys.exit(app.exec_())
      
      
    • Tkinter--简单,性能不太好,速度比较慢
      import tkinter as tk
      
      class Tkdemo(object):
      
          def __init__(self):
      
              self.root=tk.Tk()
      
              self.txt=tk.Text(self.root,width=30,height=10)
      
              self.txt.pack()
      
              self.button=tk.Button(self.root,text='Click me',command=self.sayhello)
      
              self.button.pack()
      
          def sayhello(self):
      
              self.txt.insert(tk.INSERT,"Hello, World!\n")
      
      d=Tkdemo()
      
      d.root.mainloop() 
      
    • PyGTK---在Windows上表现得不太好
      import pygtk
      
      pygtk.require('2.0')
      
      import gtk
      
      class HelloWorld:
      
          def hello(self, widget, data=None):
      
              textbuffer = self.textview.get_buffer()
      
              startiter, enditer = textbuffer.get_bounds()
      
              content_text = textbuffer.get_text(startiter, enditer)
      
              content_text += "Hello, World!\n"
      
              textbuffer.set_text(content_text)
      
          def __init__(self):
      
              self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
      
              self.window.set_title("A Simple Example of PyGtk")
      
              self.window.connect("delete_event", self.delete_event)
      
              self.window.connect("destroy", self.destroy)
      
              self.window.set_border_width(10)
      
              box1 = gtk.VBox(False, 0)
      
              self.window.add(box1)
      
              box1.show()
      
              sw = gtk.ScrolledWindow()
      
              sw.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
      
              self.textview = gtk.TextView()
      
              textbuffer = self.textview.get_buffer()
      
              sw.add(self.textview)
      
              sw.show()
      
              self.textview.show()
      
              box1.pack_start(sw)
      
              self.button = gtk.Button("Click Me")
      
              self.button.connect("clicked", self.hello, None)
      
              self.button.show()
              box1.pack_start(self.button, expand=False, fill=False)
      
              self.window.show()
      
          def main(self):
      
              gtk.main()
      
      if __name__ == "__main__":
      
          hello = HelloWorld()
      
          hello.main()
      

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值