GTK:如何忽略“无法打开显示”错误?
我使用 D 编程语言的 gtkD 绑定编写了一些 GTK 程序,这些程序原本是控制台应用程序,但能够在屏幕上显示绘图并将其保存到文件中。我想在只能通过基于控制台的 SSH 访问的计算机上运行这些程序,这意味着绘图不会显示在屏幕上,但仍会写入文件。
当我调用 Main.init() 时,我收到一条 Gtk-WARNING **: Cannot open display,如预期的那样。当我调用 Main.initCheck() 并忽略错误时,我只是在稍后的执行中收到更多与缺少屏幕相关的错误。
有没有一些简单的方法可以让我的程序忽略没有可用屏幕的事实,将其所有屏幕绘制到某个虚拟设备(相当于 /dev/null 的图形),并且仍然实际绘制到 Pixmaps 和 Pixbufs(对于将绘图保存到文件)并运行应用程序的非基于 GUI 的部分?
编辑:如果应用程序启动一个窗口并阻止事件循环,理想的做法是立即关闭窗口(或者一开始就没有成功打开它)并继续运行非基于 GUI 的部分的程序。如果这是不可能的,我可以通过确保不启动任何窗口来解决它。
此外,如果没有屏幕,Pixbuf 和 Pixmap 似乎无法工作。我尝试绘制到 Pixmap,从中创建一个 Pixbuf,并将结果保存到文件中,无论是在调用 Main.checkInit() 并忽略错误之后,还是在没有 init 语句的情况下,无论哪种方式 GTK 都会抱怨缺少屏幕。
I have written some GTK programs using the gtkD bindings for the D programming language that are otherwise console apps, but are capable of displaying plots on the screen and saving them to a file. I'd like to run these on a machine that I only have console-based SSH access to, which means that the plots wouldn't be displayed on the screen, but would still be written to files.
When I call Main.init(), I get a Gtk-WARNING **: cannot open display, as expected. When I call Main.initCheck() instead and ignore the errors, I simply get more errors later in execution related to the lack of a screen.
Is there some easy way to make my program ignore the fact that there's no screen available, do all its on-screen drawing to some dummy device (the graphics equivalent of /dev/null) and still actually draw to Pixmaps and Pixbufs (necessary for saving plots to files) and run the non-GUI-based parts of the app?
Edits: In cases where the app launches a window and blocks on an event loop, the ideal thing to do would be to close the window immediately (or not succeed in opening it in the first place) and continue running the non-GUI based parts of the program. If this is impossible I can work around it by making sure I don't launch any windows.
Also, it appears that Pixbufs and Pixmaps don't work without a screen present. I tried drawing to a Pixmap, creating a Pixbuf out of that, and saving the results to a file, either after calling Main.checkInit() and ignoring the errors or without an init statement and either way GTK complains about lack of a screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信最简单的解决方案是仅使用 GdkPixbuf 来处理绘图;据我所知,
gdk-pixbuf
库只处理内存中的pixbuf,不需要窗口系统。将代码的处理部分和显示部分严格分开。如果你这样做,没有屏幕应该没有关系。您甚至可以创建一个命令行选项来禁用在屏幕上绘图。您还可以使用
GtkOffscreenWindow
但这自 GTK 2.20 起才可用,据我所知,D 绑定最多只覆盖 2.18。或者,您可以在 SSH 会话中使用 X 转发;在 SSH 命令行上使用
-X
或-Y
。有关详细信息,请参阅 SSH 手册。如果您在通过 SSH 连接的计算机上运行 X 服务器,则绘图可以显示在本地计算机的屏幕上。I believe the simplest solution is to use only
GdkPixbuf
s for processing your plots; as far as I know, thegdk-pixbuf
library only deals with pixbufs in memory and doesn't need a window system. Keep the processing and display parts of your code strictly separated. If you do that, it shouldn't matter that there is no screen. You could even make a command-line option to disable drawing to the screen.You could also use
GtkOffscreenWindow
but this has only been available since GTK 2.20 and as far as I can tell the D bindings only cover up to 2.18.Alternatively, you could use X forwarding in your SSH session; use
-X
or-Y
on your SSH command line. See your SSH manual for more information. If you are running an X server on the machine you are SSH'ing from, then the plots can be displayed on your local machine's screen.如果没有服务器,像素图将无法工作 - 根据定义(X 术语),它们是存储在 X 服务器上的图像资源。然而,Pixbuf 存储在客户端应用程序中,它们应该在没有 X 的情况下工作。
如果您需要 Pixmap 但不想图形,您有两种选择:
-X
标志来启用 SSH X 隧道。在这种情况下,您的应用程序将能够使用本地 X 服务器。Pixmaps won't work without server - they are, by definition (in X terminology), image resources stored on X server. Pixbufs, however, are stored in client application, and they should work without X.
If you need Pixmaps but don't want to graphics, you have two choices:
-X
flag. In this case, your app will be able to use local X server.