专题教程:wxpython中文教程
wxPython 提供了几个通用对话框,可以帮助应用程序更为符合有关用户界面的各种标准.
一些通用对话框:
wx.MessageDialog
wx.ColourDialog
wx.PageSetupDialog
wx.FontDialog
wx.DirDialog
wx.SingleChoiceDialog
wx.TextEntryDialog
例子:
#!/usr/bin/python
#coding=utf-8
# www.plcxue.com
#commondialogs.py
import wx
import os, sys
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
statusBar = self.CreateStatusBar()
statusBar.SetMinHeight(100)
menuBar= wx.MenuBar()
menu = wx.Menu()
menu.Append(99, "&Message Dialog", "Shows a Message Dialog")
menu.Append(100, "&Colour Dialog", "Shows a Color Dialog")
menu.Append(101, "&File Dialog", "Shows a File Dialog")
menu.Append(102, "&Page Setup Dialog", "Shows a Page Setup Dialog")
menu.Append(103, "F&ont Dialog", "Shows a Font Dialog")
menu.Append(104, "D&irectory Dialog", "Shows a Directory Dialog")
menu.Append(105, "&SingleChoice Dialog", "Shows a SingleChoice Dialog")
menu.Append(106, "&TextEntry", "Shows a TextEntry Dialog")
menu.Append(107, "&Quit", "退出程序")
menuBar.Append(menu, "&Dialogs")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.message, id=99)
self.Bind(wx.EVT_MENU, self.choosecolor, id=100)
self.Bind(wx.EVT_MENU, self.openfile, id=101)
self.Bind(wx.EVT_MENU, self.pagesetup, id=102)
self.Bind(wx.EVT_MENU, self.choosefont, id=103)
self.Bind(wx.EVT_MENU, self.opendir, id=104)
self.Bind(wx.EVT_MENU, self.singlechoice, id=105)
self.Bind(wx.EVT_MENU, self.textentry, id=106)
self.Bind(wx.EVT_MENU, self.quit, id=107)
def message(self, event):
dlg = wx.MessageDialog(self, 'To save one life is as if you have save the world.',
'Talmud', wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def choosecolor(self, event):
dlg = wx.ColourDialog(self)
dlg.GetColourData().SetChooseFull(True)
if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetColourData()
self.SetStatusText('You Selected: %s' % str(data.GetColour().Get()))
dlg.Destroy()
def openfile(self, event):
dlg = wx.FileDialog(self, "Choose a file: ", os.getcwd(), "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
mypath = os.path.basename(path)
self.SetStatusText("You selected: %s" % mypath)
dlg.Destroy()
def pagesetup(self, event):
dlg = wx.PageSetupDialog(self)
if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetPageSetupData()
tl = data.GetMarginTopLeft()
br = data.GetMarginBottomRight()
self.SetStatusText('Margins are: %s %s ' % (str(tl), str(br)))
dlg.Destroy()
def choosefont(self, event):
default_font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False, "Verdana")
data = wx.FontData()
if sys.platform == 'win32':
data.EnableEffects(True)
data.SetAllowSymbols(False)
data.SetInitialFont(default_font)
data.SetRange(10, 30)
dlg = wx.FontDialog(self, data)
if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetFontData()
font = data.GetChosenFont()
color = data.GetColour()
text = 'Face: %s, Size: %s, Color: %s' % (font.GetFaceName(), font.GetPointSize(), color.Get())
self.SetStatusText(text)
dlg.Destroy()
def opendir(self, event):
dlg = wx.DirDialog(self, "Choose a directory: ", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dlg.ShowModal() == wx.ID_OK:
self.SetStatusText('You selected: %s' % dlg.GetPath())
dlg.Destroy()
def singlechoice(self, event):
sins = ['Greed', 'Lust', 'Gluttony', 'Pride', 'Sloth', 'Envy', 'Wrath']
dlg = wx.SingleChoiceDialog(self, 'Seven Deadly sin: ', 'Which one?', sins, wx.CHOICEDLG_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.SetStatusText('You choose: %s' % dlg.GetStringSelection())
dlg.Destroy()
def textentry(self, event):
dlg = wx.TextEntryDialog(self, 'Enter some text', 'Text Entry')
dlg.SetValue('Default')
if dlg.ShowModal() == wx.ID_OK:
self.SetStatusText('You entered: %s' % dlg.GetValue())
dlg.Destroy()
def quit(self, event):
dlg = self.Close()
class MyApp(wx.App):
def OnInit(self):
myframe = MyFrame(None, -1, 'commondialogs.py')
myframe.CenterOnScreen()
myframe.Show(True)
return True
app = MyApp(0)
app.MainLoop()
这段程序展示了 8 种不同的通用对话框. 所有输出都显示在状态栏上.
效果图,如下所示: