source: SHX/trunk/src/sandbox/commondialogs.py @ 267

Revision 267, 4.3 KB checked in by marcus, 13 years ago (diff)

sandbox cleanup

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3# commondialogs.py
4
5import wx
6import os, sys
7
8class MyFrame(wx.Frame):
9    def __init__(self, parent, id, title):
10      wx.Frame.__init__(self, parent, id, title)
11
12      self.CreateStatusBar()
13      menuBar = wx.MenuBar()
14      menu = wx.Menu()
15      menu.Append(99,  "&Message Dialog", "Shows a Message Dialog")
16      menu.Append(100, "&Color Dialog", "Shows a Color Dialog")
17      menu.Append(101, "&File Dialog", "Shows a File Dialog")
18      menu.Append(102, "&Page Setup Dialog", "Shows a Page Setup Dialog")
19      menu.Append(103, "&Font Dialog", "Shows a Font Dialog")
20      menu.Append(104, "&Directory Dialog", "Shows a Directory Dialog")
21      menu.Append(105, "&SingleChoice Dialog", "Shows a SingleChoice Dialog")
22      menu.Append(106, "&TextEntry Dialog", "Shows a TextEntry Dialog")
23      menuBar.Append(menu, "&Dialogs")
24      self.SetMenuBar(menuBar)
25
26      self.Bind(wx.EVT_MENU, self.message, id=99)
27      self.Bind(wx.EVT_MENU, self.choosecolor, id=100)
28      self.Bind(wx.EVT_MENU, self.openfile, id=101)
29      self.Bind(wx.EVT_MENU, self.pagesetup, id=102)
30      self.Bind(wx.EVT_MENU, self.choosefont, id=103)
31      self.Bind(wx.EVT_MENU, self.opendir, id=104)
32      self.Bind(wx.EVT_MENU, self.singlechoice, id=105)
33      self.Bind(wx.EVT_MENU, self.textentry, id=106)
34
35    def message(self, event):
36        dlg = wx.MessageDialog(self, 'To save one life is as if you have saved the world.', 'Talmud', wx.OK|wx.ICON_INFORMATION)
37        dlg.ShowModal()
38        dlg.Destroy()
39
40        dlg = wx.MessageDialog(self, 'Unexcepted error occured!', 'Oops!', wx.OK|wx.ICON_ERROR)
41        dlg.ShowModal()
42        dlg.Destroy()
43
44    def choosecolor(self, event):
45        dlg = wx.ColourDialog(self)
46        dlg.GetColourData().SetChooseFull(True)
47        if dlg.ShowModal() == wx.ID_OK:
48            data = dlg.GetColourData()
49            self.SetStatusText('You selected: %s\n' % str(data.GetColour().Get()))
50        dlg.Destroy()
51
52    def openfile(self, event):
53       dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN)
54       if dlg.ShowModal() == wx.ID_OK:
55                path = dlg.GetPath()
56                mypath = os.path.basename(path)
57                self.SetStatusText("You selected: %s" % mypath)
58       dlg.Destroy()
59
60    def pagesetup(self, event):
61        dlg = wx.PageSetupDialog(self)
62        if dlg.ShowModal() == wx.ID_OK:
63            data = dlg.GetPageSetupData()
64            tl = data.GetMarginTopLeft()
65            br = data.GetMarginBottomRight()
66            self.SetStatusText('Margins are: %s %s' % (str(tl), str(br)))
67        dlg.Destroy()
68
69    def choosefont(self, event):
70        default_font = wx.Font(10, wx.SWISS , wx.NORMAL, wx.NORMAL, False, "Verdana")
71        data = wx.FontData()
72        if sys.platform == 'win32':
73            data.EnableEffects(True)
74        data.SetAllowSymbols(False)
75        data.SetInitialFont(default_font)
76        data.SetRange(10, 30)
77        dlg = wx.FontDialog(self, data)
78        if dlg.ShowModal() == wx.ID_OK:
79            data = dlg.GetFontData()
80            font = data.GetChosenFont()
81            color = data.GetColour()
82            text = 'Face: %s, Size: %d, Color: %s' % (font.GetFaceName(), font.GetPointSize(),  color.Get())
83            self.SetStatusText(text)
84        dlg.Destroy()
85
86    def opendir(self, event):
87        dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
88        if dlg.ShowModal() == wx.ID_OK:
89            self.SetStatusText('You selected: %s\n' % dlg.GetPath())
90        dlg.Destroy()
91
92    def singlechoice(self, event):
93        sins = ['Greed', 'Lust', 'Gluttony', 'Pride', 'Sloth', 'Envy', 'Wrath']
94        dlg = wx.SingleChoiceDialog(self, 'Seven deadly sins', 'Which one?', sins, wx.CHOICEDLG_STYLE)
95        if dlg.ShowModal() == wx.ID_OK:
96            self.SetStatusText('You chose: %s\n' % dlg.GetStringSelection())
97        dlg.Destroy()
98
99    def textentry(self, event):
100        dlg = wx.TextEntryDialog(self, 'Enter some text','Text Entry')
101        dlg.SetValue("Default")
102        if dlg.ShowModal() == wx.ID_OK:
103            self.SetStatusText('You entered: %s\n' % dlg.GetValue())
104        dlg.Destroy()
105
106class MyApp(wx.App):
107    def OnInit(self):
108        myframe = MyFrame(None, -1, "commondialogs.py")
109        myframe.CenterOnScreen()
110        myframe.Show(True)
111        return True
112
113app = MyApp(0)
114app.MainLoop()
115
Note: See TracBrowser for help on using the repository browser.