【问题标题】:exe created using cx_freeze not working properly使用 cx_freeze 创建的 exe 无法正常工作
【发布时间】:2012-01-19 23:45:52
【问题描述】:
 use=input('what do you wanna do \n1.press w to create a new file\n2.press r to read a       file:\n')

 if use=='r':
    read()
 elif use=='w':
    write()
 else :
    print('OOPS! you enter a wrong input\n')
    user() 

当我使用 IDLE 运行此代码时,它运行正常,但是当我使用 cx_freeze 创建此 python 文件的 exe 时,if 和 elif 条件分别不适用于“r”和“w”。对于任何输入,它总是转到 else 语句。

我正在使用 python 3.2 和 cx_freeze 3.2

【问题讨论】:

  • 在input()之后添加print(repr(use)),这样您就可以看到use的实际内容。

标签: python python-3.x cx-freeze


【解决方案1】:

只是为了快速测试,我这样做了:

use = input("test input here: ")

for i in use:
    print(ord(i))

如果您输入“hello”,结果是 hello 的 ascii 字符代码加上“13”。这是\r,返回字符,它被添加到你的字符串中。这在 Linux 下不会发生,这是因为在 Windows 上换行符是 \r\n 而不是 \n。

您的解决方法是执行以下操作:

use = input("test input: ").strip("\r")

strip() 是一个字符串对象方法,它将从字符串的结尾和开头删除字符。

注意事项:

  1. 在上述示例中使用 ord() 可能不是最佳做法 - 请参阅 Unicode。
  2. 如果您曾经编写 GUI 并使用 cx_freeze,请不要使用 print() 或 input() - 在 Windows 上,GUI 应用程序根本不存在标准输入/输出句柄。用 cx_freeze + gui 代码让我绊倒了一段时间。只是当你到达那里时的一个说明。

【讨论】:

  • 使用 strip() 效果很好。非常感谢您的宝贵说明。
猜你喜欢
  • 2014-05-30
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多