WxPython FAQ Dialogs

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog...)
(Удалено по требованию автора...)
 
Строка 1: Строка 1:
-
Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.
 
-
== A Simple message box ==
 
-
 
-
A message box provides short information to the user. A good example is a cd burning application. When a cd is finished burning, a message box pops up.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# message.py
 
-
 
-
import wx
 
-
 
-
class MessageDialog(wx.Frame):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Frame.__init__(self, parent, id, title)
 
-
 
-
        wx.FutureCall(5000, self.ShowMessage)
 
-
 
-
        self.Centre()
 
-
        self.Show(True)
 
-
 
-
    def ShowMessage(self):
 
-
        wx.MessageBox('Download completed', 'Info')
 
-
 
-
 
-
app = wx.App()
 
-
MessageDialog(None, -1, 'MessageDialog')
 
-
app.MainLoop()
 
-
 
-
</source>
 
-
 
-
<source lang="python">
 
-
wx.FutureCall(5000, self.ShowMessage)
 
-
</source>
 
-
<i>wx.FutureCall</i> calls a method after 5 seconds. The first parameter is a time value, after which a given method is called. The parameter is in milliseconds. The second parameter is a method to be called.
 
-
 
-
<source lang="python">
 
-
def ShowMessage(self):
 
-
    wx.MessageBox('Download completed', 'Info')
 
-
</source>
 
-
<i>wx.MessageBox</i> shows a small dialog window. We provide three parameters. The text message, the title message and finally the button.
 
-
 
-
[[image: wxPython_faq_messagedialog.png | center]]
 
-
 
-
== Predefined dialogs ==
 
-
wxPython has several predefined dialogs. These are dialogs for common programming tasks like showing text, receiving input , loading and saving files etc.
 
-
 
-
== Message dialogs ==
 
-
Message dialogs are used to show messages to the user. They are more flexible than simple message boxes, that we saw in the previous example. They are customizable. We can change icons and buttons that will be shown in a dialog.
 
-
<source lang="python">
 
-
wx.MessageDialog(wx.Window parent, string message, string caption=wx.MessageBoxCaptionStr,
 
-
  long style=wx.OK | wx.CANCEL | wx.CENTRE, wx.Point pos=(-1, -1))
 
-
</source>
 
-
 
-
<center>
 
-
{|
 
-
|-
 
-
!flag
 
-
!meaning
 
-
|-
 
-
|wx.OK
 
-
|show Ok button
 
-
|-
 
-
| id="gray" | wx.CANCEL
 
-
| id="gray" | show Cancel button
 
-
|-
 
-
|wx.YES_NO
 
-
|show Yes, No buttons
 
-
|-
 
-
| id="gray" | wx.YES_DEFAULT
 
-
| id="gray" | make Yes button the default
 
-
|-
 
-
|wx.NO_DEFAULT
 
-
|make No button the default
 
-
|-
 
-
| id="gray" | wx.ICON_EXCLAMATION
 
-
| id="gray" | show an alert icon
 
-
|-
 
-
|wx.ICON_ERROR
 
-
|show an error icon
 
-
|-
 
-
| id="gray" | wx.ICON_HAND
 
-
| id="gray" | same as wx.ICON_ERROR
 
-
|-
 
-
|wx.ICON_INFORMATION
 
-
|show an info icon
 
-
|-
 
-
| id="gray" | wx.ICON_QUESTION
 
-
| id="gray" | show a question icon
 
-
|}
 
-
</center>
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# messages.py
 
-
 
-
import wx
 
-
 
-
class Messages(wx.Frame):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Frame.__init__(self, parent, id, title, size=(250, 150))
 
-
 
-
        panel = wx.Panel(self, -1)
 
-
 
-
        hbox = wx.BoxSizer()
 
-
        sizer = wx.GridSizer(2, 2, 2, 2)
 
-
 
-
        btn1 = wx.Button(panel, -1, 'Info')
 
-
        btn2 = wx.Button(panel, -1, 'Error')
 
-
        btn3 = wx.Button(panel, -1, 'Question')
 
-
        btn4 = wx.Button(panel, -1, 'Alert')
 
-
 
-
        sizer.AddMany([btn1, btn2, btn3, btn4])
 
-
 
-
        hbox.Add(sizer, 0, wx.ALL, 15)
 
-
        panel.SetSizer(hbox)
 
-
 
-
 
-
        btn1.Bind(wx.EVT_BUTTON, self.ShowMessage1)
 
-
        btn2.Bind(wx.EVT_BUTTON, self.ShowMessage2)
 
-
        btn3.Bind(wx.EVT_BUTTON, self.ShowMessage3)
 
