📄 dialogs.py
字号:
fontData.SetInitialFont(font)
dialog = wx.FontDialog(parent, fontData)
result = DialogResults(dialog.ShowModal())
if result.accepted:
fontData = dialog.GetFontData()
result.fontData = fontData
result.color = fontData.GetColour().Get()
result.colour = result.color
result.font = fontData.GetChosenFont()
else:
result.color = None
result.colour = None
result.font = None
dialog.Destroy()
return result
def textEntryDialog(parent=None, message='', title='', defaultText='',
style=wx.OK | wx.CANCEL):
dialog = wx.TextEntryDialog(parent, message, title, defaultText, style)
result = DialogResults(dialog.ShowModal())
result.text = dialog.GetValue()
dialog.Destroy()
return result
def messageDialog(parent=None, message='', title='Message box',
aStyle = wx.OK | wx.CANCEL | wx.CENTRE,
pos=wx.DefaultPosition):
dialog = wx.MessageDialog(parent, message, title, aStyle, pos)
result = DialogResults(dialog.ShowModal())
dialog.Destroy()
return result
## KEA: alerts are common, so I'm providing a class rather than
## requiring the user code to set up the right icons and buttons
## the with messageDialog function
def alertDialog(parent=None, message='', title='Alert', pos=wx.DefaultPosition):
return messageDialog(parent, message, title, wx.ICON_EXCLAMATION | wx.OK, pos)
def scrolledMessageDialog(parent=None, message='', title='', pos=wx.DefaultPosition,
size=(500,300)):
dialog = ScrolledMessageDialog(parent, message, title, pos, size)
result = DialogResults(dialog.ShowModal())
dialog.Destroy()
return result
def fileDialog(parent=None, title='Open', directory='', filename='', wildcard='*.*',
style=wx.OPEN | wx.MULTIPLE):
dialog = wx.FileDialog(parent, title, directory, filename, wildcard, style)
result = DialogResults(dialog.ShowModal())
if result.accepted:
result.paths = dialog.GetPaths()
else:
result.paths = None
dialog.Destroy()
return result
## openFileDialog and saveFileDialog are convenience functions
## they represent the most common usages of the fileDialog
## with the most common style options
def openFileDialog(parent=None, title='Open', directory='', filename='',
wildcard='All Files (*.*)|*.*',
style=wx.OPEN | wx.MULTIPLE):
return fileDialog(parent, title, directory, filename, wildcard, style)
def saveFileDialog(parent=None, title='Save', directory='', filename='',
wildcard='All Files (*.*)|*.*',
style=wx.SAVE | wx.OVERWRITE_PROMPT):
return fileDialog(parent, title, directory, filename, wildcard, style)
def dirDialog(parent=None, message='Choose a directory', path='', style=0,
pos=wx.DefaultPosition, size=wx.DefaultSize):
dialog = wx.DirDialog(parent, message, path, style, pos, size)
result = DialogResults(dialog.ShowModal())
if result.accepted:
result.path = dialog.GetPath()
else:
result.path = None
dialog.Destroy()
return result
directoryDialog = dirDialog
def singleChoiceDialog(parent=None, message='', title='', lst=[],
style=wx.OK | wx.CANCEL | wx.CENTRE):
dialog = wx.SingleChoiceDialog(parent, message, title, list(lst), style | wx.DEFAULT_DIALOG_STYLE)
result = DialogResults(dialog.ShowModal())
result.selection = dialog.GetStringSelection()
dialog.Destroy()
return result
def multipleChoiceDialog(parent=None, message='', title='', lst=[],
pos=wx.DefaultPosition, size=wx.DefaultSize):
dialog = wx.MultiChoiceDialog(parent, message, title, lst,
wx.CHOICEDLG_STYLE, pos)
result = DialogResults(dialog.ShowModal())
result.selection = tuple([lst[i] for i in dialog.GetSelections()])
dialog.Destroy()
return result
if __name__ == '__main__':
#import os
#print os.getpid()
class MyApp(wx.App):
def OnInit(self):
self.frame = frame = wx.Frame(None, -1, "Dialogs", size=(400, 240))
panel = wx.Panel(frame, -1)
self.panel = panel
dialogNames = [
'alertDialog',
'colorDialog',
'directoryDialog',
'fileDialog',
'findDialog',
'fontDialog',
'messageDialog',
'multipleChoiceDialog',
'openFileDialog',
'saveFileDialog',
'scrolledMessageDialog',
'singleChoiceDialog',
'textEntryDialog',
]
self.nameList = wx.ListBox(panel, -1,
size=(130, 180),
choices=dialogNames,
style=wx.LB_SINGLE)
self.Bind(wx.EVT_LISTBOX, self.OnNameListSelected, self.nameList)
tstyle = wx.TE_RICH2 | wx.TE_PROCESS_TAB | wx.TE_MULTILINE
self.text1 = wx.TextCtrl(panel, -1, size=(200, 180), style=tstyle)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.nameList, 0, wx.EXPAND|wx.ALL, 20)
sizer.Add(self.text1, 1, wx.EXPAND|wx.ALL, 20)
panel.SetSizer(sizer)
self.SetTopWindow(frame)
frame.Show(1)
return 1
def OnNameListSelected(self, evt):
import pprint
sel = evt.GetString()
result = None
if sel == 'alertDialog':
result = alertDialog(message='Danger Will Robinson')
elif sel == 'colorDialog':
result = colorDialog()
elif sel == 'directoryDialog':
result = directoryDialog()
elif sel == 'fileDialog':
wildcard = "JPG files (*.jpg;*.jpeg)|*.jpeg;*.JPG;*.JPEG;*.jpg|GIF files (*.gif)|*.GIF;*.gif|All Files (*.*)|*.*"
result = fileDialog(None, 'Open', '', '', wildcard)
elif sel == 'findDialog':
result = findDialog()
elif sel == 'fontDialog':
result = fontDialog()
elif sel == 'messageDialog':
result = messageDialog(None, 'Hello from Python and wxPython!',
'A Message Box', wx.OK | wx.ICON_INFORMATION)
#wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION)
#result = messageDialog(None, 'message', 'title')
elif sel == 'multipleChoiceDialog':
result = multipleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
elif sel == 'openFileDialog':
result = openFileDialog()
elif sel == 'saveFileDialog':
result = saveFileDialog()
elif sel == 'scrolledMessageDialog':
msg = "Can't find the file dialog.py"
try:
# read this source file and then display it
import sys
filename = sys.argv[-1]
fp = open(filename)
message = fp.read()
fp.close()
except:
pass
result = scrolledMessageDialog(None, message, filename)
elif sel == 'singleChoiceDialog':
result = singleChoiceDialog(None, "message", "title", ['one', 'two', 'three'])
elif sel == 'textEntryDialog':
result = textEntryDialog(None, "message", "title", "text")
if result:
#self.text1.SetValue(pprint.pformat(result.__dict__))
self.text1.SetValue(str(result))
app = MyApp(True)
app.MainLoop()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -