首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个wxPython窗口的问卷调查程序

使用多个wxPython窗口的问卷调查程序
EN

Code Review用户
提问于 2011-06-06 22:36:17
回答 1查看 447关注 0票数 2

我有超过300个问题,我计划包括在计划中。流程大致是这样的:

  • 创建一个带有问题的窗口
  • 将答案存储在变量中
  • 创建带有问题的新窗口
  • 存储新答案

(这将持续到300多个问题。)

我有两个问题:

  1. 这最终会不会导致崩溃,因为我创建了这么多的窗口?
  2. 如果对第二个问题(A2)选择“是”,则所有操作都适用于此代码,但如果选择“否”,则不起作用。你能看看你能不能找到它的什么毛病?
代码语言:javascript
复制
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')
EN

回答 1

Code Review用户

发布于 2011-06-06 23:17:00

代码语言:javascript
复制
import wx

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

a2 = ['No', 'Yes']

a4 = ['No', 'Yes']

Python指南推荐全局常量为ALL_CAPS。

代码语言:javascript
复制
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)

当字符串长度足够长以至于被迫跨越多条线时,通常最好将它们移动到全局常量。

代码语言:javascript
复制
        if box.ShowModal() == wx.ID_OK:
            a_1 = box.GetSelections()


        print (a_1, '\n')

我觉得这不像你想的那样。对于python2.x(除非最近已经将wxPython移植到python3.x,这必须是您正在使用的),您不应该将括号放在您的print语句后面。

代码语言:javascript
复制
            # 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:

如果对前一个问题的回答是“是”,那么框现在是问问题A3的结果。不过,不然的话,这仍然是前一个问题。因此,它会再次问同样的问题。您可能希望缩进这个if块,以便只有在a_2为Yes时才会发生这种情况。

代码语言:javascript
复制
            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')

至于你的问题:

  1. 您可能可以通过创建这么多窗口而不受影响。但是,您可以通过调用一个对话框()方法来确保一个对话框被销毁。
  2. 我上面解释了你的逻辑出了什么问题。
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/2837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档