-
        btn4.Bind(wx.EVT_BUTTON, self.ShowMessage4)
 
-
 
-
        self.Centre()
 
-
        self.Show(True)
 
-
 
-
    def ShowMessage1(self, event):
 
-
        dial = wx.MessageDialog(None, 'Download completed', 'Info', wx.OK)
 
-
        dial.ShowModal()
 
-
 
-
    def ShowMessage2(self, event):
 
-
        dial = wx.MessageDialog(None, 'Error loading file', 'Error', wx.OK |
 
-
            wx.ICON_ERROR)
 
-
        dial.ShowModal()
 
-
 
-
    def ShowMessage3(self, event):
 
-
        dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
 
-
            wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
 
-
        dial.ShowModal()
 
-
 
-
    def ShowMessage4(self, event):
 
-
        dial = wx.MessageDialog(None, 'Unallowed operation', 'Exclamation', wx.OK |
 
-
            wx.ICON_EXCLAMATION)
 
-
        dial.ShowModal()
 
-
 
-
app = wx.App()
 
-
Messages(None, -1, 'Messages')
 
-
app.MainLoop()
 
-
</source>
 
-
 
-
In our example, we have created four buttons and put them in a grid sizer. These buttons will show four different dialog windows. We create them by specifying different style flags.
 
-
 
-
<source lang="python">
 
-
dial = wx.MessageDialog(None, 'Error loading file', 'Error', wx.OK |
 
-
    wx.ICON_ERROR)
 
-
dial.ShowModal()
 
-
</source>
 
-
 
-
The creation of the message dialog is simple. We set the dialog to be a toplevel window by providing None as a parent.
 
-
The two strings provide the message text and the dialog title. We show an ok button and an error icon by specifying the <i>wx.OK</i> and <i>wx.ICON_ERROR</i> flags.
 
-
To show the dialog on screen, we call the <i>ShowModal()</i> method.
 
-
 
-
[[image: wxPython_faq_idialog.jpg | center]]
 
-
[[image: wxPython_faq_qdialog.jpg | center]]
 
-
[[image: wxPython_faq_adialog.jpg | center]]
 
-
[[image: wxPython_faq_edialog.jpg | center]]
 
-
 
-
== About dialog box ==
 
-
 
-
Almost every application has a typical about dialog box. It is usually placed in the Help menu. The purpose of this dialog is to give the user the basic information about the name and the version of the application. In the past, these dialogs used to be quite brief. These days most of these boxes provide additional information about the authors. They give credits to additional programmers or documentation writers. They also provide information about the application licence. These boxes can show the logo of the company or the application logo. Some of the more capable about boxes show animation.
 
-
wxPython has a special about dialog box starting from 2.8.x series. It is not rocket science to make such a dialog manually. But it makes a programmer's life easier. 
 
-
<!--If the underlying platform has such a dialog box-->
 
-
 
-
The dialog box is located in the Misc module. In order to create an about dialog box we must create two objects. A <i>wx.AboutDialogInfo</i> and a <i>wx.AboutBox</i>.
 
-
 
-
<source lang="python">
 
-
wx.AboutDialogInfo()
 
-
</source>
 
-
 
-
We will call the following methods upon a <i>wx.AboutDialogInfo</i> object in our example. These methods are self-exlanatory.
 
-
 
-
<center>
 
-
{|
 
-
|-
 
-
!Method
 
-
!Description
 
-
|-
 
-
|SetName(string name)
 
-
|set the name of the program
 
-
|-
 
-
| id="gray" | SetVersion(string version)
 
-
| id="gray" | set the version of the program
 
-
|-
 
-
|SetDescription(string desc)
 
-
|set the description of the program
 
-
|-
 
-
| id="gray" | SetCopyright(string copyright)
 
-
| id="gray" | set the copyright fo the program
 
-
|-
 
-
|SetLicence(string licence)
 
-
|set the licence of the program
 
-
|-
 
-
| id="gray" | SetIcon(wx.Icon icon)
 
-
| id="gray" | set the icon to be show
 
-
|-
 
-
|SetWebSite(string URL)
 
-
|set the website of the program
 
-
|-
 
-
| id="gray" | SetLicence(string licence)
 
-
| id="gray" | set the licence of the program
 
-
|-
 
-
|AddDeveloper(string developer)
 
-
|add a developer to the developer's list
 
-
|-
 
-
| id="gray" | AddDocWriter(string docwirter)
 
-
| id="gray" | add a docwriter to the docwriter's list
 
-
|-
 
-
|AddArtist(string artist)
 
-
|add an artist to the artist's list
 
-
|-
 
-
| id="gray" | AddTranslator(string developer)
 
-
| id="gray" | add a developer to the translator's list
 
-
|}
 
