Python 初识GUI

来源:互联网 发布:java 打包tar.gz 编辑:程序博客网 时间:2024/06/10 21:39

学习资料来自于

1.      Coursera 《用Python玩转数据》 https://www.coursera.org/learn/hipython 

2.      Coursera 《Python交互程序设计入门》https://www.coursera.org/course/interactivepython1

 

Python面向对象

  1. 定义一个小狗类

class Dog(object):

    def __init__(self,name):

        self.name = name

    def sound(self):

        print ("wang wang")

  1. 创建小狗的实例

>>> dog = Dog('mi')

>>>dog.sound()

wang wang

  1. 继承

class animal(object):

    def __init__(self,name):

        self.name = name

    def sound(self):

        print ("none")

       

class Dog(animal):

    def sound(self):

        print ("wang wang")

 

dog = Dog('mi')

dog.sound()

可在wxpython的官方网站查到类之间的继承关系http://wxpython.org/docs/api

  1. 私有变量

class Dog(animal):

    def __init__(self,name):

        self.__name__ = name

 

 

Python 初识GUI – 图形用户界面

GUI 开发库

  1. PyQt – 可免费地用于自由软件的开发,跨平台,缺点是运行时庞大,需要C++ 知识,更适合专业人士使用。
  2. Tkinter – 绑定Python的Tk GUI 工具集, 通过内嵌在Python解释器内部的Tcl解释器实现,优点是代码简洁,缺点是性能不够好,执行速度慢。
  3. PyGTC – 基于LGPL协议,免费,在UNIX平台下表现很好,Windows上的表现不太好。
  4. wxPython http://wxpython.org/

 

本例中使用wxPython

程序示例:在程序Frame中按下鼠标左键时,在鼠标按下的位置出现一个Button

import wx

class MyFrame(wx.Frame):

    def __init__(self, parent):

      wx.Frame.__init__(self, parent,title='Hello Python')

      self.panel = wx.Panel(self)

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

    def OnClick(self, event):

      posm = event.GetPosition()

      wx.Button(self.panel,label="Hi~~~",pos = (posm.x, posm.y))

if __name__=='__main__':

    app = wx.App()

    frame = MyFrame(None)

    frame.Show(True)

    app.MainLoop()

 

0 0