鱼C论坛

 找回密码
 立即注册
查看: 2275|回复: 4

[已解决]easygui作业问题

[复制链接]
发表于 2021-7-9 08:19:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想把原来的条件打印作业用easygui重新做一下就是第二讲后面的输入1到100的数字范围内就妹子漂亮范围外就大爷好丑。我用integerbox就是打不出大爷好丑。代码如下
import easygui as g
while True:
    msg = '请输入一个1到100之间的整数:'
    title = '条件打印输出'
    num = g.integerbox(msg, title,image=r'C:\Users\lili2\PycharmProjects\python学习\小甲鱼Python学习\turtle666.gif', lowerbound=1, upperbound=100)
    if not num:    # 按cancle时候退出循环
        break
    elif 1 <= num <= 100:
        g.msgbox('你妹好漂亮^_^')
        print('你妹好漂亮^_^')
    else:
        num == None:     # 是超出范围时num的结果是None
        g.msg('大爷好丑T_T')
else 本来就和if重合我也想不出怎么办
最佳答案
2021-7-9 09:18:13
[b]

这边代码有些问题:

第一: integerbox 点击 Cancle 时返回的是 None,超出范围时 integerbox 会自动弹出提示框

第二:else 中代码语法错误,且是 g.msgbox() 你函数输入错误

回到你代码,为什么 integerbox 不能弹出大爷你好丑呢,综上所述,第二点的代码错误是其中之一的原因

但更重要的是从第一点错误中超出范围是不会执行到后续代码的,也就是说你输入一个超出 lowerbound

upperbound 设置的范围值时,是无法执行到后续代码,所以我们要把范围增大

参考代码:

  1. import easygui as g

  2. while True:
  3.     msg = '请输入一个1到100之间的整数:'
  4.     title = '条件打印输出'
  5.     num = g.integerbox(msg, title, image=r'C:\Users\lili2\PycharmProjects\python学习\小甲鱼Python学习\turtle666.gif',
  6.                        lowerbound=-9999, upperbound=9999)
  7.     if num == None:
  8.         break
  9.     elif 1 <= num <= 100:
  10.         g.msgbox('你妹好漂亮^_^')

  11.     else:
  12.         g.msgbox('大爷好丑T_T')