-
</center>
 
-
 
-
The constructor of the <i>wx.AboutBox</i> is as follows. It takes a <i>wx.AboutDialogInfo</i> as a parameter.
 
-
 
-
<source lang="python">
 
-
wx.AboutBox(wx.AboutDialogInfo info)
 
-
</source>
 
-
 
-
 
-
wxPython can display two kinds of About boxes. It depends on which platform we use and which methods we call.
 
-
It can be a native dialog or a wxPython generic dialog. Windows native about dialog box cannot display custom icons, licence text nor the url's. If we omit these three fields, wx.Python will show a native dialog. Otherwise it will resort to a generic one. It is advised to provide licence information in a separate menu item, if we want to stay as native as possible.
 
-
GTK+ can show all these fields. 
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# aboutbox.py
 
-
 
-
import wx
 
-
 
-
ID_ABOUT = 1
 
-
 
-
class AboutDialogBox(wx.Frame):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Frame.__init__(self, parent, id, title, size=(260, 200))
 
-
 
-
        menubar = wx.MenuBar()
 
-
        help = wx.Menu()
 
-
        help.Append(ID_ABOUT, '&About')
 
-
        self.Bind(wx.EVT_MENU, self.OnAboutBox, id=ID_ABOUT)
 
-
        menubar.Append(help, '&Help')
 
-
        self.SetMenuBar(menubar)
 
-
 
-
        self.Centre()
 
-
        self.Show(True)
 
-
 
-
    def OnAboutBox(self, event):
 
-
        description = """File Hunter is an advanced file manager for the Unix operating
 
-
system. Features include powerful built-in editor, advanced search capabilities,
 
-
powerful batch renaming, file comparison, extensive archive handling and more.
 
-
"""
 
-
 
-
        licence = """File Hunter is free software; you can redistribute it and/or modify it
 
-
under the terms of the GNU General Public License as published by the Free Software Foundation;
 
-
either version 2 of the License, or (at your option) any later version.
 
-
 
-
File Hunter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 
-
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 
-
See the GNU General Public License for more details. You should have received a copy of
 
-
the GNU General Public License along with File Hunter; if not, write to
 
-
the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA"""
 
-
 
-
 
-
        info = wx.AboutDialogInfo()
 
-
 
-
        info.SetIcon(wx.Icon('icons/hunter.png', wx.BITMAP_TYPE_PNG))
 
-
        info.SetName('File Hunter')
 
-
        info.SetVersion('1.0')
 
-
        info.SetDescription(description)
 
-
        info.SetCopyright('(C) 2007 jan bodnar')
 
-
        info.SetWebSite('http://www.zetcode.com')
 
-
        info.SetLicence(licence)
 
-
        info.AddDeveloper('jan bodnar')
 
-
        info.AddDocWriter('jan bodnar')
 
-
        info.AddArtist('The Tango crew')
 
-
info.AddTranslator('jan bodnar')
 
-
 
-
        wx.AboutBox(info)
 
-
 
-
 
-
app = wx.App()
 
-
AboutDialogBox(None, -1, 'About dialog box')
 
-
app.MainLoop()
 
-
</source>
 
-
 
-
<source lang="python">
 
-
 
-
        description = """File Hunter is an advanced file manager for the Unix operating
 
-
system. Features include powerful built-in editor, advanced search capabilities,
 
-
powerful batch renaming, file comparison, extensive archive handling and more.
 
-
"""
 
-
</source>
 
-
 
-
It is not the best idea to put too much text into the code of the application. I don't want to make the example too complex, so I put all the text into the code. But in real world programs, the text should be placed separately inside a file. It helps us with the maintenace of our application. For example, if we want to translate our application to other languages.
 
-
 
-
<source lang="python">
 
-
info = wx.AboutDialogInfo()
 
-
</source>
 
-
 
-
The first thing to do is to create a <i>wx.AboutDialogInfo</i> object. The constructor is empty. It does not taky any parameters.
 
-
 
-
<source lang="python">
 
-
info.SetIcon(wx.Icon('icons/hunter.png', wx.BITMAP_TYPE_PNG))
 
-
info.SetName('File Hunter')
 
-
info.SetVersion('1.0')
 
-
info.SetDescription(description)
 
-
info.SetCopyright('(C) 2007 jan bodnar')
 
-
info.SetWebSite('http://www.zetcode.com')
 
-
info.SetLicence(licence)
 
-
info.AddDeveloper('jan bodnar')
 
-
info.AddDocWriter('jan bodnar')
 
