大多數現代的GUI應用程序,對話框窗口或對話框是不可或缺的一部分。一個對話框被定義爲兩個或以上的人之間的談話。在一個計算機應用一個對話框窗口用於“交談”應用程序。一個對話框用於輸入數據、修改數據的修改應用程序的設置等。對話框是重要的通信手段之間的一個用戶和計算機程序。
一個簡單的消息框
一個消息框提供短信息給用戶。一個很好的例子是一個cd刻錄的應用程序。當一個cd完成刻錄,彈出一個消息框。
import wx
class Example(wx.Frame):
def __init__(self,*args,**kw):
super(Example,self).__init__(*args,**kw)
self.InitUI()
def InitUI(self):
wx.FutureCall(5000,self.ShowMessage)
self.SetSize((300,200))
self.Centre()
self.Show(True)
def ShowMessage(self):
wx.MessageBox('Down Completed','Info',wx.OK|wx.ICON_INFORMATION)
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()
這個例子,展示了一個在5秒後彈出的消息框。
wx.FutureCall(5000, self.ShowMessage)
wx.FutureCall在5秒後調用的一個方法。第一個參數是一個時間值之後,一個給定的方法被調用。參數是毫秒。第二個參數是一個將被調用的方法。
def ShowMessage(self):
wx.MessageBox('Download completed', 'Info',
wx.OK | wx.ICON_INFORMATION)
wx.MessageBox顯示一個小的對話框窗口,我們提供三個參數。文本消息,標題消息和旗幟。旗子只是用來顯示不同的按鈕和圖標。在我們的案例中,我們展示了一個OK按鈕和信息圖標。
預定義的對話框
wxPython有幾個預定義對話。這些是對話框通用編程任務比如顯示文本、接收輸入,加載和保存文件等等
消息對話框用於向用戶顯示消息。他們更靈活,比簡單的信息框,我們看到在前面的示例。他們是可定製的。我們可以改變圖標和按鈕,將顯示在一個對話框。
以下是旗幟,可以使用的wx.MessageDialog類。
flag
meaning
wx.OK show Ok button
wx.CANCEL show Cancel button
wx.YES_NO show Yes, No buttons
wx.YES_DEFAULT make Yes button the default
wx.NO_DEFAULT make No button the default
wx.ICON_EXCLAMATION show an alert icon
wx.ICON_ERROR show an error icon
wx.ICON_HAND same as wx.ICON_ERROR
wx.ICON_INFORMATION show an info icon
wx.ICON_QUESTION show a question icon
import wx
class Example(wx.Frame):
def __init__(self,*args,**kw):
super(Example,self).__init__(*args,**kw)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
hbox = wx.BoxSizer()
sizer = wx.GridSizer(2,2,2,2)
btn1 = wx.Button(panel,label='info')
btn2 = wx.Button(panel,label='error')
btn3 = wx.Button(panel,label='question')
btn4 = wx.Button(panel,label='alert')
sizer.AddMany([btn1,btn2,btn3,btn4])
hbox.Add(sizer,0,wx.ALL,15)
panel.SetSizer(hbox)
btn1.Bind(wx.EVT_BUTTON, self.ShowMessage1)
btn2.Bind(wx.EVT_BUTTON, self.ShowMessage2)
btn3.Bind(wx.EVT_BUTTON, self.ShowMessage3)
btn4.Bind(wx.EVT_BUTTON, self.ShowMessage4)
self.SetSize((300, 200))
self.SetTitle('Messages')
self.Centre()
self.Show(True)
def ShowMessage1(self,e):
dial = wx.MessageDialog(None,'Download completed','info',wx.OK)
dial.ShowModal()
def ShowMessage2(self, event):
dial = wx.MessageDialog(None, 'Error loading file', 'Error',
wx.OK | wx.ICON_ERROR)
dial.ShowModal()
def ShowMessage3(self, event):
dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
dial.ShowModal()
def ShowMessage4(self, event):
dial = wx.MessageDialog(None, 'Unallowed operation', 'Exclamation',
wx.OK | wx.ICON_EXCLAMATION)
dial.ShowModal()
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()
這個例子顯示了四種不同的對話框圖標。
<關於>對話框
幾乎每一個應用程序有一個典型的關於對話框。它通常被放在的幫助菜單。這個對話框的目的是爲了給用戶的基本信息的名稱和版本的應用程序。在過去,這些對話框曾經是十分簡短。這些天大多數這些盒子提供額外的作者信息。他們給學分額外的程序員或文檔的作家。他們也提供關於應用程序的信息執照。這些箱子可以顯示公司的標誌,或者應用程序圖標。一些更有能力對盒子動畫。
'''
Created on 2012-7-6
@author: Administrator
'''
import wx
class Example(wx.Frame):
def __init__(self,*args,**kw):
super(Example,self).__init__(*args,**kw)
self.InitUI()
def InitUI(self):
menubar = wx.MenuBar()
help = wx.Menu()
help.Append(100,'&About')
self.Bind(wx.EVT_MENU, self.OnAboutBox, id=100)
menubar.Append(help,'&Help')
self.SetMenuBar(menubar)
self.SetSize((300, 200))
self.SetTitle('About dialog box')
self.Centre()
self.Show(True)
def OnAboutBox(self,e):
description = """File Hunter is an advanced file manager for
the Unix operating system. Features include powerful built-in editor,
advanced search capabilities, powerful batch renaming, file comparison,
extensive archive handling and more.
"""
licence = """File Hunter is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
File Hunter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details. You should have
received a copy of the GNU General Public License along with File Hunter;
if not, write to the Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA"""
info = wx.AboutDialogInfo()
info.SetIcon(wx.Icon('exit.png',wx.BITMAP_TYPE_PNG))
info.SetVersion('2.0')
info.SetDescription(description)
info.SetCopyright('(C) 2007 - 2011 Jan Bodnar')
info.SetWebSite('http://www.zetcode.com')
info.SetLicence(licence)
info.AddDeveloper('Jan Bodnar')
info.AddDocWriter('Jan Bodnar')
info.AddArtist('The Tango crew')
info.AddTranslator('Jan Bodnar')
wx.AboutBox(info)
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()