I have over 300 questions/prompts that I plan to include in the program. The flow is pretty much like this:
Create a window with the question.
Store answer in variable.
Create NEW window with question.
Store NEW answer.
this continues on for over 300 questions.
I have 2 concerns:
1) Will this eventually lead to a crash since I’m creating so many windows
2) Everything works with this code if you select ‘Yes’ to the second question (A2) but it does not work if you select ‘No’. Can you please see if you can find what’s wrong with it?
import wx
a1 = ['Apples', 'Bananas', 'Strawberries', 'Watermelon',
"Don't remember", 'None of the above']
a2 = ['No', 'Yes']
a4 = ['No', 'Yes']
class Fruit(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Fruit', size=(300,200))
#create panel and button
panel = wx.Panel(self)
# B1 - create multiple choice list
box = wx.MultiChoiceDialog(None, """
A1. What kind of fruit did you buy at the store?""", 'Fruit', a1)
if box.ShowModal() == wx.ID_OK:
a_1 = box.GetSelections()
print (a_1, '\n')
# A2 - create single choice list
box = wx.SingleChoiceDialog(None, """
A2. Do you like eating fruit?
""", 'Fruit', a2)
if box.ShowModal() == wx.ID_OK:
a_2 = box.GetStringSelection()
print (a_2, '\n')
if a_2 == 'Yes':
box = wx.TextEntryDialog(None, "A3. What kind of fruit is your favorite? ", "Fruit", "")
if box.ShowModal() == wx.ID_OK:
a_3 = box.GetValue()
print (a_3, '\n')
box = wx.SingleChoiceDialog(None, """
A4. Did you eat the fruit that you bought?
""", 'Fruit', a4)
if box.ShowModal() == wx.ID_OK:
a_4 = box.GetStringSelection()
print (a_4, '\n')
Thanks
Holy Cow. You’re not really chaining the dialogs like that are you? To try to answer your questions:
printfollowing someone clicking No. See bullet point #2. There’s a lot missing here, I don’t see any error handling, no__main__, missing anApp()etc. Because you’re repeatedly reassigning the value ofboxI don’t think you’re likely to encounter memory issues, but those are the least of your concerns at this stage.Everything works if you click Yes, but fails if you click No: That’s coming from this
box.ShowModal() == wx.ID_OK. You’re only creating the variablesa_#if you get the OK value from your Dialog. You could do this instead:a_1 = box.getSelections() if box.ShowModal() == wx.ID_OK else NoneHere you’d substitute in some meaningful value for
None.. Note that this uses the Python Ternary Syntax, which was introduced in 2.5 or 2.6. It would not work with 2.4.All that said, what you probably want to create is a Wizard. They are “typically used to decompose a complex dialog into several simple steps”. There’s a tutorial available here at wxWidgets that might shed some light. Once you’ve looked at that a little bit you should investigate sizers, as it appears you’re using multiline strings to create white spaces(?).