复制代码
[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-9 09:09:07 | 显示全部楼层
本帖最后由 青出于蓝 于 2021-7-9 09:50 编辑
  1. import easygui as g
  2. while True:
  3.     msg = '请输入一个1到100之间的整数:'
  4.     title = '条件打印输出'
  5.     num = g.integerbox(msg, title,lowerbound=-12000,upperbound=12000,image=r'C:\Users\lili2\PycharmProjects\python学习\小甲鱼Python学习\turtle666.gif')
  6.     if not num:    # 按cancle时候退出循环
  7.         break
  8.     elif 1 <= num <= 100:
  9.         g.msgbox('你妹好漂亮^_^')
  10.         print('你妹好漂亮^_^')
  11.     else:
  12.         g.msg('大爷好丑T_T')
复制代码

把lowerbound,upperbound调整一下就好了
输入过大或过小会报错就是因为这啷个参数限定了范围,修改下就没问题了
有问题欢迎追问
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-9 09:18:13 | 显示全部楼层    本楼为最佳答案   
[b]

这边代码有些问题:

第一: integerbox 点击 Cancle 时返回的是 None,超出范围时 integerbox 会自动弹出提示框

第二:else 中代码语法错误,且是 g.msgbox() 你函数输入错误

回到你代码,为什么 integerbox 不能弹出大爷你好丑呢,综上所述,第二点的代码错误是其中之一的原因

但更重要的是从第一点错误中超出范围是不会执行到后续代码的,也就是说你输入一个超出 lowerbound

upperbound 设置的范围值时,是无法执行到后续代码,所以我们要把范围增大

参考代码:

  1. import easygui as g

  2. while True:
  3.     msg = '请输入一个1到100之间的整数:'
  4.     title = '条件打印输出'
  5.     num = g.integerbox(msg, title, image=r'C:\Users\lili2\PycharmProjects\python学习\小甲鱼Python学习\turtle666.gif',
  6.                        lowerbound=-9999, upperbound=9999)
  7.     if num == None:
  8.         break
  9.     elif 1 <= num <= 100:
  10.         g.msgbox('你妹好漂亮^_^')

  11.     else:
  12.         g.msgbox('大爷好丑T_T')
复制代码
[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-9 09:54:51 | 显示全部楼层
这是easygui源文件设定的问题,如果在默认范围值之外会
返回msgbox('The value that you entered is less than the lower bound of {}.'.format(lowerbound), "Error")
msgbox('The value that you entered is greater than the upper bound of {}.'.format(upperbound), "Error")

我们可以通过修改源文件的方式来达成我们的目的

  1. """

  2. .. moduleauthor:: easygui developers and Stephen Raymond Ferg
  3. .. default-domain:: py
  4. .. highlight:: python

  5. """

  6. try:
  7.     from .fillable_box import __fillablebox
  8.     from .button_box import buttonbox
  9.     from . import text_box as tb
  10.     from . import utils as ut
  11. except (SystemError, ValueError, ImportError):
  12.     from fillable_box import __fillablebox
  13.     from button_box import buttonbox
  14.     import text_box as tb
  15.     import utils as ut

  16. # -------------------------------------------------------------------
  17. # various boxes built on top of the basic buttonbox
  18. # -----------------------------------------------------------------------

  19. # -----------------------------------------------------------------------
  20. # ynbox
  21. # -----------------------------------------------------------------------


  22. def ynbox(msg="Shall I continue?", title=" ",
  23.           choices=("[<F1>]Yes", "[<F2>]No"), image=None,
  24.           default_choice='[<F1>]Yes', cancel_choice='[<F2>]No'):
  25.     """
  26.     Display a msgbox with choices of Yes and No.

  27.     The returned value is calculated this way::

  28.         if the first choice ("Yes") is chosen, or if the dialog is cancelled:
  29.             return True
  30.         else:
  31.             return False

  32.     If invoked without a msg argument, displays a generic
  33.     request for a confirmation
  34.     that the user wishes to continue.  So it can be used this way::

  35.         if ynbox():
  36.             pass # continue
  37.         else:
  38.             sys.exit(0)  # exit the program

  39.     :param msg: the msg to be displayed
  40.     :type msg: str
  41.     :param str title: the window title
  42.     :param list choices: a list or tuple of the choices to be displayed
  43.     :param str image: Filename of image to display
  44.     :param str default_choice: The choice you want highlighted
  45.         when the gui appears
  46.     :param str cancel_choice: If the user presses the 'X' close, which
  47.       button should be pressed

  48.     :return: True if 'Yes' or dialog is cancelled, False if 'No'
  49.     """
  50.     return boolbox(msg=msg,
  51.                    title=title,
  52.                    choices=choices,
  53.                    image=image,
  54.                    default_choice=default_choice,
  55.                    cancel_choice=cancel_choice)

  56. # -----------------------------------------------------------------------
  57. # ccbox
  58. # -----------------------------------------------------------------------


  59. def ccbox(msg="Shall I continue?", title=" ",
  60.           choices=("C[o]ntinue", "C[a]ncel"), image=None,
  61.           default_choice='Continue', cancel_choice='Cancel'):
  62.     """
  63.     Display a msgbox with choices of Continue and Cancel.

  64.     The returned value is calculated this way::

  65.         if the first choice ("Continue") is chosen,
  66.           or if the dialog is cancelled:
  67.             return True
  68.         else:
  69.             return False

  70.     If invoked without a msg argument, displays a generic
  71.     request for a confirmation
  72.     that the user wishes to continue.  So it can be used this way::

  73.         if ccbox():
  74.             pass # continue
  75.         else:
  76.             sys.exit(0)  # exit the program

  77.     :param str msg: the msg to be displayed
  78.     :param str title: the window title
  79.     :param list choices: a list or tuple of the choices to be displayed
  80.     :param str image: Filename of image to display
  81.     :param str default_choice: The choice you want highlighted
  82.       when the gui appears
  83.     :param str cancel_choice: If the user presses the 'X' close,
  84.       which button should be pressed

  85.     :return: True if 'Continue' or dialog is cancelled, False if 'Cancel'
  86.     """
  87.     return boolbox(msg=msg,
  88.                    title=title,
  89.                    choices=choices,
  90.                    image=image,
  91.                    default_choice=default_choice,
  92.                    cancel_choice=cancel_choice)

  93. # -----------------------------------------------------------------------
  94. # boolbox
  95. # -----------------------------------------------------------------------


  96. def boolbox(msg="Shall I continue?", title=" ",
  97.             choices=("[Y]es", "[N]o"), image=None,
  98.             default_choice='Yes', cancel_choice='No'):
  99.     """
  100.     Display a boolean msgbox.

  101.     The returned value is calculated this way::

  102.         if the first choice is chosen, or if the dialog is cancelled:
  103.             returns True
  104.         else:
  105.             returns False

  106.     :param str msg: the msg to be displayed
  107.     :param str title: the window title
  108.     :param list choices: a list or tuple of the choices to be displayed
  109.     :param str image: Filename of image to display
  110.     :param str default_choice: The choice you want highlighted
  111.       when the gui appears
  112.     :param str cancel_choice: If the user presses the 'X' close, which button
  113.       should be pressed
  114.     :return: True if first button pressed or dialog is cancelled, False if
  115.       second button is pressed
  116.     """
  117.     if len(choices) != 2:
  118.         raise AssertionError(
  119.             'boolbox takes exactly 2 choices!  Consider using indexbox instead'
  120.         )

  121.     reply = buttonbox(msg=msg,
  122.                       title=title,
  123.                       choices=choices,
  124.                       image=image,
  125.                       default_choice=default_choice,
  126.                       cancel_choice=cancel_choice)
  127.     if reply is None:
  128.         return None
  129.     if reply == choices[0]:
  130.         return True
  131.     else:
  132.         return False


  133. # -----------------------------------------------------------------------
  134. # indexbox
  135. # -----------------------------------------------------------------------
  136. def indexbox(msg="Shall I continue?", title=" ",
  137.              choices=("Yes", "No"), image=None,
  138.              default_choice='Yes', cancel_choice='No'):
  139.     """
  140.     Display a buttonbox with the specified choices.

  141.     :param str msg: the msg to be displayed
  142.     :param str title: the window title
  143.     :param list choices: a list or tuple of the choices to be displayed
  144.     :param str image: Filename of image to display
  145.     :param str default_choice: The choice you want highlighted
  146.       when the gui appears
  147.     :param str cancel_choice: If the user presses the 'X' close,
  148.       which button should be pressed
  149.     :return: the index of the choice selected, starting from 0
  150.     """
  151.     reply = buttonbox(msg=msg,
  152.                       title=title,
  153.                       choices=choices,
  154.                       image=image,
  155.                       default_choice=default_choice,
  156.                       cancel_choice=cancel_choice)
  157.     if reply is None:
  158.         return None
  159.     for i, choice in enumerate(choices):
  160.         if reply == choice:
  161.             return i
  162.     msg = ("There is a program logic error in the EasyGui code "
  163.            "for indexbox.\nreply={0}, choices={1}".format(
  164.                reply, choices))
  165.     raise AssertionError(msg)


  166. # -----------------------------------------------------------------------
  167. # msgbox
  168. # -----------------------------------------------------------------------
  169. def msgbox(msg="(Your message goes here)", title=" ",
  170.            ok_button="OK", image=None, root=None):
  171.     """
  172.     Display a message box

  173.     :param str msg: the msg to be displayed
  174.     :param str title: the window title
  175.     :param str ok_button: text to show in the button
  176.     :param str image: Filename of image to display
  177.     :param tk_widget root: Top-level Tk widget
  178.     :return: the text of the ok_button
  179.     """
  180.     if not isinstance(ok_button, ut.basestring):
  181.         raise AssertionError(
  182.             "The 'ok_button' argument to msgbox must be a string.")

  183.     return buttonbox(msg=msg,
  184.                      title=title,
  185.                      choices=[ok_button],
  186.                      image=image,
  187.                      default_choice=ok_button,
  188.                      cancel_choice=ok_button)


  189. def convert_to_type(input_value, new_type, input_value_name=None):
  190.     """
  191.     Attempts to convert input_value to type new_type and throws error if it can't.

  192.     If input_value is None, None is returned
  193.     If new_type is None, input_value is returned unchanged
  194.     :param input_value: Value to be converted
  195.     :param new_type: Type to convert to
  196.     :param input_value_name: If not None, used in error message if input_value cannot be converted
  197.     :return: input_value converted to new_type, or None
  198.     """
  199.     if input_value is None or new_type is None:
  200.         return input_value

  201.     exception_string = (
  202.         'value {0}:{1} must be of type {2}.')
  203.     ret_value = new_type(input_value)
  204. #        except ValueError:
  205. #            raise ValueError(
  206. #                exception_string.format('default', default, type(default)))
  207.     return ret_value


  208. # -------------------------------------------------------------------
  209. # integerbox
  210. # -------------------------------------------------------------------
  211. def integerbox(msg="", title=" ", default=None,
  212.                lowerbound=0, upperbound=99, image=None, root=None):
  213.     """
  214.     Show a box in which a user can enter an integer.

  215.     In addition to arguments for msg and title, this function accepts
  216.     integer arguments for "default", "lowerbound", and "upperbound".

  217.     The default, lowerbound, or upperbound may be None.

  218.     When the user enters some text, the text is checked to verify that it
  219.     can be converted to an integer between the lowerbound and upperbound.

  220.     If it can be, the integer (not the text) is returned.

  221.     If it cannot, then an error msg is displayed, and the integerbox is
  222.     redisplayed.

  223.     If the user cancels the operation, None is returned.

  224.     :param str msg: the msg to be displayed
  225.     :param str title: the window title
  226.     :param int default: The default value to return
  227.     :param int lowerbound: The lower-most value allowed
  228.     :param int upperbound: The upper-most value allowed
  229.     :param str image: Filename of image to display
  230.     :param tk_widget root: Top-level Tk widget
  231.     :return: the integer value entered by the user

  232.     """

  233.     if not msg:
  234.         msg = "Enter an integer between {0} and {1}".format(
  235.             lowerbound, upperbound)

  236.     # Validate the arguments for default, lowerbound and upperbound and
  237.     # convert to integers
  238.     default = convert_to_type(default, int, "default")
  239.     lowerbound = convert_to_type(lowerbound, int, "lowerbound")
  240.     upperbound = convert_to_type(upperbound, int, "upperbound")

  241.     while True:
  242.         reply = enterbox(msg, title, default, image=image, root=root)
  243.         if reply is None:
  244.             return None
  245.         try:
  246.             reply = convert_to_type(reply, int)
  247.         except ValueError:
  248.             msgbox('The value that you entered:\n\t"{}"\nis not an integer.'.format(reply), "Error")
  249.             continue
  250.         if lowerbound is not None:
  251.             if reply < lowerbound:
  252.                 msgbox('The value that you entered is less than the lower bound of {}.'.format(lowerbound), "Error")
  253.                 continue
  254.         if upperbound is not None:
  255.             if reply > upperbound:
  256.                 msgbox('The value that you entered is greater than the upper bound of {}.'.format(upperbound), "Error")
  257.                 continue
  258.         # reply has passed all validation checks.
  259.         # It is an integer between the specified bounds.
  260.         break
  261.     return reply







  262. # -------------------------------------------------------------------
  263. # enterbox
  264. # -------------------------------------------------------------------
  265. def enterbox(msg="Enter something.", title=" ", default="",
  266.              strip=True, image=None, root=None):
  267.     """
  268.     Show a box in which a user can enter some text.

  269.     You may optionally specify some default text, which will appear in the
  270.     enterbox when it is displayed.

  271.     Example::

  272.         reply = enterbox(....)
  273.         if reply:
  274.             ...
  275.         else:
  276.             ...

  277.     :param str msg: the msg to be displayed.
  278.     :param str title: the window title
  279.     :param str default: value returned if user does not change it
  280.     :param bool strip: If True, the return value will have
  281.       its whitespace stripped before being returned
  282.     :return: the text that the user entered, or None if he cancels
  283.       the operation.
  284.     """
  285.     result = __fillablebox(
  286.         msg, title, default=default, mask=None, image=image, root=root)
  287.     if result and strip:
  288.         result = result.strip()
  289.     return result


  290. def passwordbox(msg="Enter your password.", title=" ", default="",
  291.                 image=None, root=None):
  292.     """
  293.     Show a box in which a user can enter a password.
  294.     The text is masked with asterisks, so the password is not displayed.

  295.     :param str msg: the msg to be displayed.
  296.     :param str title: the window title
  297.     :param str default: value returned if user does not change it
  298.     :return: the text that the user entered, or None if he cancels
  299.       the operation.
  300.     """
  301.     return __fillablebox(msg, title, default, mask="*",
  302.                          image=image, root=root)


  303. # -----------------------------------------------------------------------
  304. # exceptionbox
  305. # -----------------------------------------------------------------------
  306. def exceptionbox(msg=None, title=None):
  307.     """
  308.     Display a box that gives information about
  309.     an exception that has just been raised.

  310.     The caller may optionally pass in a title for the window, or a
  311.     msg to accompany the error information.

  312.     Note that you do not need to (and cannot) pass an exception object
  313.     as an argument.  The latest exception will automatically be used.

  314.     :param str msg: the msg to be displayed
  315.     :param str title: the window title
  316.     :return: None

  317.     """
  318.     if title is None:
  319.         title = "Error Report"
  320.     if msg is None:
  321.         msg = "An error (exception) has occurred in the program."

  322.     codebox(msg, title, ut.exception_format())


  323. # -------------------------------------------------------------------
  324. # codebox
  325. # -------------------------------------------------------------------

  326. def codebox(msg="", title=" ", text=""):
  327.     """
  328.     Display some text in a monospaced font, with no line wrapping.
  329.     This function is suitable for displaying code and text that is
  330.     formatted using spaces.

  331.     The text parameter should be a string, or a list or tuple of lines to be
  332.     displayed in the textbox.

  333.     :param str msg: the msg to be displayed
  334.     :param str title: the window title
  335.     :param str text: what to display in the textbox
  336.     """
  337.     return tb.textbox(msg, title, text, codebox=True)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-9 17:14:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 20:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表