-
info.AddArtist('The Tango crew')
 
-
info.AddTranslator('jan bodnar')
 
-
</source>
 
-
 
-
The next thing to do is to call all necessary methods upon the created <i>wx.AboutDialogInfo</i> object.
 
-
 
-
<source lang="python">
 
-
wx.AboutBox(info)
 
-
</source>
 
-
 
-
In the end we create a <i>wx.AboutBox</i> widget. The only parameter it takes is the <i>wx.AboutDialogInfo</i> object.
 
-
 
-
And of course, if we want to have an animation or some other eye candy, we must implement our about dialog manually.
 
-
 
-
[[image: wxPython_faq_aboutbox.png | center]]
 
-
 
-
== A custom dialog ==
 
-
In the next example we create a custom dialog. An image editing application can change a color depth of a picture.
 
-
To provide this funcionality, we could create a suitable dialog.
 
-
 
-
<source lang="python">
 
-
#!/usr/bin/python
 
-
# colordepth.py
 
-
 
-
import wx
 
-
 
-
ID_DEPTH = 1
 
-
 
-
class ChangeDepth(wx.Dialog):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Dialog.__init__(self, parent, id, title, size=(250, 210))
 
-
 
-
        panel = wx.Panel(self, -1)
 
-
        vbox = wx.BoxSizer(wx.VERTICAL)
 
-
 
-
        wx.StaticBox(panel, -1, 'Colors', (5, 5), (240, 150))
 
-
        wx.RadioButton(panel, -1, '256 Colors', (15, 30), style=wx.RB_GROUP)
 
-
        wx.RadioButton(panel, -1, '16 Colors', (15, 55))
 
-
        wx.RadioButton(panel, -1, '2 Colors', (15, 80))
 
-
        wx.RadioButton(panel, -1, 'Custom', (15, 105))
 
-
        wx.TextCtrl(panel, -1, '', (95, 105))
 
-
 
-
        hbox = wx.BoxSizer(wx.HORIZONTAL)
 
-
        okButton = wx.Button(self, -1, 'Ok', size=(70, 30))
 
-
        closeButton = wx.Button(self, -1, 'Close', size=(70, 30))
 
-
        hbox.Add(okButton, 1)
 
-
        hbox.Add(closeButton, 1, wx.LEFT, 5)
 
-
 
-
        vbox.Add(panel)
 
-
        vbox.Add(hbox, 1, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 10)
 
-
 
-
        self.SetSizer(vbox)
 
-
 
-
 
-
class ColorDepth(wx.Frame):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Frame.__init__(self, parent, id, title, size=(350, 220))
 
-
 
-
        toolbar = self.CreateToolBar()
 
-
        toolbar.AddLabelTool(ID_DEPTH, '', wx.Bitmap('icons/color.png'))
 
-
 
-
        self.Bind(wx.EVT_TOOL, self.OnChangeDepth, id=ID_DEPTH)
 
-
 
-
        self.Centre()
 
-
        self.Show(True)
 
-
 
-
    def OnChangeDepth(self, event):
 
-
        chgdep = ChangeDepth(None, -1, 'Change Color Depth')
 
-
        chgdep.ShowModal()
 
-
        chgdep.Destroy()
 
-
 
-
app = wx.App()
 
-
ColorDepth(None, -1, '')
 
-
app.MainLoop()
 
-
</source>
 
-
 
-
<source lang="python">
 
-
class ChangeDepth(wx.Dialog):
 
-
    def __init__(self, parent, id, title):
 
-
        wx.Dialog.__init__(self, parent, id, title, size=(250, 210))
 
-
</source>
 
-
 
-
In our code example we create a custom ChangeDepth dialog. We inherit from a <i>wx.Dialog</i> widget.
 
-
 
-
<source lang="python">
 
-
chgdep = ChangeDepth(None, -1, 'Change Color Depth')
 
-
chgdep.ShowModal()
 
-
chgdep.Destroy()
 
-
</source>
 
-
 
-
We instantiate a ChangeDepth class. Then we call the <i>ShowModal()</i> dialog. We must not forget to destroy our dialog. Notice the visual difference between the dialog and the top level window. The dialog in the following figure has been activated. We cannot work with the toplevel window until the dialog is destroyed.
 
-
There is a clear difference in the titlebar of the windows.
 
-
 
-
<center>[[image: wxPython_faq_colordepth1.png  | center]] [[image: wxPython_faq_colordepth2.png | center]]</center>
 
-
 
-
[[Категория:wxWidgets]]
 
-
[[Категория:Python]]
 

Текущая версия на 12:00, 7 апреля 2009