首页 > 解决方案 > 如何在 Python 服务器版本上使用 Tkinter?

问题描述

我尝试在 Python 服务器版本上使用 Tkinter。(Linux)但我失败了。如何在 Python 服务器版本上使用 tkinter?

-----------------------------------------------------------------------------
import tkinter ; window = tkinter.Tk()
-----------------------------------------------------------------------------

TclError                                  Traceback (most recent call last)
<ipython-input-16-c9e945514158> in <module>
      1 import tkinter
----> 2 window = tkinter.Tk()

~/anaconda3/lib/python3.7/tkinter/__init__.py in __init__(self, screenName, baseName, className, useTk, sync, use)
   2021                 baseName = baseName + ext
   2022         interactive = 0
-> 2023         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   2024         if useTk:
   2025             self._loadtk()

TclError: no display name and no $DISPLAY environment variable

标签: pythontkinter

解决方案


将 DISPLAY 环境变量设置为:0.0一般来说,您可以在终端中使用以下命令执行此操作:

export DISPLAY=:0.0

或者在python中:

import os
os.environ["DISPLAY"] = ":0.0"

推荐阅读