继承:私有属性和方法
- 默认情况下,python类的成员属性和方法都是public。
- 提供“访问控制符号”来限定成员函数的访问。
- 双下划线--—_var属性会被_classname_var替换,将防止父类与子类中的同名冲突。
- 单下划线———在属性名前使用一个单下划线字符,防止模块的属性用“from mymodule import *”来加载。
GUI的基本框架
- 一个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()
组件
- 组件容器(Containers)--用于容纳其他组件:例子:wx.Panel等
- 动态组件(Dynamic Widgets)--可用于被用户编辑:例子:wx.Button,wx.TextCtrl,wx.ListBox等
- 静态组件(Static Widgets)--显示信息用,不能被用户编辑,例:wx.StaticBitmap,wx.StaticText,wxStaticLine
- 其他组件 例: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常用组件
- 按钮Button
- wx.Button:文本按钮
- wx.BitmapButton:位图按钮
- wx.ToggleButton:开关按钮
- 菜单Menu
- wx.MenuBar
- wx.Menu
- wx.MeuItem
- wx.EVT_MENU
#绑定实践处理
self.Bind(wx.EVT_MENU,selfOnClickBigger.biggerItem)
self.Bind(wx.EVT_MENU,selfOnClickBigger.biggerItem)
self.Bind(wx.EVT_MENU,selfOnClickQuit,id=wx.ID_EXIT)
self.Bind(wx.EVT_MENU,selfOnClickQuit,id=wx.ID_EXIT)
#事件处理器
def OnClickBigger(self,e):
OnClickBigger(self,e):
pass
pass
def OnClickQuit(self,e):
OnClickQuit(self,e):
self.close()
self.close()
静态文本(StaticText)和文本框(TextCtrl)
列表(ListCtrl)
扫描二维码关注公众号,回复:
4565287 查看本文章
- wx.LC_ICON(图标)
- wx.LC_SMALL_ICON(小图标)
- wx.LC_LIST(列表)
- WX.LC_REPORT(报告)
单选框RadioBox和复选框CheckBox(与下面sizer的例子相同)布局管理
- sizer---一种布局算法并且允许嵌套,一般在容器之后,子容器之前进行创建
- wx.BoxSizer
- wx.FlexGridSizer
- wx.GridSizer
- wx.GridBagSizer
- wx.StaticBoxSizer
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 = "Hello World in wxPython")
wx.Frame.__init__(self, parent = superior, title = "Hello World in wxPython")
panel = wx.Panel(self)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.VERTICAL)
self.text1= wx.TextCtrl(panel, value = "Hello, World!", size = (200,180), style = wx.TE_MULTILINE)
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)
sizer.Add(self.text1, 0, wx.ALIGN_TOP | wx.EXPAND)
button = wx.Button(panel, label = "Click Me")
button = wx.Button(panel, label = "Click Me")
sizer.Add(button)
sizer.Add(button)
panel.SetSizerAndFit(sizer)
panel.SetSizerAndFit(sizer)
panel.Layout()
panel.Layout()
self.Bind(wx.EVT_BUTTON,self.OnClick,button)
self.Bind(wx.EVT_BUTTON,self.OnClick,button)
def OnClick(self, text):
def OnClick(self, text):
self.text1.AppendText("\nHello, World!")
self.text1.AppendText("\nHello, World!")
其他GUI库
- pyQT--比较丰富且兼容C++,注意用C++避免内存泄漏
import sys
sys
from PyQtS import QtWidgets:
PyQtS import QtWidgets:
class TestWidget(QtWidgets.QWidget):
class TestWidget(QtWidgets.QWidget):
def __init__(self):
def __init__(self):
super().__init__()
super().__init__()
self.setWindowTitle("HelloWorld")
self.setWindowTitle("HelloWorld")
self.outputArea=QtWidgets.QTextBrowser()
self.outputArea=QtWidgets.QTextBrowser()
self.helloButton=QtWidgets.QPushButton("Click Me")
self.helloButton=QtWidgets.QPushButton("Click Me")
self.layout=QtWidgets.QVBoxLayout()
self.layout=QtWidgets.QVBoxLayout()
self.layout.addWidget(self.outArea)
self.layout.addWidget(self.outArea)
self.layout.addWidget(self.helloButton)
self.layout.addWidget(self.helloButton)
self.setLayout(self.layout)
self.setLayout(self.layout)
self.Button.clicked.connect(self.sayHello)
self.Button.clicked.connect(self.sayHello)
def sayHello(self):
def sayHello(self):
self.outputArea.append("Hello World!")
self.outputArea.append("Hello World!")
if _name_=="__main__":
_name_=="__main__":
app=QTWidgets.QApplication(sys.argv)
app=QTWidgets.QApplication(sys.argv)
testWidget=TestWidget()
testWidget=TestWidget()
testWidget.show()
testWidget.show()
sys.exit(app.exec_())
sys.exit(app.exec_())
Tkinter--简单,性能不太好,速度比较慢
import tkinter as tk
tkinter as tk
class Tkdemo(object):
Tkdemo(object):
def __init__(self):
def __init__(self):
self.root=tk.Tk()
self.root=tk.Tk()
self.txt=tk.Text(self.root,width=30,height=10)
self.txt=tk.Text(self.root,width=30,height=10)
self.txt.pack()
self.txt.pack()
self.button=tk.Button(self.root,text='Click me',command=self.sayhello)
self.button=tk.Button(self.root,text='Click me',command=self.sayhello)
self.button.pack()
self.button.pack()
def sayhello(self):
def sayhello(self):
self.txt.insert(tk.INSERT,"Hello, World!\n")
self.txt.insert(tk.INSERT,"Hello, World!\n")
d=Tkdemo()
=Tkdemo()
d.root.mainloop()
.root.mainloop()
PyGTK---在Windows上表现得不太好
import pygtk
pygtk
pygtk.require('2.0')
.require('2.0')
import gtk
gtk
class HelloWorld:
HelloWorld:
def hello(self, widget, data=None):
def hello(self, widget, data=None):
textbuffer = self.textview.get_buffer()
textbuffer = self.textview.get_buffer()
startiter, enditer = textbuffer.get_bounds()
startiter, enditer = textbuffer.get_bounds()
content_text = textbuffer.get_text(startiter, enditer)
content_text = textbuffer.get_text(startiter, enditer)
content_text += "Hello, World!\n"
content_text += "Hello, World!\n"
textbuffer.set_text(content_text)
textbuffer.set_text(content_text)
def __init__(self):
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("A Simple Example of PyGtk")
self.window.set_title("A Simple Example of PyGtk")
self.window.connect("delete_event", self.delete_event)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.window.set_border_width(10)
box1 = gtk.VBox(False, 0)
box1 = gtk.VBox(False, 0)
self.window.add(box1)
self.window.add(box1)
box1.show()
box1.show()
sw = gtk.ScrolledWindow()
sw = gtk.ScrolledWindow()
sw.set_policy(gtk.POLICY_AUTOMATIC,
sw.set_policy(gtk.POLICY_AUTOMATIC,
gtk.POLICY_AUTOMATIC)
gtk.POLICY_AUTOMATIC)
self.textview = gtk.TextView()
self.textview = gtk.TextView()
textbuffer = self.textview.get_buffer()
textbuffer = self.textview.get_buffer()
sw.add(self.textview)
sw.add(self.textview)
sw.show()
sw.show()
self.textview.show()
self.textview.show()
box1.pack_start(sw)
box1.pack_start(sw)
self.button = gtk.Button("Click Me")
self.button = gtk.Button("Click Me")
self.button.connect("clicked", self.hello, None)
self.button.connect("clicked", self.hello, None)
self.button.show()
self.button.show()
box1.pack_start(self.button, expand=False, fill=False)
box1.pack_start(self.button, expand=False, fill=False)
self.window.show()
self.window.show()
def main(self):
def main(self):
gtk.main()
gtk.main()
if __name__ == "__main__":
__name__ == "__main__":
hello = HelloWorld()
hello = HelloWorld()
hello.main()
hello.main()