Let's take a look at the basics of wxPython by creating a simple text dialog. It will ask the user to input a string of text, and it will then print the string, unless the user exited the dialog by clicking the “X” or the “Cancel” button. In that case, we'll display another message to the user. Here's the code for our application, complete with comments to explain what we're doing: # Here we import wxPython: from wxPython.wx import * # Before we create anything, we must create an "App" object: application = wxPySimpleApp() # Now we create the dialog: dialog = wxTextEntryDialog ( None, 'Enter some text:', 'Title Here', 'Default text here.' ) # If the user presses, "OK", print the value. Otherwise, print another message. if dialog.ShowModal() == wxID_OK: print dialog.GetValue() else: print 'You did not push the "OK" button.' # Destroy the dialog dialog.Destroy() As you can see, the whole process isn't very complicated. The only thing to add to the above example is that the first argument passed to wxTextEntryDialog is the parent of the dialog. Since we're not working with any windows (yet), we pass None. Note that we mix our primitive GUI with a console. This is not necessary. Instead of printing the resulting message, we can display it in another dialog: from wxPython.wx import * application = wxPySimpleApp() dialog = wxTextEntryDialog ( None, 'Enter some text:', 'Title Here', 'Default text here.' ) if dialog.ShowModal() == wxID_OK:
# Create a "wxMessageDialog" to display the input: message = wxMessageDialog ( None, dialog.GetValue(), 'Title Here', wxOK ) message.ShowModal() message.Destroy() else: # Create another "wxMessageDialog" to display a simple message: message = wxMessageDialog ( None, 'You did not push the "OK" button.', 'Title Here', wxOK ) message.ShowModal() message.Destroy() dialog.Destroy() To get rid of the console completely, save the file with the extension “.pyw” rather than the default extension. Notice how we pass wxOK as wxMessageDialog's final argument. This creates our dialog with only an “OK” button. If we need a “Cancel” button, we can take out wxOK and pass nothing: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here' ) dialog.ShowModal() dialog.Destroy() We can also pass wxCANCEL in addition to wxOK: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxOK | wxCANCEL ) dialog.ShowModal() dialog.Destroy() As you saw earlier, it is very simple to check whether the user clicked “OK” or not: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here' ) if dialog.ShowModal() == wxOK: # User pressed "OK" pass dialog.Destroy() Of course, we can do more to our simple dialog. Instead of “OK” and “Cancel”, we can display “Yes” and “No”: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxYES_NO ) dialog.ShowModal() dialog.Destroy() We can also create an icon. Let's make an information icon ( “i” ): from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_INFORMATION ) dialog.ShowModal() dialog.Destroy() A question mark icon is also possible: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_QUESTION ) dialog.ShowModal() dialog.Destroy() So is an exclamation mark: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_EXCLAMATION ) dialog.ShowModal() dialog.Destroy() If we want to notify the user of an error, it is possible to put an error icon in: from wxPython.wx import * application = wxPySimpleApp() dialog = wxMessageDialog ( None, 'Just a message.', 'Title Here', wxICON_ERROR ) dialog.ShowModal() dialog.Destroy()
blog comments powered by Disqus |
|
|
|
|
|
|
|