大多数现代的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()
wx.FutureCall(5000, self.ShowMessage)
def ShowMessage(self): wx.MessageBox('Download completed', 'Info', wx.OK | wx.ICON_INFORMATION)
预定义的对话框
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()