Introduction
How to create a simple text-input using a dialog.
Special Concerns
Have in mind
- I program python for about 2 days
- I use wxWindow for bout 2 hours
- I think the documentation in wxWindow to be terrible overcomplicated
So, This is in no way easily reusable in a project, use just to understand the wxWindow's Dialogs usage.
Code Sample
1 import wx
2 class MyApp(wx.App):
3 def OnInit(self):
4 # Args below are: parent, question, dialog title, default answer
5 dlg = wx.TextEntryDialog(None,'What is your favorite programming language?','Eh??', 'Python')
6
7 # This function returns the button pressed to close the dialog
8 ret = dlg.ShowModal()
9 # Let's check if user clicked OK or pressed ENTER
10 if ret == wx.ID_OK:
11 print('You entered: %s\n' % dlg.GetValue())
12 else:
13 print('You don\'t know')
14
15 # The dialog is not in the screen anymore, but it's still in memory
16 #for you to access it's values. remove it from there.
17 dlg.Destroy()
18 return True
19
20 # Always use zero here. otherwise, you will have an error window that will last only nano seconds
21 # on the screen. dumb.
22 # (Only if the error is in the startup code before MainLoop is called. --RobinDunn)
23 app = MyApp(redirect = 0)
24 app.MainLoop()
Simple "Select directory" dialog
1 import wx
2 class MyApp(wx.App):
3 def OnInit(self):
4 # Args below are: parent, question, dialog title, default answer
5 dd = wx.DirDialog(None, "Select directory to open", "~/", 0, (10, 10), wx.Size(400, 300))
6
7 # This function returns the button pressed to close the dialog
8 ret = dd.ShowModal()
9
10 # Let's check if user clicked OK or pressed ENTER
11 if ret == wx.ID_OK:
12 print('You selected: %s\n' % dd.GetPath())
13 else:
14 print('You clicked cancel')
15
16 # The dialog is not in the screen anymore, but it's still in memory
17 #for you to access it's values. remove it from there.
18 dd.Destroy()
19 return True
20
21 # Always use zero here. otherwise, you will have an error window that will last only nano seconds
22 # on the screen. dumb.
23 # (Only if the error is in the startup code before MainLoop is called. --RobinDunn)
24 app = MyApp(redirect = 0)
25 app.MainLoop()
Comments
Later, more examples with other kinds of Dialogs