这最终会导致崩溃吗(wxpython)
我计划将 300 多个问题/提示纳入该计划中。流程非常像这样:
创建一个包含问题的窗口。 将答案存储在变量中。 创建带有问题的新窗口。 存储新答案。
这将持续超过 300 个问题。
我有 2 个担忧:
1) 这是否最终会导致崩溃,因为我创建了如此多的窗口
2) 一切都可以使用如果您对第二个问题 (A2) 选择“是”,则此代码有效,但如果您选择“否”,则该代码不起作用。您能看看您是否能找出问题所在吗?
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')
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
圣牛。你并没有真正像那样链接对话框,是吗?尝试回答您的问题:
print
时失败。请参阅要点 #2。这里缺少很多东西,我没有看到任何错误处理,没有__main__
,缺少App()
等。因为你重复地重新分配的值>box
我认为您不太可能遇到内存问题,但这些是现阶段您最不关心的问题。如果单击“是”,则一切正常,但如果单击“否”,则一切都会失败:这是来自此
box.ShowModal() == wx.ID_OK
。仅当您从对话框中获得 OK 值时,您才创建变量a_#
。你可以这样做:a_1 = box.getSelections() if box.ShowModal() == wx.ID_OK else None
这里您可以用一些有意义的值替换
None
.. 请注意,这使用了 Python 三元语法,该语法是在 2.5 或 2.6 中引入的。它不适用于 2.4。综上所述,您可能想要创建的是一个向导。它们“通常用于将复杂的对话分解为几个简单的步骤”。 wxWidgets 上有一个教程,可能会有所启发。一旦你稍微了解了这一点,你应该研究一下 sizer,因为看起来你正在使用多行字符串来创建空格(?)。
Holy Cow. You're not really chaining the dialogs like that are you? To try to answer your questions:
print
following 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 ofbox
I 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 None
Here 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(?).