我儿子正在尝试使用 pythonAnywhere 执行一个小程序。
import random, easygui
secret = random.randint(1,99)
guess = 0
tries = 0
easygui.msgbox ("AHOY! I'm the Dread Pirate Roberts, and I have a secret!\n"
"It is a number from 1 to 99. I'll give ye 6 tries.")
while guess != secret and tries < 6:
guess = easygui.integerbox ("What's yer guess, matey?")
if not guess: break
if guess < secret:
easygui.msgbox (str(guess) + "is too low, ye scurvy dog!")
elif guess > secret:
easygui.msgbox (str(guess) + "is too high, landlubber!" )
tries = tries + 1
if guess == secret:
easygui.msgbox ("Avast! Ye got it! Found me secret, ye did!" )
else:
easygui.msgbox ("No more guesses! The number was" + str(secret))
从 bash 控制台运行代码时,抛出以下错误:
File "NumGuess.py", line 6, in <module>
It is a number from 1 to 99. I'll give ye 6 tries.""")
File "/home/016646/.local/lib/python2.7/site- packages/easygui/boxes/derived_boxes.py", line 216, in msgbox
cancel_choice=ok_button)
File "/home/016646/.local/lib/python2.7/site-packages/easygui/boxes/base_boxes.py", line 66, in buttonbox
boxRoot = Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
按照 PythonAnywhere 论坛上的建议,使用 pip install --user easygui 安装模块 easygui。
由于 PA 是基于网络的环境,我不确定是否存在一些额外的细微差别。网上有一些关于 ssh 进入系统时出现此错误的建议。但是,PA 是通过 Web 登录访问的,因此这可能不适用。
最佳答案
Tkinter(因此,easygui)需要一个实际的屏幕来绘制。该错误告诉您找不到屏幕。 Tkinter 根本就不是为了在基于 Web 的环境中运行而设计的。
更多信息请访问 this pythonanywhere help page on tkinter and other gui libraries .
关于PythonAnywhere:尝试使用 easygui 模块时出现 Tkinter.py 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302455/