您的位置:首页 > 其它

用wx.MessageDialog创建消息对话框

2012-07-15 21:22 288 查看
运行效果图:



Python代码:

#!/usr/bin/env python
# -*- encoding:utf-8 -*-
'Create MessageDialog Example'

import wx

class MyFrame(wx.Frame):

def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'MessageDialog',size=(300,200))
panel=wx.Panel(self)

box=wx.MessageDialog(None,'Go On?','Message',wx.OK)
answer=box.ShowModal()
box.Destroy()

if __name__=='__main__':
app=wx.PySimpleApp()
myframe=MyFrame(parent=None,id=-1)
myframe.Show(True)
app.MainLoop()

wx.MessageDialog函数参数如下:

wx.MessageDialog(parent,  message, caption, style
= wxOK | wxCANCEL,  pos = wxDefaultPosition)

style的取值主要有以下几种:
wxOKShow an OK button.
wxCANCELShow a Cancel button.
wxYES_NOShow Yes and No buttons.
wxYES_DEFAULTUsed with wxYES_NO, makes Yes button the default - which is the default behaviour.
wxNO_DEFAULTUsed with wxYES_NO, makes No button the default.
wxICON_EXCLAMATIONShows an exclamation mark icon.
wxICON_HANDShows an error icon.
wxICON_ERRORShows an error icon - the same as wxICON_HAND.
wxICON_QUESTIONShows a question mark icon.
wxICON_INFORMATIONShows an information (i) icon.
wxSTAY_ON_TOPThe message box stays on top of all other window, even those of the other applications (Windows only). 
在看看相关的wx.ShowModal()函数,它使用应用程序在对话框关闭前不能响应其它窗口的用户事件,返回一个整数,取值如下:

  wx.ID_YES, wx.ID_NO, wx.ID_CANCEL, wx.ID_OK。

例子二:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx

class MyFrame(wx.Frame):

def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, u'测试面板Panel', size = (600, 300))

#创建面板
panel = wx.Panel(self)

#在Panel上添加Button
button = wx.Button(panel, label = u'关闭', pos = (150, 60), size = (100, 60))

#绑定单击事件
self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button)

def OnCloseMe(self, event):
dlg = wx.MessageDialog(None, u"消息对话框测试", u"标题信息", wx.YES_NO | wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
self.Close(True)
dlg.Destroy()

if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()


效果图:



例子三:

import wx

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Hello My Message", size=(300,200))
panel=wx.Panel(self)
b = wx.Button(panel, -1, "Show a Message", (100,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Show()
def OnButton(self, evt):
dlg = wx.MessageDialog(self, 'Hello from Python and wxPython!',
'A Message Box',
wx.OK | wx.ICON_INFORMATION
#wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
)
dlg.ShowModal()
dlg.Destroy()
evt.Skip()
def OnClose(self, evt):
dlg = wx.MessageDialog(self, 'Are you sure you want to close My Frame?',
'Close Frame', wx.YES_NO | wx.ICON_QUESTION)
ret = dlg.ShowModal()
dlg.Destroy()
if ret == wx.ID_YES:
evt.Skip()

app = wx.App(0)
MyFrame()
app.MainLoop()


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