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

Revision 249, 4.2 KB checked in by marcus, 13 years ago (diff)

sandboxing...

  • 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    def choosecolor(self, event):
41        dlg = wx.ColourDialog(self)
42        dlg.GetColourData().SetChooseFull(True)
43        if dlg.ShowModal() == wx.ID_OK:
44            data = dlg.GetColourData()
45            self.SetStatusText('You selected: %s\n' % str(data.GetColour().Get()))
46        dlg.Destroy()
47
48    def openfile(self, event):
49       dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN)
50       if dlg.ShowModal() == wx.ID_OK:
51                path = dlg.GetPath()
52                mypath = os.path.basename(path)
53                self.SetStatusText("You selected: %s" % mypath)
54       dlg.Destroy()
55
56    def pagesetup(self, event):
57        dlg = wx.PageSetupDialog(self)
58        if dlg.ShowModal() == wx.ID_OK:
59            data = dlg.GetPageSetupData()
60            tl = data.GetMarginTopLeft()
61            br = data.GetMarginBottomRight()
62            self.SetStatusText('Margins are: %s %s' % (str(tl), str(br)))
63        dlg.Destroy()
64
65    def choosefont(self, event):
66        default_font = wx.Font(10, wx.SWISS , wx.NORMAL, wx.NORMAL, False, "Verdana")
67        data = wx.FontData()
68        if sys.platform == 'win32':
69            data.EnableEffects(True)
70        data.SetAllowSymbols(False)
71        data.SetInitialFont(default_font)
72        data.SetRange(10, 30)
73        dlg = wx.FontDialog(self, data)
74        if dlg.ShowModal() == wx.ID_OK:
75            data = dlg.GetFontData()
76            font = data.GetChosenFont()
77            color = data.GetColour()
78            text = 'Face: %s, Size: %d, Color: %s' % (font.GetFaceName(), font.GetPointSize(),  color.Get())
79            self.SetStatusText(text)
80        dlg.Destroy()
81
82    def opendir(self, event):
83        dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
84        if dlg.ShowModal() == wx.ID_OK:
85            self.SetStatusText('You selected: %s\n' % dlg.GetPath())
86        dlg.Destroy()
87
88    def singlechoice(self, event):
89        sins = ['Greed', 'Lust', 'Gluttony', 'Pride', 'Sloth', 'Envy', 'Wrath']
90        dlg = wx.SingleChoiceDialog(self, 'Seven deadly sins', 'Which one?', sins, wx.CHOICEDLG_STYLE)
91        if dlg.ShowModal() == wx.ID_OK:
92            self.SetStatusText('You chose: %s\n' % dlg.GetStringSelection())
93        dlg.Destroy()
94
95    def textentry(self, event):
96        dlg = wx.TextEntryDialog(self, 'Enter some text','Text Entry')
97        dlg.SetValue("Default")
98        if dlg.ShowModal() == wx.ID_OK:
99            self.SetStatusText('You entered: %s\n' % dlg.GetValue())
100        dlg.Destroy()
101
102class MyApp(wx.App):
103    def OnInit(self):
104        myframe = MyFrame(None, -1, "commondialogs.py")
105        myframe.CenterOnScreen()
106        myframe.Show(True)
107        return True
108
109app = MyApp(0)
110app.MainLoop()
111
Note: See TracBrowser for help on using the repository browser.