【问题标题】:If and else statements in easyGUI?easyGUI 中的 if 和 else 语句?
【发布时间】:2015-01-11 03:19:50
【问题描述】:

所以我有一个基本问题:如何在 easyGUI 中正确使用 if 和 else 语句? 这就是我所拥有的:

import easygui

msg = "Oh i see m9, choos your difficulty"
title = "Mountain Dew Franchise"
choices = ["Pro 360 noscoper(+1001)", "Dank skrubl0rd(-666)"]
choice = easygui.ynbox(msg, title, choices)

#if choices==choices[0]:
    easygui.msgbox("Good choos m20, let the skrubl0rd noscoping begin.")

#if choices==choices[1]:
    easygui.msgbox("Oh i see m8.")

# 行似乎是问题区域

它不会让我去任何一个 msgbox,而是只是关闭程序,任何帮助将不胜感激。

【问题讨论】:

  • 使用 easygui 时,if 语句的工作方式与其他任何地方没有任何不同。您应该担心的是您正在检查的值。

标签: python if-statement syntax msgbox easygui


【解决方案1】:
choice = easygui.ynbox(msg, title, choices)

这个ynbox 返回TrueFalse。这意味着choice 只能是这两个值之一。

if choices==choices[0]:

您正在比较您的列表 (choices) 是否等于同一列表中第一个元素的值。


要使您的程序正常运行,您需要稍微修改您的 if 部分。

if choice:
     easygui.msgbox("Good choos m20, let the skrubl0rd noscoping begin.")
else:
     easygui.msgbox("Oh i see m8.")

由于choice 只能是TrueFalse,并且choices 列表中的第一个选项成为True 值,因此此逻辑将起作用。

【讨论】:

    【解决方案2】:

    ynbox 返回TrueFalse,而不是您的choices 之一(这正是它在两个按钮上显示的内容!)。因此,将您的检查更改为 if choice:else:(并确保您的缩进是正确的 - 在您的 Q 中看起来很奇怪!-),您应该会没事的。

    【讨论】:

      【解决方案3】:

      一个可能有用的简单示例:

      from easygui import *
      
      
      msg  = "Flavor"
      title = "survey"
      choices = ["vanila", "chocolate", "foo","strbry"]
      choice = choicebox(msg, title, choices)
      
      if choice == "foo":
         print "your choice is good" 
      else:
         print "Try again its not a good choice !"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        • 2014-02-15
        • 2019-04-09
        相关资源
        最近更新 更多