DietPI:_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量 [英] DietPI: _tkinter.TclError: no display name and no $DISPLAY environment variable

查看:54
本文介绍了DietPI:_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在dietPI 上运行一个python 脚本.我选择dietPI 是因为我想快速启动.

i want to run a python script on dietPI. I chose dietPI because i want to boot fast.

当我运行一个简单的 tkinter 程序时,我收到错误 _tkinter.TclError: no display name and no $DISPLAY environment variable我已经尝试过像这样设置显示变量:export DISPLAY=:0 (我也试过其他号码)

When i run a simple tkinter program i get the error _tkinter.TclError: no display name and no $DISPLAY environment variable Ive already tried setting a display variable like so: export DISPLAY=:0 (i tried other numbers aswell)

但后来我得到_tkinter.TclError:无法连接到显示:0.0"

有什么帮助吗?

推荐答案

这让我花了一整天才开始工作.(我还在不同的 SE 网站上发布了相同的答案:https://raspberrypi.stackexchange.com/a/118928/60683)

This took me all day to get working. (I also posted this same answer on a different SE site here: https://raspberrypi.stackexchange.com/a/118928/60683)

注意:这适用于 raspbian,主用户帐户使用默认用户名(pi")

以下是按顺序进行的步骤:

Here are the steps in order:

将您的 python 文件命名为 main.py 并将您的 python 文件拖到桌面上.

Name your python file main.py and drag your python file to the desktop.

接下来,创建一个新的纯文本文件并添加以下内容:

Next, create a new plain text file and add the following contents:

#!/bin/bash

python3 /home/pi/Desktop/main.py

将此文本文件保存为桌面上的 launch.command.这是用于启动您的 python 文件的 shell 脚本.默认情况下它不起作用,我们必须修改它(下一步)

Save this text file as launch.command on the desktop. This is shell script to launch your python file. By default it won't work, we have to chmod it (next step)

运行终端并运行以下命令:

Run terminal and run the following command:

chmod u+x /home/pi/Desktop/launch.command

现在双击launch.command(并选择execute)将启动你的python文件.

Now double clicking launch.command (and selecting execute) will launch your python file.

此时,我们已准备好让它在启动时启动.如果您在此之前进行过任何其他启动尝试,请撤消您所做的所有文件更改.

At this point, we are ready to get it launching on boot. If you've done any other launch attempts prior to this please undo all of those file changes you had done.

打开终端并运行以下命令:

Open terminal and run the following:

sudo nano/etc/systemd/system/myproject.service

我们暂时将其命名为 myproject,在您使用它之前不要更改任何内容,然后随时尝试重命名

在弹出的窗口中,复制粘贴以下内容:

In the window that pops up, copy paste the following exactly:

[Unit]
Description=Start Myproject
After=graphical.target
Wants=graphical.target

[Service]
User=pi
Group=pi
ExecStart=/bin/bash -c "export DISPLAY=:0; export XAUTHORITY=/home/pi/.Xauthority; /home/pi/Desktop/launch.command"

[Install]
WantedBy=graphical.target

现在按 ctrl+x 退出并保存,它会提示你保存更改,输入y",文件名应该已经输入为 .../myproject.service,如果是然后按回车它会保存并退出,如果文件名是空白的,它就搞砸了(这种情况有时会发生),退出终端并再次尝试所有这些,抱歉.

Now press ctrl+x to exit-and-save, it will prompt you to save changes, type "y", the filename should already be entered as .../myproject.service, if it is then press enter and it will save and exit, if the file name is blank it messed up (this just happens sometimes), quit terminal and try all of this again, sorry.

现在在终端输入:

sudo systemctl daemon-reload

然后

sudo systemctl enable myproject.service

最后

sudo reboot

在启动时,在其他任何显示之前(当然是在启动启动画面显示之后,但在桌面显示之前)您的 GUI 现在将启动,恭喜!

On launch, before anything else shows (after the boot up splash screen shows of course, but before desktop shows) your GUI will now launch, congrats!

我建议你全屏启动它,编辑你的python文件来使用它:

I recommend you launch it full screen, to do that edit your python file to use this:

root.attributes('-fullscreen', True)

注意,'root' 可能不是你所说的 TK() init .... 在你的代码末尾你有类似 XXXX.mainloop() 的东西,改变'root' 到任何 XXXX,在 TK()

NOTE, 'root' is likely not what you called your TK() init.... at the end of your code you have something like XXXX.mainloop(), change 'root' to whatever XXXX is, stick this right after your init of TK()

重新启动后,您可能会卡在应用程序中(尤其是当您全屏启动时),alt+f4 或 alt+f11 之类的东西不会终止您的程序.要退出,请按键盘上的窗口/菜单按钮,访问面板将出现,您可以转到附件>终端以启动新的终端窗口,然后运行以下命令:

Once you've reboot, you are likely stuck in the app (especially if you launched it full screen), things like alt+f4 or alt+f11 won't kill your program. To exit, press the windows/menu button on your keyboard, the access panel will appear you and can go to accessories>terminal to launch a new terminal window, then run the following:

sudo systemctl stop myproject.service

程序将被杀死,您将返回到您的标准桌面...

The program will be killed and you will be back to your standard desktop...

现在,为了防止它在启动时再次启动,只需将您的 launch.command 文件重命名为 DISABLED_launch.command,当您准备好在启动时启动再次设置名称.

NOW, to prevent it from launching again on bootup, simply rename your launch.command file to DISABLED_launch.command and when you're ready to start launching on bootup again set the name back.

去喝一杯吧,你赚到了.

Go get a drink, you've earned it.

此外,您显然可以将这些位置放在桌面以外的位置,但请确保在任何这些命令中始终使用完整路径,不要使用~/.."快捷方式.

这篇关于DietPI:_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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