无头服务器上的 Tkinter 设置 [英] Tkinter setup on headless server

查看:26
本文介绍了无头服务器上的 Tkinter 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个带有 Tkinter GUI 的 Python 应用程序,后来添加了一些 CLI 功能,以便在没有 GUI 的情况下在无头服务器 (Ubuntu Server 16.04) 上使用它.

I wrote a Python app with a Tkinter GUI, and later added in some CLI functionalities to use it without the GUI for use on a headless server (Ubuntu Server 16.04).

为了限制我必须更改的代码,一些 Tkinter 设置仍然完成,即使在 CLI 模式下

To limit the code I had to change, some of the Tkinter setup is still done, even in CLI mode

root = tk.Tk()
canvas = tk.Canvas(root, borderwidth=0)
frame = tk.Frame(canvas)
...

现在,如果我按照此处的建议使用 -X SSH 到服务器 34584827,效果很好.但我希望它运行的方式是 NodeJs 聊天机器人(在同一台服务器上无人值守运行)启动应用程序并使用结果.

Now if I ssh onto the server using -X as suggested here 34584827, it works fine. But the way I want it to run is that a NodeJs chatbot (running unattended on the same server) launches the app and uses the result.

这给了我错误:

Traceback (most recent call last):
  File "sim.py", line 60, in <module>
    root = tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1818, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

如果我能做些什么来挽救这种情况?我可以在聊天机器人中使用的参数,Tkinter 的配置......?

If there anything I can do to salvage this situation? A parameter I can use in the chatbot, a configuration for Tkinter...?

注意:此应用程序也使用 Matplotlib 生成图形,但在 37604289 处理那部分.

Note: this app also generates graphs with Matplotlib, but the solutions proposed in 37604289 take care of that part.

推荐答案

如果我能做些什么来挽救这种情况?

If there anything I can do to salvage this situation?

如果在命令行模式下运行,您唯一可以做的就是修改代码以不初始化 tkinter.您可以添加一个选项,也可以捕获实例化Tk 时抛出的异常.

The only thing you can do is modify your code to not initialize tkinter if running in command line mode. You can add an option, or you can catch the exception thrown when instantiating Tk.

这篇关于无头服务器上的 Tkinter 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