简体   繁体   English

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

[英]PythonAnywhere: Tkinter.py error when trying to use easygui module

My son is trying to execute a small program using 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))

When running the code from the bash console, the follow error gets thrown: 从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

The module easygui was installed using pip install --user easygui, as suggested on PythonAnywhere's forums. 根据PythonAnywhere论坛上的建议,使用pip install --user easygui安装了easygui模块。

Since PA is a web-based environment, I'm not sure if there is some extra nuance. 由于PA是基于Web的环境,因此我不确定是否还有其他细微差别。 There are some suggestions online regarding this error when ssh'ing into a system. 网上有一些关于此错误的建议。 However, PA is accessed via web login, so that might not apply. 但是,可以通过Web登录访问PA,因此可能不适用。

Tkinter (and thus, easygui) requires an actual screen to draw on. Tkinter(因此,easygui)需要一个实际的屏幕来绘制。 The error is telling you it can't find the screen. 错误告诉您找不到屏幕。 Tkinter simply is not designed to run in a web-based environment. Tkinter根本不旨在在基于Web的环境中运行。

More info at this pythonanywhere help page on tkinter and other gui libraries . 有关tkinter和其他gui库的pythonanywhere帮助页面上的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM