筆記 Data Processing Using Python 5(GUI和圖形化介面)
阿新 • • 發佈:2018-12-19
繼承:私有屬性和方法
- 預設情況下,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)
- 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()