2
\$\begingroup\$

I have over 300 questions 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 questions:

  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')
\$\endgroup\$
3
\$\begingroup\$
import wx

a1 = ['Apples', 'Bananas', 'Strawberries', 'Watermelon',
     "Don't remember", 'None of the above']

a2 = ['No', 'Yes']

a4 = ['No', 'Yes']

The Python style guide recommend ALL_CAPS for global constants.

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)

When strings get long enough that they force breaking across multiple lines, its usually best to move them to global constants

        if box.ShowModal() == wx.ID_OK:
            a_1 = box.GetSelections()


        print (a_1, '\n')

I don't think this does what you think it does. For python 2.x (which unless wxPython has been ported to python 3.x recently, must be what you are using), you shouldn't put parenthesis around your print statements.

            # 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:

If the answer to the previous question was "Yes", then box is now the result of asking question A3. However, otherwise its the still the previous question. As a result, it'll ask the same question again. You probably want to indent this if block so that it only happens if a_2 was Yes.

            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')

As for your questions:

  1. You can probably get away with creating that many windows. However, you can make sure that a dialog gets destroyed by calling its Destroy() method.
  2. I explained above what was wrong with your logic.
\$\endgroup\$
  • \$\begingroup\$ Thank you. So basically I need to change a1 to A1, and maybe create another module with all of the questions and answers and just import it to keep my code clean? Anyways, thanks for taking the time to give me a detailed answer. For some reason I keep having trouble with the indented blocks so I will try to check that next time when something goes wrong. Thanks again. \$\endgroup\$ – jerry Jun 6 '11 at 23:25
  • \$\begingroup\$ @Jerry, if you elaborate on what you plan on doing with the answers I can suggest the best way to structure that part. \$\endgroup\$ – Winston Ewert Jun 6 '11 at 23:45
  • \$\begingroup\$ Hey Winston, after the answers are all entered, I will be attempting to use pyodbc to write them to an MS Access Database. Hopefully it won't be too hard, but I am already finding your approach much easier than mine :) \$\endgroup\$ – jerry Jun 6 '11 at 23:48
  • \$\begingroup\$ @Jerry, basically, what I think should happen is to make a list of all questions that you can then just process in a for loop. There is some trickiness in doing that, but its probably the best way. \$\endgroup\$ – Winston Ewert Jun 7 '11 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.