PythonAnywhere:尝试使用easygui模块时出现Tkinter.py错误

时间:2015-12-16 01:19:29

标签: python bash tkinter pythonanywhere

我的儿子正在尝试使用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

模块easygui是使用pip install --user easygui安装的,正如PythonAnywhere论坛上所建议的那样。

由于PA是基于网络的环境,我不确定是否存在一些额外的细微差别。当ssh进入系统时,网上有一些关于此错误的建议。但是,PA可以通过Web登录访问,因此可能不适用。

1 个答案:

答案 0 :(得分:5)

Tkinter(因此,easygui)需要一个实际的屏幕来绘制。错误告诉您它无法找到屏幕。 Tkinter根本不适合在基于Web的环境中运行。

this pythonanywhere help page on tkinter and other gui libraries的更多信息。